JavaSercer Pages Also note the following lines, which

JavaSercer Pages Also note the following lines, which add the current date and time to the response (shown in Figure 3.4): out.println(” It’s ” + (new java.util.Date( ).toString( )) + ” and all is well.”); Figure 3.4. The output from the Hello World servlet Example 3.2 shows a JSP page that produces the same result as the Hello World servlet. Example 3.2. Hello World JSP Page

Hello World

It’s <%= new java.util.Date( ).toString( ) %> and all is well. This is as simple as it gets. A JSP page is a regular HTML page, except that it may also contain JSP elements like the highlighted element in this example. This element inserts the same Java code in the page as was used in the servlet to add the current date and time. If you compare this JSP page to the corresponding servlet, you see that the JSP page can be developed using any web page editor that allows you to insert extra, non-HTML elements. And the same tool can later be used to easily modify the layout. This is a great advantage over a servlet with embedded HTML. The JSP page is automatically turned into a servlet the first time it’s requested, as described earlier. The generated servlet looks something like in Example 3.3. Example 3.3. Servlet Generated from JSP Page import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; import java.io.*; import org.apache.jasper.*; import org.apache.jasper.runtime.*; public class _0005chello_0002ejsphello_jsp_1 extends HttpJspBase { public void _ jspService(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { JspFactory _ jspxFactory = null; PageContext pageContext = null; HttpSession session = null; ServletContext application = null; ServletConfig config = null; JspWriter out = null; Object page = this; String _value = null; try { _ jspxFactory = JspFactory.getDefaultFactory( ); response.setContentType(”text/html”); pageContext = _ jspxFactory.getPageContext(this, request, response,”", true, 8192, true); page 30

Note: If you are looking for good and affordable webspace to host and run your servlet application check Virtualwebstudio servlet hosting services


Comments are closed.