Showing posts with label jsp. Show all posts
Showing posts with label jsp. Show all posts

Thursday, June 30, 2011

Tomcat - JSP Precompilation


JSP is usually compiled during runtime by Java server. Some disadvantages are

1. If you JSP page is large, it will take time to compile at runtime. If that is the first hit, the user will have to wait before the page is served to the user. This is a performance bottlenect.

2. Although the current IDE, such as Eclipse, provide JSP syntax check, you may still run into the situation whereby runtime error occurs due to JSP runtime compilation issues.

To overcome these 2 issues, you can precompile your JSP page before putting them into the server. Tomcat come with JSP precompilation tools for your usage.

The following steps provide information on how to perform JSP precompilation

1. Make sure you have your Tomcat server installed. Or, you need the jar files from tomcat/bin and tomcat/lib, as well as tomcat/bin/catalina-tasks.xml. catalina-tasks.xml is a helper file for loading Catalina ant task for you, ie, jasper task.

2. Make sure you have Apache Ant installed

3. Add the following build script. This build script assume that your jsp source is at your web container.


<project name "Webapp Precompilation" default = "all" basedir= ".">
<import file = "${tomcat.home }/bin/catalina-tasks.xml" />
<target name= " jspc">
<jasper
validateXml = " false"
uriroot= "${webapp.path}"
webXmlFragment= "${webapp path }/WEB-INF/ generat ed we b xml "
outputDir= " $ {webapp.path}/WEB-INF/src" />
</target>

<target name= "compile">
<mkdir dir= " ${webapp.path}/WEB-INF/classes" />
<mkdir dir= " ${webapp.path }/WEB-INF/lib" />
<javac destdir= " ${webapp.path}/WEB-INF/classes"
optimize="off"
debug="on" failonerror= "false"
srcdir= " $ {webapp.path}/WEB-INF/src"
excludes= " ** /*.smap">
<classpath>
<pathelement location= "${webapp.path}/WEB-INF/classes"/>
<fileset dir= "${webapp.path}/WEB-INF/lib">
<include name= " .jar" />
</fileset>
<pathelement location= "${tomcat.home}/lib" />
<fileset dir= "${tomcat.home}/lib">
<include name= "*.jar" />
</fileset>
<fileset dir= "${tomcat.home}/bin">
<include name= "*.jar" />
</fileset>
</classpath>
<include name= " ** " />
<exclude name= "tags/**" />
</javac>
</target >
<target name= "all " depends = "jspc,compile">
</target>
<target name= "cleanup">
<delete>
<fileset dir= "${webapp.path}/WEB-INF/src" />
<fileset dir= "${webapp.path}/WEB-INF/classes/org/apache/jsp"/>
</delete>
</target >
</project>


4. run the script with ant -Dtomcat.home="your tomcat server install home" -Dwebapp.path="your jsp source path". As you can see, tomcat.home is used to locate catalina-tasks.xml and webapp.path is used to locate your libraries and jsp source code. By changing these variables accordingly, you can customize your build path.

5. By default, it will compile your jsp into class file and put them at your "webapp.path"/WEB_INF/classes

Now, how to use these JSP classes file. 2 ways

1. Locate "webapp.path"/WEB_INF/generated_web.xml. Copy these contents into your web.xml

2. Copy all files at "webapp.path"/WEB_INF/classes into your tomcat server work folder. The common path is "your_tomcat_home"/work/Catalina/localhost/_/

You may want to ask why are we not using Ant JSPC task. That task is deprecated due to known problem in Tomcat 1.5 and it won't be fix by Apache Ant as well.

To date, there are 2 known issues for JSP precompilation. below is the abstract from Tomcat



As described in bug 39089, a known JVM issue, bug 6294277, may cause a java.lang.InternalError: name is too long to represent exception when compiling very large JSPs. If this is observed then it may be worked around by using one of the following:
reduce the size of the JSP
disable SMAP generation and JSR-045 support by setting suppressSmap to true.

Friday, June 24, 2011

A sample JSP

We looked how servlet look here. Here, we are going to take a look at a sample JSP file and the contents of the file.

A JSP File Contents:

A JSP file can contain the following:
a. HTML contents
b. JavaScript
c. Java Code

Combining the features of the above 3 mentioned items; we get a powerful entity called the JSP. JSPs are used for the User Interface layer or the more colloquially called Front End layer of any J2EE application.

