Jump to content

JavaServer page example output

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Cholmes75 (talk | contribs) at 23:44, 27 May 2006 (+cat). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

For the input here, the java code for the resulting servlet class would look something like the following:

 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" %>
 import ...

 class _myserlvet 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>" );
             //note, toStringOrBlank() converts the expression into a string or if
             // the expression is null, it uses the empty string.
             //from <%= "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" %>
         }
    }
 }