Servlet practice

Servlet practice

A, Servlet simple understanding

1. What is Servlet

Java Servlet is a program running on the Web server or application server, which is a database or between applications on the server and HTTP request from the Web browser or other HTTP clients intermediate layer . Use Servlet, we can gather user input from web forms, showing records from a database or other source, you can also create dynamic web pages.

2, Servelet framework

Reference and W3C, the position of the servlet in a web application

img

3, Servlet main task

  • Read client (browser) of the transmitted data explicit. Form HTTP client program that includes HTML form on a web page, or may be derived from or customized applet.
  • Read client (browser) sends implicit HTTP request data. This includes cookies, media types and browsers can understand compression format, and so on.
  • Processing the data and generate a result. This process may need to access the database, the implementation of RMI or CORBA calls, call the Web service, or directly calculated corresponding response.
  • Transmitting the explicit data (i.e., document) to the client (browser). The format of the document can be varied, including text files (HTML or XML), binary files (GIF image), Excel and so on.
  • Sending an implicit HTTP response to the client (browser). This includes tells the browser or other client is returned the document type (eg HTML), set cookies and cache parameters, and other similar tasks.

4, Servlet relates to packet

Servlet can use javax.servlet and javax.servlet.http create packages

Two, Servlet life cycle

Servlet life cycle can be defined as the entire process from creation to destruction.

The following is the procedure to follow Servlet:

(1)、Servlet通过调用init ()方法进行初始化。
(2)、Servlet调用service()方法来处理客户端的请求。
(3)、Servlet通过调用destroy()方法终止(结束)。
(4)、最后Servlet由JVM的垃圾回收器进行垃圾回收的。

1, init () method

The init method is designed to call only once. It is called when you first create a Servlet, it is no longer called upon each subsequent user request. I know it can be.

Look at the definition:

public void init() throws ServletException {
    //初始化代码
}

2, service () method

The method of performing the actual service is the main task for handling a request from the client, and to write the formatted response back to the client. It calls doGet, doPost, doPut, doDelete methods at the appropriate time.

Look at the definition:

public void service(ServletRequest request, ServletResponse response) throws ServletException,IOException{
}

Mentioned above would go some way to call, so we do not do anything to service () method, you only need to override the doGet () or doPost () according to the type of request from the client.

①doGet () method:

public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    // Servlet 代码
}

doGet request from a normal URL request, or unspecified METHOD HTML form.

②doPost () method:

public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
    // Servlet 代码
}

doPost a special request from the HTML form specifies the METHOD of Post

3, destroy () method

destroy () method is invoked only once, to be called at the end of the Servlet life cycle. General Servlet will close the database connection, stop the background thread, list, or click on the Cookie counter is written to disk, and perform other similar clean-up activities.

Look at the definition:

public void destroy() {
    // 终止化代码...
}

Three, Servlet simple example

To a most original Hello World Servlet output

EDITORIAL I use IDEA to create a web project, when writing Servlet may not javax.servlet and javax.servlet.http packages, how to do it? Solved, File-> Project Structure-> Library-> will be above a java, New Project Library appears after clicking, we select java, then find lib \ servlet-api.jar import it to our tomcat directory.

img

The start code patterns!

package wzm;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {
    private String message;

    @Override
    public void init() throws ServletException {
        message="Hello World";
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{
        //设置响应内容
        response.setContentType("text.html");
        //实际逻辑
        PrintWriter out = response.getWriter();
        out.println("<h1>" + message + "</h1>");
    }

    @Override
    public void destroy() {
        //nothing
    }
}

Here are just familiar cycle again Servlet

Four, IDEA deployment Servlet

1, Method a: web.xml

Add the following web-app tag in web.xml file under WEB-INF directory:

<servlet>  
    <servlet-name>HelloWorld</servlet-name>  
    <servlet-class>wzm.HelloWorld</servlet-class>  
</servlet>  
  
<servlet-mapping>  
    <servlet-name>HelloWorld</servlet-name>  
    <url-pattern>/HelloWorld</url-pattern>  
</servlet-mapping> 

2, two methods: Method annotations

In front of the file with the HelloWorld class: @WebServlet ( "/ HelloWorld")


Servelet should remain the core web, where you know roughly how it works, and know some of the ways to use the service just fine.

Guess you like

Origin www.cnblogs.com/wangzheming35/p/11881559.html