Answerslab

Answerslab
Question and Answers | Ask Question | Share Information| Know Answers

Thursday, March 17, 2011

Java Server Pages


JSP(java server pages)
JSPs are compiled into servlets by a JSP compiler. The compiler either generates a servlet in Java code that is then compiled by the Java compiler, or it may compile the servlet to byte code which is directly executable. JSPs can alen to reload changes.
Regardless of whether the JSP compiler generates Java source code for a servlet or emits the byte code directly, it is helpful to understand how the JSP compiler transforms the page into a Java servlet. For example, consider the following input JSP and its resulting generated Java Servlet.
Input JSP
<%@ page errorPage="myerror.jsp" %>
 <%@ page import="com.foo.bar" %>
 
 <html>
 <head>
 <%! int serverInstanceVariable = 1;%>
 
 <% int localStackBasedVariable = 1; %>
 <table>
 <tr><td><%= toStringOrBlank( "expanded inline data " + 1 ) %></td></tr>
Resulting servlet
package jsp_servlet;
 import java.util.*;
 import java.io.*;
 import javax.servlet.*;
 import javax.servlet.http.*;
 import javax.servlet.jsp.*;
 import javax.servlet.jsp.tagext.*;
 
 import com.foo.bar; // Imported as a result of <%@ page import="com.foo.bar" %>
 importclass _myservlet implements javax.servlet.Servlet, javax.servlet.jsp.HttpJspPage {
    // Inserted as a
    // result of <%! int serverInstanceVariable = 1;%>
    int serverInstanceVariable = 1;public void _jspService( javax.servlet.http.HttpServletRequest request,
    javax.servlet.http.HttpServletResponse response )
    throws javax.servlet.ServletException,
    java.io.IOException
    {
        javax.servlet.ServletConfig config =; // Get the servlet config
        Object page = this;
        PageContext pageContext =;            // Get the page context for this request
        javax.servlet.jsp.JspWriter out = pageContext.getOut();
        HttpSession session = request.getSession( true );
        try {
            out.print( "<html>\r\n" );
            out.print( "<head>\r\n" );// From <% int localStackBasedVariable = 1; %>
            int localStackBasedVariable = 1;
            …
            out.print( "<table>\r\n" );
            out.print( " <tr><td>" );
            // From <%= toStringOrBlank( "expanded inline data " + 1 ) %>
            out.print( toStringOrBlank( "expanded inline data " + 1 ) );
            out.print( " </td></tr>\r\n" );} catch ( Exception _exception ) {
            // Clean up and redirect to error page in <%@ page errorPage="myerror.jsp" %>
        }
    }
 }

JavaServer Pages compiler
JavaServer Pages compiler, or JSP compiler, is a program that parses JavaServer Pages (JSPs), and transforms them into executable Java Servlets. A program of this type is usually embedded into an application server and run automatically the first time a JSP is accessed, but pages may also be precompiled for better performance, or compiled as a part of the build process to test for errors.


Most JSP containers support configuring how often the container checks JSP file timestamps to see if the page has changed. Typically, this timestamp would be set to a short interval (perhaps seconds) during software development, and a longer interval (perhaps minutes, or even never) for a deployed Web application.

No comments: