Skip to content

Commit

Permalink
Adding examples for Custom tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Atul Dwivedi authored and Atul Dwivedi committed Nov 3, 2016
1 parent bfbc920 commit 149c035
Show file tree
Hide file tree
Showing 18 changed files with 365 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/bin
/build
/WebContent/WEB-INF/lib
/WebContentMETA-INF
/WebContent/META-INF
.settings
.project
.classpath
Expand Down
21 changes: 21 additions & 0 deletions WebContent/WEB-INF/bts.tld
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<uri>http://atuldwivedi.com/ctsg1</uri>
<tag>
<name>expandAbr</name>
<tag-class>com.atuldwivedi.learnjsp.customtag.ExpandAbbreviationTag</tag-class>
<body-content>jsp</body-content>
</tag>

<tag>
<name>countword</name>
<tag-class>com.atuldwivedi.learnjsp.customtag.CountWordTag</tag-class>
<body-content>jsp</body-content>
<attribute>
<name>word</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
15 changes: 15 additions & 0 deletions WebContent/WEB-INF/err.tld
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<uri>http://atuldwivedi.com/ctags</uri>
<tag>
<name>loginError</name>
<tag-class>com.atuldwivedi.learnjsp.customtag.ErrorTagHandler</tag-class>
<body-content>empty</body-content>
<attribute>
<name>property</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
Binary file added WebContent/WEB-INF/files/Model 1 Architecture.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added WebContent/WEB-INF/files/Model 2 Architecture.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added WebContent/WEB-INF/files/Ticket.pdf
Binary file not shown.
16 changes: 16 additions & 0 deletions WebContent/WEB-INF/greet.tld
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<uri>http://atuldwivedi.com/ctag</uri>
<tag>
<name>greet</name>
<tag-class>com.atuldwivedi.learnjsp.customtag.GreetTagHandler
</tag-class>
<body-content>empty</body-content>
<attribute>
<name>uname</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
12 changes: 12 additions & 0 deletions WebContent/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>LearnJSP</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
20 changes: 20 additions & 0 deletions WebContent/custom-tags/body-tag-support.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="bts" uri="http://atuldwivedi.com/ctsg1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Learn BodyTagSupport</title>
</head>
<body>
<h1>
<bts:expandAbr>Learn JSP</bts:expandAbr>
</h1>

<bts:countword word="JSP">
JSP stands for Java Server Pages. JSP is extension of Servlet. JSP is used to develop dynamic web applications.
</bts:countword>

</body>
</html>
14 changes: 14 additions & 0 deletions WebContent/custom-tags/greet.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="g" uri="http://atuldwivedi.com/ctag"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Learn Custom Tags</title>
</head>
<body>
<g:greet uname="Atul" />
<br>
<g:greet uname="" />
</body>
</html>
12 changes: 12 additions & 0 deletions WebContent/custom-tags/home.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome Home!</title>
</head>
<body>
<h1>You have logged in successfully!</h1>
</body>
</html>
18 changes: 18 additions & 0 deletions WebContent/custom-tags/login.jsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="err" uri="http://atuldwivedi.com/ctags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Learn Custom Tags</title>
</head>
<body>
<form action="CTagsLoginServlet" method="post">
<label>User Name:</label><input type="text" name="userName" />
<font color="red"><err:loginError property="userName" /></font>
<br /> <label>Password:</label><input type="password" name="password" />
<font color="red"><err:loginError property="password" /></font>
<br /> <input type="submit" value="Login" />
</form>
</body>
</html>
52 changes: 52 additions & 0 deletions src/com/atuldwivedi/learnjsp/customtag/CountWordTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.atuldwivedi.learnjsp.customtag;

