Servlet Development 1.ServletCreate
Servlet creation is very simple, there are two main methods: one is to create a common Java class to inherit the httpservlet class, in the manual configuration The Web. XML file registers the Servlet object. The other is created directly from the IDE Inheritance development tool.
2.ServletConfiguration
(1) Declaring a Servlet Object
InWeb. XMLin, through<servlet>label declares aServletobject. Under this tab there are mainly two sub-elements, respectively:<servlet-name>and the<servlet-class>. Which<servlet-name>element is used to specifyServletName , which can be a custom name;<servlet-class>element is used to specifyServletthe full location of the object. IncludeServletthe package name and class name of the object. Its life syntax format:
<servlet>
<servlet-name>SimpleServlet</servlet-name>
<servlet-class>com.zgy.servlet</servlet-class>
</servlet>
(2) Mapping Servlet
InWeb. XMLthe file declares theServletobject, you need to map the accessServletof theURL. This operation uses<servlet-mapping>label to configure. <servlet-mappig>The label contains two child elements, each of which is a<servlet-name>and the<url-pattern>. Among them,<servlet-name>Elements and<servlet>in the label<servlet-name>elements should not be arbitrarily changed to name. <url-pattern>element is used to map accessURL. Its configuration method:
<servlet-mapping>
<servlet-name>SimpleServlet</servlet-name>
<url-pattern>/SimpleServlet</url-pattern>
</servlet-mapping>
Example:
Package com.zgy.servlet;
Import java.io.IOException;
Import Java.io.PrintWriter;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
public class Myservlet extends HttpServlet {
/**
* The Doget method of the servlet. <br>
*
* This method was called when a form had its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response The response send by the server to the client
* @throws servletexception If an error occurred
* @throws IOException If an error occurred
*/
public void doget (HttpServletRequest request, httpservletresponse response)
Throws Servletexception, IOException {
Response.setcontenttype ("text/html");
Response.setcharacterencoding ("GBK");
PrintWriter out = Response.getwriter ();
Out.println ("<! DOCTYPE HTML public \ "-//W3C//DTD HTML 4.01 transitional//en\" > ");
Out.println ("<HTML>");
Out.println ("example </TITLE></HEAD>");
Out.println ("<BODY>");
Out.print ("Servlet instance ");
Out.print (This.getclass ());
Out.println (", using the GET method");
Out.println ("</BODY>");
Out.println ("</HTML>");
Out.flush ();
Out.close ();
}
}
Web. XML Configuration
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "3.0"
Xmlns= "Http://java.sun.com/xml/ns/javaee"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd ">
<display-name></display-name>
<servlet>
<description>this is the description of my EE component</description>
<display-name>this is the display name of my EE component</display-name>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.zgy.servlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/servlet/MyServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
JSP detailed article--servlet (ii)