|
JavaServer Pages (JSP)
Part:
1
2
(Continued from previous part...)
You will also see a Java class file: hello_jsp.class.
What happened here was that
Tomcat, the JSP Web server, has translated hello.jsp into hello_jsp.java, and
compiled it to hello_jsp.class.
The Java file, hello_jsp.java, shows that:
- hello_jsp is a sub class of org.apache.jasper.runtime.HttpJspBase,
which is a sub class of javax.servlet.http.HttpServlet.
- The important method in hello_jsp is _jspService() with two objects listed
as parameters: one represents the Servlet request, and the other represents
the Servlet response.
- The static content of hello.jsp is translated into out.write() statements.
- The embedded Java statement in hello.jsp is copied directly.
Writing JSP Servlet Class Directly
Now we know that a JSP page is served by the JSP Web server by executing the JSP Servlet
Java class translated from the JSP page. This means that we can write a JSP Servlet
Java class directly, and ask the JSP Web server to serve it.
To try this idea, let's first write this JSP page, fake.jsp,
and save it to \local\jakarta-tomcat-4.1.18\webapps\ROOT:
<!--
- fake.jsp
- Copyright (c) 2002 by Dr. Herong Yang. All rights reserved.
-->
<html><body>
This a faked JSP page. The real content will come from the output
of the JSP Servlet class.
</body></html>
Then, write the following JSP Servlet class, fake_jsp.java:
and save it to \local\jakarta-tomcat-4.1.18\work\standalone\localhost\_:
/**
* fake_jsp.java
* Copyright (c) 2002 by Dr. Herong Yang. All rights reserved.
*/
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import org.apache.jasper.runtime.*;
public class fake_jsp extends HttpJspBase {
public java.util.List getIncludes() {
return null;
}
public void _jspService(HttpServletRequest request,
HttpServletResponse response)
throws java.io.IOException, ServletException {
JspFactory _jspxFactory = null;
javax.servlet.jsp.PageContext pageContext = null;
JspWriter out = null;
try {
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html;charset=ISO-8859-1");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
out = pageContext.getOut();
out.write("<html>");
out.write("<body>\r\n");
out.println("Hello world! - From Servlet");
out.write("\r\n");
out.write("</body>");
out.write("</html>");
} catch (Throwable t) {
if (out != null && out.getBufferSize() != 0)
out.clearBuffer();
if (pageContext != null) pageContext.handlePageException(t);
} finally {
if (_jspxFactory != null)
_jspxFactory.releasePageContext(pageContext);
}
}
}
Compile this class with JDK 1.3.1:
cd \local\jakarta-tomcat-4.1.18\work\standalone\localhost\_
set classpath=..\..\..\..\common\lib\servlet.jar
set classpath=%classpath%;..\..\..\..\common\lib\servlet.jar
\local\jdk1.3.1\bin\javac fake_jsp.java
Now, run IE with url: http://localhost:8080/fake.jsp. Guess what you will
get on the IE window? The text from the fake.jsp page, or the output of
fake_jsp.java?
You should see the output of fake_jsp.java. Tomcat has been fooled by the
file names and time stamps. When Tomcat receives a HTTP request for fake.jsp,
it will look for fake_jsp.class at the JSP Servlet directory. Since fake_jsp.class
is there and has newer time stamp than fake.jsp, it will assume fake_jsp.class
is the latest class translated from fake.jsp, and execute it immediately.
Be aware that if you modify fake.jsp and save it back. The next time when Tomcat
receives a request for fake.jsp, it will translate the new fake.jsp and replace
both fake_jsp.java and fake_jsp.class. The original fake_jsp.java will be gone.
Part:
1
2
|