import java.io.Writer;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class CountWordTag extends BodyTagSupport {

/**
*
*/
private static final long serialVersionUID = 1L;

private String word;

@Override
public int doEndTag() throws JspException {
try {
String str = getBodyContent().getString();
Writer out = pageContext.getOut();
out.write(word +" count is: "+ countWord(str));
} catch (Exception e) {
e.printStackTrace();
}
return EVAL_PAGE;
}

@Override
public int doStartTag() throws JspException {
return EVAL_BODY_BUFFERED;
}

public void setWord(String word) {
this.word = word;
}

private int countWord(String str){
int count = 0;

for (int i = 0; i < str.length(); i++) {
int index = str.indexOf(word, i);
if(index >= 0){
count++;
i = index + 1;
}
}
System.out.println(count);
return count;
}

}
32 changes: 32 additions & 0 deletions src/com/atuldwivedi/learnjsp/customtag/ErrorTagHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.atuldwivedi.learnjsp.customtag;

import java.io.Writer;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class ErrorTagHandler extends TagSupport {

private static final long serialVersionUID = 1L;

private String property;

public void setProperty(String property) {
this.property = property;
}

@Override
public int doEndTag() throws JspException {
try {
Object message = pageContext.findAttribute(property);
if (message != null) {
Writer out = pageContext.getOut();
out.write(message.toString());
}
} catch (Exception e) {
e.printStackTrace();
}

return super.doEndTag();
}
}
34 changes: 34 additions & 0 deletions src/com/atuldwivedi/learnjsp/customtag/ExpandAbbreviationTag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.atuldwivedi.learnjsp.customtag;

import java.io.Writer;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class ExpandAbbreviationTag extends BodyTagSupport {

/**
*
*/
private static final long serialVersionUID = 1L;

@Override
public int doEndTag() throws JspException {
String bodyContent = getBodyContent().getString();
int index = bodyContent.indexOf("JSP");
String subString = bodyContent.substring(0, index - 1);

try {
Writer out = pageContext.getOut();
out.write(subString +" "+ "Java Server Pages");
} catch (Exception e) {
e.printStackTrace();
}
return EVAL_PAGE;
}

@Override
public int doStartTag() throws JspException {
return EVAL_BODY_BUFFERED;
}
}
31 changes: 31 additions & 0 deletions src/com/atuldwivedi/learnjsp/customtag/GreetTagHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.atuldwivedi.learnjsp.customtag;

import java.io.IOException;
import java.io.Writer;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class GreetTagHandler extends TagSupport {

private static final long serialVersionUID = 1L;
private String uname;

public void setUname(String uname) {
this.uname = uname;
}

@Override
public int doEndTag() throws JspException {
Writer out = pageContext.getOut();
try {
if (uname == null || uname.trim().isEmpty()) {
out.write("You have not specified your name.");
} else {
out.write("Hello " + uname);
}
} catch (IOException e) {
e.printStackTrace();
}
return super.doEndTag();
}
}
32 changes: 32 additions & 0 deletions src/com/atuldwivedi/learnjsp/servlet/BodyTagSupportServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.atuldwivedi.learnjsp.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class BodyTagSupportServlet
*/
@WebServlet("/BodyTagSupportServlet")
public class BodyTagSupportServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public BodyTagSupportServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
*/
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("custom-tag/body-tag-support.jsp").forward(request, response);
}

}
55 changes: 55 additions & 0 deletions src/com/atuldwivedi/learnjsp/servlet/CTagsLoginServlet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.atuldwivedi.learnjsp.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class CTagsLoginServlet
*/
@WebServlet("/CTagsLoginServlet")
public class CTagsLoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public CTagsLoginServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String userName = request.getParameter("userName");
String password = request.getParameter("password");

boolean err = false;
String page = "custom-tags/login.jsp";

if (userName == null || userName.trim().length() == 0) {
request.setAttribute("userName", "User name is mandatory");
err = true;
}

if (password == null || password.trim().length() == 0) {
request.setAttribute("password", "Password is mandatory");
err = true;
}

if (!err) {
page = "custom-tags/home.jsp";
}
request.getRequestDispatcher(page).forward(
request, response);
}

}

0 comments on commit 149c035

Please sign in to comment.