|
JavaServer Pages (JSP)
Part:
1
2
What is JSP?
JSP is a technology, not a language. It allows Web page authors to put dynamic data
into a Web document with Java statements embedded in special HTML tags. The embedded Java
statements will be executed by the JSP enabled Web server, not by the Web browser.
JSP Page is a Web page, HTML document, with enbedded Java statements in a structure defined by the
JSP specification. Here is a JSP page example, hello.jsp:
<html><body>
<% out.println("Hello world!"); %>
</body></html>
Line 1 and 3 are normal HTML tags. But line 2 is a Java statement embedded in special HTML tag.
We know how a normal Web page is served by a normal Web server:
- Step 1: The Web browser sends a HTTP request to the Web server with the path name of
the Web page.
- Step 2: The Web server picks up the Web page by following the specified path name.
- Step 3: The Web server puts the content of the Web page without any changes into
a HTTP response.
- Step 4: The Web server sends the HTTP response to the Web browser.
Here is how a JSP page is served by a JSP Web server:
- Step 1: The Web browser sends a HTTP request to the Web server with the path name of
the JSP page.
- Step 2: The Web server checks to see if there is a compiled version of the requested
JSP page. If no, it compiles the requested JSP page immediately.
- Step 3: The Web server executes the compiled version of the requested JSP page, and
collects the output of the execution.
- Step 4: The Web server puts the output of the execution into a HTTP response.
- Step 5: The Web server sends the HTTP response to the Web browser.
There are two key processes involved in serving a JSP page:
- Compilation: A JSP page must be compiled into a Java Servlet class,
before it can be executed. The server can compile a JSP page in real-time when the page
is requested for the first time, if the page is not pre-compiled.
- Execution: When a JSP page is requested, its compiled class will be executed
on the server. The server will send back the output of the execution, not the content
of the JSP page.
"Hello world!" Java Servlet Class
To understand more about the JSP compilation process, let's use my hello.jsp again:
<html><body>
<% out.println("Hello world!"); %>
</body></html>
Then save hello.jsp to \local\jakarta-tomcat-4.1.18\webapps\ROOT,
and run IE with url: http://localhost:8080/hello.jsp.
You should see "Hello world!" in the IE window.
Now, if you look at the directory: \local\jakarta-tomcat-4.1.18\work\standalone\localhost\_,
you will see a Java file: hello_jsp.java,
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import org.apache.jasper.runtime.*;
public class hello_jsp extends HttpJspBase {
private static java.util.Vector _jspx_includes;
public java.util.List getIncludes() {
return _jspx_includes;
}
public void _jspService(HttpServletRequest request,
HttpServletResponse response)
throws java.io.IOException, ServletException {
JspFactory _jspxFactory = null;
javax.servlet.jsp.PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
try {
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html;charset=ISO-8859-1");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write("<html>");
out.write("<body>\r\n");
out.println("Hello world!");
out.write("\r\n");
out.write("</body>");
out.write("</html>");
} catch (Throwable t) {
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
out.clearBuffer();
if (pageContext != null) pageContext.handlePageException(t);
} finally {
if (_jspxFactory != null)
_jspxFactory.releasePageContext(pageContext);
}
}
}
(Continued on next part...)
Part:
1
2
|