Servlet introduction

Servlet Technology

a. What is a Servlet

1.servlet is one of the Java EE specifications, and the specification is the interface

2.Servlet is one of the three major components of Java Web. The three major components are servlet program, Filter and Listener listener

3.Servlet is a java applet running on the server. It can receive requests from the client and respond to data to the client


b. Manually implement the Servlet program

1. Write a class to implement the Servlet interface

public class HelloServlet implements Servlet { 
/*** service Methods are designed to handle requests and responses 
* @param servletRequest 
* @param servletResponse 
* @throws ServletException 
* @throws IOException 
**/
 @Override 
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { 		 
	System.out.println("Hello Servlet Was interviewed"); 
 	} 
 }

2. Implement the service method, process the request and respond to the data

3. Configure the access address of the servlet program in web.xml

The configuration is as follows

The Servlet name tag gives the Servlet program an alias (usually a class name)
The Servlet class tag is the full class name of the Servlet program
The Servlet mapping tag configures the access address for the Servlet program
The Servlet name tag is used to tell the server which Servlet program I currently configure to use
The URL pattern tag configures the access address
/The slash indicates that the address is: http://ip:port/ Engineering path
/Hello means the address is: http://ip:port/ Project path / hello2


url address access to Servlet program



Servlet lifecycle

1. Execute Servlet constructor method

2. Execute init initialization method

The first and second step is to create a Servlet program that will call

3. Execute the service method

Step 3: each access will call

4. Implement the destroy destruction method

Step 4: call when the web project stops


Distribution processing of get and post requests

Implement Servlet program by inheriting HttpServlet

Generally, in the actual project development, the Servlet program is implemented by inheriting the HttpServlet class

1. Write a class to inherit the HttpServlet class

2. Rewrite doGet or doPost methods according to business needs

3. Configure the access address of the Servlet program in web.xml

package com.xrj.servlet;

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

public class HelloServlet2 extends HttpServlet {

    /*
    doGet()Method is called when a get request is made
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("HelloServlet2 of doGet method");
    }
    /*
    doPost()Method is called on a post request
    */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("HelloServlet2 of doPost method");
    }
}


Inheritance system of Servlet class

ServletConfig class

From the perspective of class name, ServletConfig class is the configuration information class of Servlet program

Both the Servlet program and the ServletConfig object are created by Tomcat and used by us

By default, Servlet programs are created during the first access. ServletConfig is a corresponding ServletConfig object created when each Servlet program is created


Three functions of ServletConfig class:

1. You can get the value of the alias Servlet name of the Servlet program

2. Get the initialization parameter init param

3. Get ServletContext object



ServletContext class

What is ServletContext?

1.ServletContext is an interface that represents a Servlet context object

2. A web project has only one ServletContext object instance

3.ServletContext object is a domain object

4.ServletContext is created when the web project deployment starts and destroyed when the web project stops

Domain object: an object that can access data like a map. The domain here refers to the operation range of accessing data

The domain here refers to the operation range of accessing data, the whole web project

Save dataFetch dataDelete data
mapput()get()remove()
Domain objectsetAttribute()getAttribute()removeAttribute()

Four functions of ServletContext class?

1. Get the context parameter context param configured in web.xml

2. Get the current project path, format: / Project path

3. Obtain the absolute path on the server disk after project deployment

package com.xrj.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;

public class ContextServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //1. Get the context parameter context param configured in web.xml
        ServletContext context=getServletConfig().getServletContext();

        String username=context.getInitParameter("username");
        System.out.println("context-param parameter username The values are:"+username);
        String password=context.getInitParameter("password");
        System.out.println("context-param parameter password The values are:"+password);

        //2. Get the current project path, format: / Project path
        System.out.println("Current project path:"+context.getContextPath());
        //3. Obtain the absolute path on the porcelain disk of the server after project deployment
        /*
            / The slash is resolved by the server to: http://ip Address: port / project name /, mapped to the web directory of idea code
         */
        System.out.println("The project deployment path is:"+context.getRealPath("/"));//web directory
        //4. Store data like map
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

web.xml configuration:

4. Store data like map



http protocol

http protocol

The so-called http protocol refers to the rules that need to be observed when transmitting data between the client and the server

The data in http is called message


Requested http protocol format

The client sends a data call request to the server

The server sends back data to the client to call the response

Requests are divided into get requests and post requests

1.get request

1. Request line

(1) The method of the request is GET

(2) Requested resource path [+? + request parameters]

(3) Requested protocol version number HTTP/1.1

2. Request header

Key: value forms different key value pairs, indicating different meanings

What are the get requests?

1.form tag method=get

2.a label

3.link tags are introduced into css

4.Script tag import js file

5.img label introduction picture

6.iframe introduces html page

7. Enter the address in the browser address bar and press enter


2.post request

1. Request line

(1) Request mode: Post

(2) Requested resource path [+? + request parameters]

(3) Requested protocol version number HTTP/1.1

2. Request header

Key: value forms different key value pairs, indicating different meanings

Blank line

3. The request body = = > > > is the data sent to the server

What are the POST requests?

8.form label method=post


http protocol format of response

1. Request line

(1) Response protocol and version number http/1.1

(2) Response status code 200

(3) Response status descriptor OK

2. Response head

Key: value forms different key value pairs, indicating different meanings

Blank line

3. The response body - > > > is the data returned to the client


Description of common response codes

200 indicates that the request was successful

302 indicates request redirection

404 indicates that the request server has received it, but the data you want does not exist (the request address is wrong)

500 indicates that the server has received the request, but the server has an internal error (code error)

Keywords: Java JavaEE server

Added by holowugz on Thu, 09 Dec 2021 02:20:42 +0200