By: Sugandha Gupta

` ` ` `

What is JSP? Understanding the need for JSP Evaluating the benefits of JSP Understanding the JSP lifecycle

Definition and some basic points to note

`

A JSP page is a page created by the web developer that includes JSP technology-specific and custom tags, in combination with other static (HTML or XML) tags. A JSP page has the extension .jsp or .jspx; this signals to the web server that the JSP engine will process elements on this page. (Using the web.xml deployment descriptor)

`

JSP Big Picture
GET /hello.jsp

Hello.jsp

<html>Hello!</html>

HelloServlet.java

Server w/ JSP Container

HelloServlet.class

Why JSP when you already have servlets?

`

`

JSP pages are compiled into servlets, so theoretically servlets alone can do the work and support your webbased applications. However, JSP technology was designed to simplify the process of creating pages by separating web presentation from web content. In many applications, the response sent to the client is a combination of template data and dynamicallygenerated data. To cater to this dynamic content requirement, JSP was more frequently used as it was comparatively less cumbersome and easier to read and understand.

A simple program in both technologies to show the difference between the two and why the latter is chosen over the former

public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<HTML>\n" + "<HEAD><TITLE>Hello</TITLE></HEAD>\n" + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1>Hello World</H1>\n" + "</BODY></HTML>"); } }

<html> <head> <title>Hello World JSP Page.</title> </head> <body> <font size="10"> "Hello World!´ </font> </body> </html>

Using Servlets out.println(³printing numbers from 1 to 10´); for(i=1;i<=10;i++) { out.println(³ number:´+i); }
`

`

Using JSP Printing numbers from 1 to 10 <% for(i=1;i<=10;i++) { %> Number:<%=i%> <% } %>

This code is comparatively cumbersome

This has easy separation from html and java code

How is JSP better than other competing technologies

`

`

JSP makes it easier to separate the designing code from the matter. So in an organization, a web developer and a web designer can work independently without each JSP makes it easier to use tools like Dream Weaver

`

JSP scripting elements let you insert code into the servlet that will be generated from the JSP page. There are three forms:

1. Expressions of the form <%= expression %>, which are evaluated and inserted into the servlet¶s output Eg: Current time: <%= new java.util.Date() %> 2. Scriptlets of the form <% code %>, which are inserted into the Service method. Eg: <% String queryData = request.getQueryString(); out.println("Attached data: " + queryData); %> 3. <%! declarations %> The declarations are inserted into the servlet class, not into a method

<%@ page contentType="text/html" %> <%@ taglib prefix="c" uri=http://java.sun.com/jstl/core %> <html> <head> <title>JSP is Easy</title> </head> <body > <h1>JSP is as easy as ...</h1> <%-- Calculate the sum of 1 + 2 + 3 dynamically --%> 1 + 2 + 3 = <c:out value="${1 + 2 + 3}" /> </body> </html>

<%@ page contentType="text/html" %> <%@ taglib prefix="c" uri=http://java.sun.com/jstl/core %> <html> <head> <title>JSP is Easy</title> </head> <body bgcolor="white"> <h1>JSP is as easy as ...</h1> <%-- Calculate the sum of 1 + 2 + 3 dynamically --%> 1 + 2 + 3 = <c:out value="${1 + 2 + 3}" /> </body> </html>

`

Java expression whose output is spliced into HTML <%= 1 + 1 %> <%= new java.util.Date() %>

<%@ page import="java.util.*" %> <% // Create an ArrayList with test data ArrayList list = new ArrayList( ); Map author1 = new HashMap( ); author1.put("name", "John Irving"); author1.put("id", new Integer(1)); list.add(author1); Map author2 = new HashMap( ); author2.put("name", "William Gibson"); author2.put("id", new Integer(2)); list.add(author2); Map author3 = new HashMap( ); author3.put("name", "Douglas Adams"); author3.put("id", new Integer(3)); list.add(author3); pageContext.setAttribute("authors", list); %> <html> <head><title>my web pageM/title></head> <body bgcolor=³blue´>

<%@ page language="java" contentType="text/html" %> <%! int globalCounter = 0; %> <html> <head> <title>A page with a counter</title> </head> <body bgcolor="white"> This page has been visited: <%= ++globalCounter %> times. <p> <% int localCounter = 0; %> This counter never increases its value: <%= ++localCounter %> </body> </html>

<%@ page contentType="text/html" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <html> <head> <title>JSP is Easy</title> </head> <body bgcolor="white"> <h1>JSP is as easy as ...</h1> <%-- Calculate the sum of 1 + 2 + 3 dynamically --%> 1 + 2 + 3 = <c:out value="${1 + 2 + 3}" /> </body> </html>

How it is created and then destroyed?

`

`

When a web container or servlet container receives a request from client for a jsp page, it takes the jsp through its various life cycle phases, and then returns the response to the client. When the page is created it goes through various stages of creation. They can be broadly classified into the following 3 stages:
Instantiation Request Processing Destruction

A diagrammatical representation of the JSP life cycle

`

When a web container receives a jsp request (may be first or subsequent), it checks for the jsp¶s servlet instance. If no servlet instance is available, then, the web container creates the servlet instance using following stages.
Translation Compilation Loading Instantiation Initialization

`

Web container translates (converts) the jsp code into a servlet code. This means that jsp is actually a servlet. After this stage, there is no jsp, everything is a servlet. Here on, the static content and dynamic contents are treated differently. The resultant is a java class instead of an html page.

`

The generated servlet is compiled to validate the syntax. As it is a java class, the compilation is done using µjavac¶ command. This will generate the byte code to be run on JVM.

`

The compiled byte code is loaded by the class loader used by web container. This is a standard process of using any java class.

`

In this step, instance of the servlet class is created so that it can serve the request.

`

Initialization is done. This is one time activity at the start of the initialization process. Initialization will make the ServletContext and ServletConfig objects available. One can access many attributes related to the web container and the servlet itself. After initialization the servlet is ready to process requests.

`

Entire initialization process is done to make the servlet available in order to process the incoming request. jspService() is the method that actually processes the request. It prints the response in html (any other) format, using µout¶ object.

`

`

Whenever the server is shutting down or when the server needs memory, the server removes the instance of the servlet. The destroy method jspDestroy() can be called by the server after initialization and before or after request processing. Once destroyed the jsp needs to be initialized again. Just to summarize, web container handles incoming requests to a jsp by converting it into a servlet and then by using this servlet to generate the response. Also when the server shuts down, the container needs to clear the instances.

Sign up to vote on this title
UsefulNot useful