JSP Skeleton

Below is how a Skeleton JSP File would look like. (The file has to be saved as .jsp)
// Page Imports
<%@ page import = “com.xyz.ClassName %>

// Tag Library References
<%@ taglib URI = “path to Taglib file” prefix = “xx” %>
// here xx refers to the prefix with which the tag library will be referred to

// HTML Head & Title Content

// Java Script Content



// HTML Body & Form Contents

Note: Java code can be placed within the <% %> tags in the body part of the JSP page within the Body tags.

As you can see, a JSP file is pretty straightforward. Also, an important point to note here is the fact that, not all of the entities mentioned above are mandatory. You can include or exclude any of the entities mentioned above, based on your requirement and convenience.

Tomcat will now convert the Java embedded in the JSP to the following:

// begin [file="/jsp/my_first_jsp.jsp";from=(7,3);to=(7,31)]
int val1 = 10, val2=5;
// end

It also generates this version of the try block, which differs slightly from the previous servlet code:

try {

if (_jspx_inited == false) {
synchronized (this) {
if (_jspx_inited == false) {
_jspx_init();
_jspx_inited = true;
}
}
}
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html;charset=ISO-8859-1");
pageContext = _jspxFactory.getPageContext(this,
request, response,
"", true, 8192, true);

application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();

// HTML // begin [file="/jsp/my_first_jsp.jsp"...]
out.write("" +
"\r\n< html >\r\n< body >\r\n" +
"I Like Cars, Especially < b > Ferrari < / b >.
\r\n");

// end
// HTML // begin [file="/jsp/my_first_jsp.jsp";from=...]
out.write("\r\n ");

// end
// begin [file="/jsp/my_first_jsp.jsp";from=...]
out.print(val1 * val2);
// end
// HTML // begin [file="/jsp/my_first_jsp.jsp";from=...]
out.write("\r\n\r\n\r\n\r\n");

// end

}

As you can see, Tomcat now takes our val1 and val2 variables that were declared at the top of our JSP page and generates declarations as class variables in the servlet.

So, < % - val1 & val2 % > becomes

out.print(val1*val2);

Once the conversion is complete, this servlet will be compiled and loaded to memory. Every call to invoke this JSP will make Tomcat compare the modification date of the loaded servlet with the date of the JSP. If it is the same, the compiled servlet is executed and contents displayed on screen. Else, if it sees that the JSP has changed, it will recompile the JSP and load the newly converted Servlet instead of the older version.

Monday, May 16, 2011

JSP compilers

We can compile a JSP manually.
The JSP compiler(JSPC) depends on the Web Container. Eg. take, BEA WebLogic Application Server.

  • goto DOS Shell and run "setEnv.cmd"
  • using cd command move to the JSP file directory.
  • run " java weblogic.jspc -keepgenerated JspOne.jsp "

Note : if " -keepgenerated " is not used on above command, the JSPCompiler removes the .java file which is generated by JSPC.

Friday, April 1, 2011

JSP Scripting Elements

Scripting Elements are basically used to add contents to the HTML page dynamically.
Lets you add java code inside the HTML/JSP.

1) JSP Comments
-FORMAT <!-- -->
- used to comments for user information.

2) JSP Expressions
- used to insert dynamic results to the output page.
- FORMAT - <%= expression %>
- they are printed directly to the output page.
- e.g. Thanks for ordering <%= request.getParameter("title") %>
- e.g. Current time: <%= new java.util.Date() %>
- XML SYNTAX - Java Expression

3) JSP Scriptlets
- used to insert java code into the method that handles request for the page.
- FORMAT - <% code %>
- they are inserted into the _jspservice(called by service) method of the servlet.
- they have access to implicit objects automatically.
- e.g. <% String q = request.getQueryString(); out.println("GET data: " + q);%>
- they can be used for setting response headers and status codes, invoking side effects such as writing to the server log or updating a database, or executing code that contains loops, conditionals, or other complex constructs.
- XML SYNTAX - Code

4) JSP Declaraions
- used to add methods and field declarations to the servlet that corresponds to the JSP page.
- FORMAT - <%! Java Code %>
- they are inserted to the main body of the servlet class, outside the _jspService() and called by service to process the request.
- XML SYNTAX - Code
- implicit objects wont be accessible within declarations.

Chitika