Thursday, April 29, 2010

How to generate servlet source from a JSP


Understanding the XML view

The core JSP syntax is made up of scriptlets, actions, directives, expressions and declarations defined by the JSP specification. Web browsers do not understand JSP syntax as they do HTML. For the browser to display the contents of a JSP page, JRun must first compile the JSP source as a servlet which JRun then uses to generate the response. The response is HTML or whatever markup language is requested. However, the JSP-to-servlet compilation process is not a direct one. The JSP must first be transformed into XML.

The following steps occur when a JSP is requested for the first time:

  1. A client requests a JSP for the first time.
  2. JRun converts the JSP into a temporary XML representation called the XML view.
  3. JRun validates the XML view of the JSP page.
  4. JRun transforms the XML view into a .java class file that extends jrun.jsp.runtime.HttpJSPServlet and implements jrun.jsp.runtime.JRunJspPage. You can view the servlet's source code as described in "Viewing servlet source code".
  5. JRun compiles the new class into a servlet.
  6. The servlet generates the output based on the client request.

The following image shows the lifecycle of a JSP page when it is first requested by a client and finally compiled into a servlet. The servlet then returns the request to the client.

This images shows the flow of a JSP page when it is first requested by a client. The JSP page goes from *.JSP to XML View to *.JAVA to a compiled servlet. Then the servlet returns the request to the client.

Viewing servlet source code

You can view the source code of the servlet representation of the JSP page. You cannot view the XML view of the JSP page because it is only maintained in memory temporarily while JRun constructs the servlet.

JRun does not keep servlets generated from JSP source code by default. This section shows you how to configure JRun to keep the Java files and where to view them.

To keep generated JSPs:

  1. In the jrun_root/servers/jrun_server/SERVER-INF/default-web.xml file, set the JSPServlet's keepGenerated initialization parameter to true, as shown in the following example:
     <servlet>
         <servlet-name>JSPServlet</servlet-name>
         <servlet-class>jrun.jsp.JSPServlet</servlet-class>
         <init-param>
               <param-name>keepGenerated</param-name>
               <param-value>true</param-value>
         </init-param>
     </servlet>
  2. Delete the JSP class files in the web application's /WEB-INF/jsp directory.

    Doing this removes the already-generated servlet class files. JRun checks for the existence of the class file before recompiling a JSP. If the class files were still available, JRun would not recompile the JSP. JRun also checks the data/time stamp of the JSP, so you can change the JSP source code and save the JSP file to cause JRun to regenerate the Java source code and recompile the class.

  3. Restart the JRun server.
  4. Request the JSP.
  5. Open the JSP's generated Java class file (*.java) in the web application's /WEB-INF/jsp directory. JRun names the source code files starting with "jrun__" and appends version information to the name.

Source: http://livedocs.adobe.com



No comments:

Post a Comment