Java Server Page (JSP) is a technology that helps create web applications similarly to servlets. JSP pages contain HTML code and JSP tags, making them easier to maintain than servlets by separating design from development. JSP provides additional features like expression language and custom tags. JSP is an extension of servlets and allows using implicit objects, predefined tags, and expression language to simplify development compared to servlets. A JSP page is translated to a servlet by the JSP translator, then compiled and executed similarly to servlets through initialization, request processing, and destruction.
Java Server Page •Deisigned By: • Shivendra Srivastwa • Java and Android consultant
2.
Java Server Page(JSP) JSP technology is used to create web application just like Servlet technology. It can be thought of as an extension to the servlet because it provides more functionality than servlet. A JSP page contains HTML code and JSP tags. The jsp pages are easier to maintain than servlet because we can separate designing and development. It provides some additional features such as Expression Language, Custom Tag etc.
3.
Advantage of JSPover Servlet JSP is the extension to the servlet technology. We can use all the features of Servlet in JSP. In addition to, we can use implicit objects, predefined tags, expression language and Custom tags in JSP, that makes JSP development easy. JSP can be easily managed because we can easily separate our business logic with presentation logic. In servet, we mix our business logic with the presentation logic. If JSP page is modified, we don't need to redeploy the project. The servlet code needs to be updated and recompiled if we have to change the look and feel of the application.
4.
Life cycle ofa JSP Page Compilation of JSP Page Classloading (class file is loaded by the classloader) Instantiation (Object of the Generated Servlet is created). Initialization ( jspInit() method is invoked by the container). Reqeust processing ( _jspService() method is invoked by the container). Destroy ( jspDestroy() method is invoked by the container)
5.
JSP page istranslated into servlet by the help of JSP translator. The JSP translator is a part of webserver that is responsible to translate the JSP page into servlet. Afterthat Servlet page is compiled by the compiler and gets converted into the class file. Moreover, all the processes that happens in servlet is performed on JSP later like initialization, commiting response to the browser and destroy.
The JSP API TheJSP API consists of two packages: 1. javax.servlet.jsp package 2. javax.servlet.jsp.tagext
8.
javax.servlet.jsp package The Interfacesare as follows: 1. JspPage HttpJspPage The classes are as follows: 1. JspWriter 2. PageContext 3. JspFactory 4.JspEngineInfo 5.JspExceptionJspError 2.
9.
According to theJSP specification, all the generated servlet classes must implement the JspPage interface. It extends the Servlet interface. It provides two life cycle methods. The JspPage interface
10.
METHODS OF JSPPAGE INTERFACE publicvoid 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.
11.
THE 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.
12.
JSP scripting Element Thescripting elements provides the ability to insert java code inside the jsp. There are three types of scripting elements: Scripting elements: ◦ 1. scriptlet tag ◦ 2. expression tag ◦ 3. declaration tag
13.
Scriptlet tag A scriptlettag is used to execute java source code in JSP. Syntax is as follows: Syntax: <% java source code %>
14.
JSP expression tag Thecode placed within expression tag is written to the output stream of the response. So you need not write out.print() to write data. It is mainly used to print the values of variable or method. Syntax: <%= statement %>
15.
JSP declaration tag TheJSP declaration tag is used to declare fields and methods. The code written inside the jsp declaration tag is placed outside the service() method of auto generated servlet. So it doesn't get memory at each request Syntax: <%! statement %>
16.
Difference between thejsp scriptlet tag and jsp declaration tag The jsp scriptlet tag can only declare variables not methods whereas jsp declaration tag can declare variables as well as methods. The declaration of scriptlet tag is placed inside the _jspService() method whereas the declaration of jsp declaration tag is placed outside the _jspService() method.
17.
Implicit Object There are9 implicit objects available for the JSP page. The Auto Generated Servlet contains many objects like out, request, config, session, application etc.
19.
out implicit object Forwriting any data to the buffer, JSP provides an implicit object named out. Syntax: out.print();
20.
request implicit object InJSP, request is an implicit object of type HttpServletRequest. Let's see the simple example of request implicit object where we are printing the name of the user with welcome message. eg: request.getParameter
21.
response implicit object InJSP, response is an implicit object of type HttpServletResponse. Let's see the example of response implicit object where we are redirecting the request to the Google.
22.
config implicit object InJSP, config is an implicit object of type ServletConfig. This object can be used to get configuration information for a particular JSP page. This variable information can be used for one jsp page only.
23.
application implicit object InJSP, application is an implicit object of type ServletContext.This object can be used to get configuration information from configuaration file(web.xml). This variable information can be used for all jsp pages.
24.
session implicit object InJSP, session is an implicit object of type HttpSession.The Java developer can use this object to set,get or remove attribute or to get session information.
25.
pageContext implicit object InJSP, pageContext is an implicit object of type PageContext class.The pageContext object can be used to set,get or remove attribute from one of the following scopes: i. Page ii. Request Iii. Session iv. Application Note: page scope is the default scope
26.
exception implicit object InJSP, exception is an implicit object of type java.lang.Throwable class. This object can be used to print the exception. But it can only be used in error pages.It is better to learn it after page directive. Let's see a simple example: