The JSP API consists of two packages:
- javax.servlet.jsp
- javax.servlet.jsp.tagext
javax.servlet.jsp package
The javax.servlet.jsp package has two interfaces and classes.The two interfaces are as follows:
- JspPage
- HttpJspPage
The classes are as follows:
- JspWriter
- PageContext
- JspFactory
- JspEngineInfo
- JspException
- JspError
The JspPage interface
According to the JSP specification, all the generated servlet classes must implement the JspPage interface. It extends the Servlet interface. It provides two life cycle methods.
Methods of JspPage interface
- public void jspInit(): It is invoked only once during the life cycle of the JSP when JSP page is requested firstly. It is used to perform initialization. It is same as the init() method of Servlet interface.
- public void jspDestroy(): It is invoked only once during the life cycle of the JSP before the JSP page is destroyed. It can be used to perform some clean up operation.
The HttpJspPage interface
The HttpJspPage interface provides the one life cycle method of JSP. It extends the JspPage interface.
Method of HttpJspPage interface:
- public void _jspService(): It is invoked each time when request for the JSP page comes to the container. It is used to process the request. The underscore _ signifies that you cannot override this method.
JSP Scriptlet tag
In JSP, java code can be written inside the jsp page using the scriptlet tag. Let's see what are the scripting elements first.
JSP Scripting elements
The scripting elements provides the ability to insert java code inside the jsp. There are three types of scripting elements:
- scriptlet tag
- expression tag
- declaration tag
JSP scriptlet tag
A scriptlet tag is used to execute java source code in JSP. Syntax is as follows:
Example of JSP scriptlet tag
In this example, we are displaying a welcome message.
Example of JSP scriptlet tag that prints the user name
In this example, we have created two files index.html and welcome.jsp. The index.html file gets the username from the user and the welcome.jsp file prints the username with the welcome message.
File: index.html
File: welcome.jsp
No comments:
Post a Comment