Before learning the specification of the Java EE the servlet (Java EE specification--servlet), it is from the macro understanding of the servlet, know what he is used for? The servlet is now used in the project to solve the problem of how to use it. Use an example to summarize how to use it.
It takes three steps to create a new servlet program in MyEclipse. 1. The foreground display code. 2. inherit HttpServlet. Rewrite doget or dopost.3. Register the servlet in Web. Xml.
1. Foreground Display Code
<body>
2. inherit the HttpServlet. rewrite doget or dopost.
Inherited from Httpservletpublic class HelloServlet extends HttpServlet {@Overrideprotected void doget (httpservletrequest Request, HttpServletResponse response) throws Servletexception, IOException {//TODO auto-generated method STUBSYSTEM.OUT.PRINTLN ("Processing Get Request"); PrintWriter out=response.getwriter (); Out.println ("Hello servlet");} @Overrideprotected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//TODO auto-generated method Stub//super.dopost (req, resp);}}
3. Register the servlet in Web. Xml.<servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>servlet. helloservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name> helloservlet</servlet-name> <url-pattern>/servlet/HelloServlet</url-pattern> </ Servlet-mapping>
The servlet class that needs to be accessed is entered in the Servlet-class node. Url-pattern This node specifies the URL to access this class.
Summarize
An instance of the starter servlet, re - doget or Dopost,is the main way for the servlet to connect the server side to the client. In addition, MyEclipse also integrates the new Servlet application, which can be new->servlet directly in the MyEclipse, which makes it easier to develop.
Java EE Specification--servlet example