How does JSP work? Behind the scenes, the
How does JSP work? Behind the scenes, the server automatically creates, compiles, loads, and runs a special servlet to generate the page’s content, as shown in Figure 2-12. You can think of this special servlet as a background, workhorse servlet. The static portions of the HTML page are generated by the workhorse servlet using the equivalent of out.println() calls, while the dynamic portions are included directly. For example, the servlet shown in Example 2-7 might be the background workhorse for hello1.jsp.* Example 2-7. The workhorse servlet for hello1.jsp* import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class _hello1_xjsp extends HttpServlet { public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType( text/html ); PrintWriter out = response.getWriter(); BufferedReader in = request.getReader(); out.println( ); out.printIn(
); if (request.getParameter( name ) == null) { out.println( Hello World ); } else { out.println( Hello, + request.getParameter( name )); } out.println(
); out.println( ); } } The first time you access a JSP page, you may notice that it takes a short time to respond. This is the time necessary for the server to create and compile the background servlet. Subsequent requests should be as fast as ever because the server can reuse the servlet. The one exception is when the .jsp file changes, in which case the server notices and recompiles a new background servlet. If there’s ever an error in compiling, you can expect the server to somehow report the problem, usually in the page returned to the client. * If you’re interested in seeing the true servlet source code for a JSP page, poke around the directories under your server root. After all, the server needs to save the Java source code somewhere before compiling it! If you find the true servlet source, you’re likely to see that it is far more complicated and convoluted than what is shown here.Note: If you are looking for inexpensive but high quality provider to host and run your serlvet application check Astra servlet hosting services