Jsp&Servelet study notes (1)
Author:Eve Cole
Update Time:2009-07-02 17:15:10
1.1 Write a servelet program
1.2 Write a Jsp program
1.3 Compile a Servlet program
1.4 Package a Servlets and Jsps program
1.5 Build a deployment descriptor
1.1 Write a servelet program
Problem You want to write a servlet as a web application Solution Write a java class that inherits from javax.servlet.http.HttpServlet. First, a class package servlet.jar must be poured. You will need the class library inside to compile this servlet program.
Discussion A servlet program is a java class that is used to respond to dynamic content requested by the client through the network. If you are familiar with (CGI) programs, then java technology servlets can completely replace CGI programs. Often referred to as a web component, a servlet program executed in a runtime environment will be provided by a servles container such as Tomcat and Bea WebLogic.
Note: A web container can be
Servlets are installed in a web container as part of a web application. This application will contain WEB resources. Like HTML pages, images, multimedia clips, servlets, jsps, xml configuration files, java runtime classes and class libraries. When a web application is deployed into a web container, the container will generate and load instances of the java servlet class into the JVM. Handle requests for servlets.
All servlets inherit the javax.servlet.Servlet interface. Programmers who develop web applications write a special servlet
Inherited from javax.servlet.http.HttpServlet, an abstract class implements the Servlet interface and is specifically designed for handling HTTP requests.
When a web container generates a servlet instance, its basic sequence is as follows:
1. The servlet container first calls the init() method of the servlet, which will initialize a resource for the servlet to use. Listed as a logger. This init() method will only be called once during the entire servlet life cycle.
2. The init() method initializes an object, which inherits the java.servlet.ServletConfig interface. This object enables the servlet to initialize parameters declared in the deployment descriptor. ServletConfig also gives the servlet access to a javax.servlet.ServletContext object, which the servlet can use to log information, dispatch requests to other web components, and use other web resources in the same application.
3. The servlet container calls the service() method of this servlet to respond to some requests of the servlet. Based on HttpServlets, service() automatically calls the appropriate HTTP method to handle the request by calling the servlet's doGet() or doPost() method. For several examples, the user sends a Post request and the servlet responds to the request by executing the doPost() method.
4. When calling the doPost() and doGet() methods of the two main HttpServlets, the servlet container will generate javax..servlet.http.HttpServletRequest and HttpServletResponse objects and pass them as parameters to these request processing methods.
http://www.downcodes.com/
5. Manage the life cycle of a servlet, or determine the processing time of this servlet instance on the request request and the existence time on the Java virtual machine. When a servlet container starts to remove a servlet, the servlet's destroy() method will be called, in which all resources, such as a database connection, can be released.
Example A typical HttpServlet
package com.mydev;
import java.io.IOException; import java.io.PrintWriter;
import java.util.Enumeration; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//Must inherit the HttpServlet interface
public class FirstServlet extends HttpServlet ...{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException ...{
//set the MIME type of the response, "text/html"
response.setContentType("text/html");
//use a PrintWriter to send text data to the client who has requested the
//servlet
java.io.PrintWriter out = response.getWriter( );
//Begin assembling the HTML content out.println("<html><head>");
out.println("<title>Help Page</title></head><body>");
out.println("<h2>Please submit your information</h2>");
//make sure method="post" so that the servlet service method
//calls doPost in the response to this form submit
out.println( "<form method="post" action ="" + request.getContextPath( ) + "/firstservlet" >");
out.println("<table border="0"><tr><td valign="top">");
out.println("Your first name: </td> <td valign="top">");
out.println("<input type="text" name="firstname" size="20">");
out.println("</td></tr><tr><td valign="top">");
out.println("Your last name: </td> <td valign="top">");
out.println("<input type="text" name="lastname" size="20">");
out.println("</td></tr><tr><td valign="top">");
out.println("Your email: </td> <td valign="top">");
out.println("<input type="text" name="email" size="20">");
out.println("</td></tr><tr><td valign="top">");
out.println("<input type="submit" value="Submit Info"></td></tr>");
out.println("</table></form>"); out.println("</body></html>");
}
//doGet
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException ...{
//display the parameter names and values
Enumeration paramNames = request.getParameterNames( );
String parName;
//this will hold the name of the parameter
boolean emptyEnum = false;
if (! paramNames.hasMoreElements( ))
emptyEnum = true;
//set the MIME type of the response, "text/html"
response.setContentType("text/html");
//use a PrintWriter to send text data to the client
java.io.PrintWriter out = response.getWriter( );
//Begin assembling the HTML content
out.println("<html><head>");
out.println("<title>Submitted Parameters</title></head><body>");
if (emptyEnum) ...{
out.println( "<h2>Sorry, the request does not contain any parameters</h2>");
} else ...{
out.println( "<h2>Here are the submitted parameter values</h2>");
}
while(paramNames.hasMoreElements( )) ...{
parName = (String) paramNames.nextElement( );
out.println( "<strong>" + parName + "</strong> : " + request.getParameter(parName));
out.println("<br />");
}//while
out.println("</body></html>");
}
//doPost
}
You may notice that doGet() and doPost() will each throw ServletException and IOException. That's because response.getWriter() will throw an IO exception.
http://blog.csdn.net/bruceleey/archive/2007/02/07/1503843.aspx