Javaweb-servlet Development __web

Source: Internet
Author: User
A brief introduction of the servlet

The servlet is a technology offered by Sun to develop dynamic Web resources.
In its API, Sun provides a servlet interface that requires the following 2 steps to be completed if users want to use a Dynamic Web resource (that is, to develop a Java program to output data to the browser):
1. Write a Java class to implement the Servlet interface.
2, the development of a good Java class deployed to the Web server.
In accordance with a customary salutation, we usually also implement the Servlet interface Java program, called the servlet second, the servlet's running process

The servlet program is invoked by the Web server, and the Web server receives the client's servlet access request:
The ①web server first checks to see if the Servlet's instance object has been loaded and created. If so, perform the ④ step directly, otherwise, perform step ②.
② loads and creates an instance object for the servlet.
③ invokes the Init () method of the Servlet instance object.
④ creates a HttpServletRequest object that encapsulates the HTTP request message and a HttpServletResponse object that represents the HTTP response message, and then calls the servlet's service () Method and passes the request and response objects in as parameters.
Before the ⑤web application is stopped or restarted, the servlet engine unloads the servlet and calls the servlet's Destroy () method before uninstalling.

Third, the servlet call graph

  Iv. developing a servlet in eclipse

When you create a new Web project project in Eclipse, Eclipse automatically creates the directory structure shown in the following illustration:

  4.1, the Servlet interface implementation class

Servlet Interface Sun Company defines two default implementation classes: Genericservlet, HttpServlet.

HttpServlet refers to the servlet that handles HTTP requests, adding some HTTP protocol processing methods to the original Servlet interface, which is more powerful than the servlet interface. As a result, developers should typically inherit this class when writing a servlet and avoid directly implementing the Servlet interface.
HttpServlet when implementing the Servlet interface, the service method is overridden, and the code inside the method automatically determines how the user is requested, such as a GET request, and calls the HttpServlet Doget method, such as a POST request, to the Dopost method. As a result, when writing a servlet, developers usually only need to override the Doget or Dopost methods, rather than overwrite the service method. 4.2. Creating and writing servlet through Eclipse

Select the Gacl.servlet.study package, right-click →new→servlet, as shown in the following figure:

  

  

  

In this way, we create a servlet named ServletDemo1 through Eclipse, and the following code is created inside the ServletDemo01:

 1 package gacl.servlet.study;
 2 3 Import java.io.IOException;
 4 Import Java.io.PrintWriter;
 5 6 Import Javax.servlet.ServletException;
 7 Import Javax.servlet.http.HttpServlet;
 8 Import Javax.servlet.http.HttpServletRequest;
9 Import Javax.servlet.http.HttpServletResponse; The public class ServletDemo1 extends HttpServlet {/** * The Doget method of the servlet. <br&gt
;
The ' This ' is ' called when ' a form has its tag value ' equals to get. * @param request the request send by the client to the server * @param response the response SE  nd by the "server to" @throws servletexception if an error occurred * @throws IOException if             Error occurred/* public void doget (HttpServletRequest request, httpservletresponse response) 24 Throws Servletexception, IOException {response.setcontenttype ("text/html"); PrintWriter out = Response. Getwriter (); Out.println ("<!
DOCTYPE HTML public \-//w3c//dtd HTML 4.01 transitional//en\ ">");
Out.println ("<HTML>");
Out.println (" 

The code is automatically generated by Eclipse, and the Web.xml file has a lot of <servlet></servlet> and <servlet-mapping></servlet-mapping > Two pairs of labels, these two pairs of tags are configured ServletDemo1, as shown in the following figure:

We can then access the ServletDemo1 servlet through the browser, as shown in the following illustration:

   Five, servlet development attention to detail 5.1. servlet Access URL mapping configuration

Because the client accesses the resources in the Web server through the URL address, the servlet program must map the servlet program to a URL address, using the <servlet> element and < in the Web.xml file, if it wants to be accessed by the outside world. servlet-mapping> element Complete.
The <servlet> element is used to register the servlet, which contains two main child elements:<servlet-name> and <servlet-class> Used to set the registration name of the servlet and the full class name of the servlet, respectively.
A <servlet-mapping> element is used to map an external access path to a registered servlet that contains two child elements:<servlet-name> and <url-pattern> Used to specify the registration name of the servlet and the external access path of the servlet. For example:

1   <servlet>
2     <servlet-name>ServletDemo1</servlet-name>
3     < Servlet-class>gacl.servlet.study.servletdemo1</servlet-class>
4   </servlet>
5 
6   <servlet-mapping>
7     <servlet-name>ServletDemo1</servlet-name>
8     <url-pattern>/servlet/ServletDemo1</url-pattern>
9   </servlet-mapping>

The same servlet can be mapped to multiple URLs, that is, the set value of <servlet-name> child elements of multiple <servlet-mapping> elements can be the registered name of the same servlet. For example:

1  <servlet>
 2     <servlet-name>ServletDemo1</servlet-name>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.