Javaweb Study Summary Fourth chapter--servlet Development

Source: Internet
Author: User

servlet Development

The user enters a URL in the browser and returns, and the browser sends an HTTP request to the server. The server-side program accepts the request, processes the request, and then sends a response. The browser receives a response and then displays the content of the response. This request-response pattern is a typical Web application access process.

All request-response in the Javaweb application is done by the servlet. The servlet is the core program of Javaweb, and all URLs are eventually handed to the servlet for processing. The servlet does not have an execution method like main. When a user accesses a server, Tomcat accomplishes the entire process by invoking certain methods of the servlet.

What is a servlet?

A servlet program is a Java class that implements a special interface that is invoked and started by a Web server that supports Servlets. A servlet program is responsible for processing access requests for one or a set of URL addresses that it corresponds to, and receiving access request information from clients and generating response content.

The servlet program can accomplish most of the tasks that ordinary Java programs can accomplish:

    • Gets the data that the client submits through the form form of the HTML and the parameter information after the URL
    • Create response information content to the client
    • Accessing the server-side file system
    • Connect to databases and develop database-based applications
    • Calling other Java classes
servlet Work Flow

The browser submits a request that follows the text of the HTTP protocol. This text is received and parsed by the server, Tomcat, and is encapsulated as a request object of type HttpServletRequest. All HTTP header data can be queried in the appropriate way by request. Tomcat also encapsulates the output stream as a response object of type HttpServletResponse, and the output can be controlled by setting the response object. In this process of request-response, Tomcat calls the request object, the response object as a parameter, and invokes the corresponding method of the servlet, such as Dopost (Request,response), Doget (Request,response) such as

Servlet Interface

A servlet is a class that implements the Javax.servlet.Servlet interface. The servlet interface specifies a specific method to handle a particular request. The servlet specification is based on the HTTP specification. The HTTP 1.1 specification supports options, GET, POST, HEAD, PUT, DELETE, Trace, and other 7 access methods, the most common of which is GET and POST.

GET: Represents the query information, the URL can be accompanied by a small amount of parameter information, but the total URL length must not exceed 255 characters, and the parameters will be displayed in the browser address bar.

POST: Represents the submission information, generally used to submit big data information or files, submitted content is not limited by the length, and does not appear in the browser address bar.

Firstservlet

With the knowledge of the servlet basics, we can try to write a servlet program. First, we need to get to know the common ways of Servlets.

1 Importjavax.servlet.*;2 Importjava.io.IOException;3 4 /**5 * Created by Administrator on 2015/6/2.6  */7 8 /*9 * View methods in the Servlet interfaceTen  */ One  Public classAservletImplementsServlet { A  - @Override -      Public voiddestroy () { theSystem.out.println ("Destroy () ...."); -     } -  - @Override +      PublicServletConfig Getservletconfig () { -System.out.println ("Getservletconfig () ...."); +         return NULL; A     } at  - @Override -      PublicString Getservletinfo () { -System.out.println ("Getservletinfo () ...."); -         return NULL; -     } in  - @Override to      Public voidInit (ServletConfig servletconfig)throwsservletexception{ +System.out.println ("init () ....")); -     } the  * @Override $      Public voidService (servletrequest request,servletresponse response)Panax Notoginseng             throwsservletexception,ioexception { -SYSTEM.OUT.PRINTLN ("Service () ...."); the     } +}

Note :The life cycle approach of the servlet

Next, we write a simple servlet program and try to compile the run.
1 Importjavax.servlet.ServletException;2 ImportJavax.servlet.http.HttpServlet;3 Importjavax.servlet.http.HttpServletRequest;4 ImportJavax.servlet.http.HttpServletResponse;5 Importjava.io.IOException;6 ImportJava.io.PrintWriter;7 8 /**9 * Created by Administrator on 2015/6/2.Ten  */ One  Public classFirstservletextendshttpservlet{ A  -      Public voidService (httpservletrequest request,httpservletresponse response) -         throwsservletexception,ioexception{ the  -PrintWriter out =Response.getwriter (); -Out.println ("); -Out.println ("<font size=30 color=red>www.baidu.com</font><br/>"); +Out.println ("<marquee>" +NewJava.util.Date () + "</marquee>"); -Out.println ("); +     } A}

Configuration <servlet>

We just don't have the servlet class file, because we want the Web server to know how the browser accesses the servlet, that is, to configure the servlet's class files and access methods. This configuration is done in the Web application's description file, XML, and is configured as follows:

1<?xml version= "1.0" encoding= "UTF-8"?>2<web-app xmlns= "Http://xmlns.jcp.org/xml/ns/javaee"3Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"5version= "3.1" >6 7<servlet>8<servlet-name>xxx</servlet-name>9<servlet-class>aservlet</servlet-class>Ten</servlet> One  A<servlet-mapping> -<servlet-name>xxx</servlet-name> -<url-pattern>/HelloServlet</url-pattern> the</servlet-mapping> -  -<servlet> -<servlet-name>yyy</servlet-name> +<servlet-class>firstservlet</servlet-class> -</servlet> +  A<servlet-mapping> at<servlet-name>yyy</servlet-name> -<url-pattern>/FirstServlet</url-pattern> -</servlet-mapping> -</web-app>

<servlet> and </servlet> are the start and end tags for the servlet configuration, respectively. The middle part is the configuration information for a servlet. Where the <servlet-name> and <servlet-class> attributes are the name and class name of the servlet, respectively, they must be configured. <servlet-name> can be any string, but it must be guaranteed to be the only name in Web. Xml.

1 <init-param>2        <param-name>message</param-name>3        < Param-value>welcome to firstservlet</param-value>4 </init-param>5 < Load-on-startup>1</load-on-startup>

Use the <init-param> tag to configure an initialization parameter, including a parameter name and a parameter value. A servlet can configure multiple initialization parameters. The servlet can use Method Getservletcontext (). Getinitparam (String paramname) to obtain the configured initialization parameter values.

Tags <load-on-startup> Configure how the servlet is loaded, with an optional value of 0 and 1. If 1,TOMCAT is configured to load the servlet at boot time, Tomcat will not load the servlet the first time it is requested.

Compiling run servlet

Javaweb Study Summary Fourth chapter--servlet Development

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.