EJB & JSP: Java On The Edge, Unlimited Edition by Lou Marco ISBN: 0764548026 Your Guide to Cutting-Edge J2EE Programming Techniques. Chapter 8: JSPs and Servlets Revisited In previous chapters, you ve read much about the relationship between JavaServer Pages and Java servlets. In this chapter, you can take a closer look at this relationship. You can delve into the servlet life cycle and discover the differences between a raw servlet and a JSP-generated servlet. You can also read about important servlet and JSP methods, along with learning about the servlet environment. In addition, you can gain insight into why you need to use servlets, which is discussed in this chapter s section on using Java servlets and JSPs together. Examining the Servlet Life Cycle Because JSPs get translated into Java servlets, the JSP life cycle closely parallels that of servlets. In brief, when you request a JSP page, the translator generates a servlet and the Java compiler on the server compiles the generated servlet. Then the server invokes the class loader to load the servlet and start execution. If the servlet contains an init method, the container calls it. The init method runs only once. You can see that, for JSP-generated servlets, the analogue for the servlet init method is called jspInit. Both init and jspInit are optional methods. After execution of init or jspInit, the servlet executes its service method. The JSP equivalent for the service method is _jspService. The server can run multiple threads accessing the service or the _jspService method simultaneously, or you can force the server to single thread the method s access by using a single-threaded model using the isThreadSafe attribute of the page directive. Next, in servlets the service method invokes either the doGet or doPost method. The doGet and doPost methods of the servlet implement the get and post requests made from the client browser to the servlet, respectively. JSP-generated servlets do not have implementations for doPost and doGet. JSP- generated servlets perform both post and get requests in the _jspService method. Actually, a servlet can implement various do methods depending on the particulars of the HTTP request, such as doPut and doDelete methods. When the server unloads a servlet, the server invokes the destroy method. The JSP-generated equivalent to the destroy method is jspDestroy. As with init and jspInit, destroy and jspDestroy are optional. Writing the Minimal Servlet Given that most of the servlet methods discussed above are optional, you may wonder what is the smallest or minimal servlet? Listing 8-1 provides an example of a minimal servlet. Listing 8-1: A contender for the minimal servlet import java.io.* ; import java.text.* ; import java.util.* ; import javax.servlet.* ; import javax.servlet.http.* ; public class minimalServlet extends HttpServlet { public void doGet( HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { response.setContentType(”text/html”) ; PrintWriter out = response.getWriter() ; out.println(”
Hello World ” ) ; out.close() ; } public void doPost( HttpServletRequest request, HttpServletResponse response) throws IOException, ServletExceptionIf you are in search for clan hosting account you just came in to right place. We host many clan websites in almost all popular games like Counterstrike, Call Of Duty and other. More details about our offer you can find inside clan web hosting section.