Java Web Programming--1, servlet__ algorithm

Source: Internet
Author: User
Tags java web

1, a servlet is a Java applet that runs on a Web server. The servlet will receive and respond to requests from the Web client and use HTTP to peer.

(A servlet is a small Java program that runs within A WEB server. Servlets receive and respond to requests from WEB clients, usually across HTTP, the hypertext Transfer.)

2. As the core class for all Web applications, the servlet is the only class that can directly process and respond to user requests or delegate processing to other parts of the application.

4. Currently, the only servlet protocol supported by Java EE is: HTTP.

3, all of the servlet must implement Javax.servlet.Servlet interface, modify the interface contains the following methods:


4, in most cases, the Servlet, has inherited the Javax.servlet.GenericServlet. Genericservlet extends Java.lang.Object implements Servlet, ServletConfig, java.io.Serializable. Genericservlet contains only one abstract method: Service ().

5, Javax.servlet.Servlet and Javax.servlet.GenericServlet are not dependent on specific agreements.

6, as the corresponding HTTP request Java.servlet.http.HttpServlet, inherited the Genericservlet. and implements a service method that accepts only HTTP requests. and provides an empty implementation of methods for each of the HTTP method types (GET, post, head, PUT, delete, options, trace). The servlet we use always inherits HttpServlet. When we inherit, but do not rewrite the methods, our servlet can accept HTTP requests, but returns an error 405 Method not allowed (WARNING: Any methods that are not overridden return the error. )

7. Configure servlet:

1) deployed in Web.xml:

<servlet>

<servlet-name>testServlet</servlet-name>

<servlet-class>com.howie.TestServlet</servlet-class>

<loda-on-startup>1</loda-on-startup> <!--start sequence-->

<init-param>

<param-name></param-name>

<param-value></param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>testServlet</servlet-name>

<url-pattern>/test</url-pattern>

</servlet-mapping>

---The reason why URLs are not mapped directly to the Servlet class: the same logic, but links to different databases, and so on.

2) directly through the annotation

@WebServlet (

Name = "Testservlet",

Urlpatterns = {"/test", "/other"},

Loadonstartup=1,

InitParams ={

@WebInitParam (name= "", value= "")

@WebInitParam (name= "", value= "")

}

8. The service method of the Servlet class will handle all incoming requests, but in practice, the service implementation is very complex, and as the Web container is different, the implementation method will vary. The advantage of extending httpservlet is that we don't need to focus on these details. The only requirement is the Doget method or other methods that need to be used.

9. HttpServletRequest: Provides a number of ways to obtain detailed information about HTTP requests. There are two different forms of the request parameter: The query parameter, the request body encoded in application/x-www-form-urlencoded or Multipart/form-data.

It is to be noted that:

The first time the GetParameter, Getparametermap, Getparameternames, getparametervalues method of the Request object is invoked, the Web container determines whether the request contains a post variable, if it is included, It will read the requested inputs he ream and parse the post variables. The requested InputStream can only be read once, and the illegalstateexception exception is triggered when you try to get it again.

10, HttpServletResponse:

Note: 1 Do not use both the Getoutputstream and Getwriter methods for the same response object, otherwise the illegalstateexception exception will be triggered.

2 If you want to set the encoding format for the response content, you need to set the setContentType and Setcharacterencoding methods before getwriter.

11. Initialize Context Parameters:

1 in the configuration file by using the deployment descriptor in Web.xml

<context-param>

<param-name>settingContext</param-name>

<param-value>OOO</param-value>

</context-param>

You can get to the context by Getservletcontext () at this time. Then get an initialized value by Getinitparameter ("Settingcontext").

2 If you do not use the deployment descriptor configuration, you can set the Setinitparameter through the Servletcontent However, it should be noted that this method can only be used in the Contextnitialized method of Javax.servlet.ServletContextListener or Javax.servlet.ServletContainerInitializer Onsta Called in the Rtup method.

3 Initialize the context only for a sevlet, using the <init-param> or InitParams @WebInitParam.

If you have more interest in the Tomcat servlet, you can view the api:http://tomcat.apache.org/tomcat-5.5-doc/servletapi/index.html

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.