JSP stands for Java Server Pages. A JSP page typically consists of HTML tags and JSP tags. It is a sever side technology to create dynamic platform-independent web content. JSP first converts into servlet with the help of the JSP container before processing the client’s request.
JSPÂ technology helps to create web-based applications just like Servlet technology. In other words, it can be thought of as an extension to Servlet because it provides more functionality than servlet. The JSP pages are quite easy to maintain as compared to Servlet because we can separate design and development. In addition, it provides some additional features such as Expression Language, Custom Tags, etc.
Syntax –
- Declaration Tags –
- It is used to declare variables.
- <%! Dec var %>
- Java Scriptlets–
- Â It allows us to add java code, variables, and expressions.
- <% java code %>
- JSP Expression –
- Â It evaluates and converts the expression to a string.
- <%= expression %>
- Comments –
- Â It contains the text that is added for information that has to be ignored.
- <% — Comments %>
Advantages of JSP over Servlet –
- Easy maintenance
- JSP is easily managable because we can easily separate our business logic with presentation logic.
- Quick development
- If the JSP page is modified, we don’t need to recompile and redeploy the project.
- Access available to entire APIs of JAVA
- JSP have access to the entire family of Java APIs, including the JDBC API to access enterprise databases.
- An extended version of Servlet
- In addition to features of Servlet, we can use implicit objects, predefined tags, expression language, and Custom tags in JSP.
- Reduction in code length
- In JSP, we can use many tags such as action tags, JSTL, custom tags, etc. that reduce the code.
Life cycle of JSP –
- Translation of JSP Page into Servlet by JSP Translator.
- The compilation of Servlet converts it to a class file.
- Classloading i.e. the classloader loads class file.
- Instantiation of a Generated Servlet is done.
- Initialization performed by jspInit() method.
- Request processing performed by jspService() method.
- Destruction performed by jspDestroy() method.
Creating a JSP Page –
- Write some HTML code and save it by .jsp extension. Let’s say we save it as index.jsp.
- Put it in a folder and paste the folder in the web-apps directory in apache tomcat to run the JSP page.
- Follow the directory structure –
- web-apps folder
- WEB_INF folder
- classes folder
- class files
- lib folder
- web.xml
- classes folder
- JSP files
- Static Resources like HTML, images, etc
- WEB_INF folder
- web-apps folder
- Start the server.
- Deploy on the server.
- Visit the browser by the URL http://localhost:8888/app/index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1">
<title>Hello World</title>
</head>
<body>
<%= "Hello World!" %>
</body>
</html>
Creating Custom Tag –
- Create the Tag handler class –
- To perform an action at the start or at the end of the tag.
- Inherit TagSupport class and overriding its method doStartTag().
- Use the JspWriter class to write data for JSP.
- The PageContext class provides getOut() method that returns the instance of the JspWriter class.
- TagSupport class provides an instance of pageContext by default.
- Create the TLD file –
- In the Tag Library Descriptor file we define tags i.e. it contains information of tag and Tag Hander classes.
- It must be present inside the WEB-INF directory.
- Create the JSP file –
- It uses the Custom tag defined in the TLD file.
- In addition, it is recommended to use the URI name instead of the full path of the TLD file.
- It uses taglib directive to use the tags defined in the TLD file.Â
public class MyTagHandler extends TagSupport {
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
System.out.println(Calender.getInstance().getTime());
} catch(Exception e) {
System.out.println(e);
}
return SKIP_BODY;
}
}
<tag>
<name>
Today
</name>
<tag-class>
com.project.MyTagHandler
</tag-class>
</tag>
<%@ taglib uri="WEB-INF/mytags.tld" prefix="m" %>