[Servlet] crazy God's notes

Basic concepts of Web

Static web

The data provided by html and css to everyone will never change

Dynamic web

The information provided to everyone will change. Everyone will see different information at different times and places; For example, Taobao; The technology stack includes servlet, jsp, asp and php

advantage:
web pages can be updated dynamically, and all users don't see the same page
Can interact with database (data persistence)

web server explanation

php

php development is fast, powerful, cross platform and simple code
Unable to carry large traffic (limitations)

JSP,Servlet

The BS architecture mainly promoted by sun company is developed based on java language and can bear the impact of the three high problems

BS architecture

Browser and server

CS architecture

Client and server

web server

Server is a passive operation, which is used to process some user requests and give users some response information

Tomcat

A free and open source lightweight application server is highly used in small systems and few concurrent users. It can run JSP pages and servlets

Http details

HTTP (Hypertext Transfer Protocol) is a simple request response protocol, which usually runs on top of TCP
Text: html, string...
Hypertext: picture, music, location, video, map...

Http request

Client - Request - server

Request URL:https://www.baidu.com/ request address
Request Method:GET  get Methods post method
Status Code:200 OK  Status code 200
Remote Address:14.215.177.39:443  Remote address

Request line

Request method in request line: GET
Request method: Get, Post...

get request

The request can carry fewer parameters and is limited in size. The data content will be displayed in the URL address bar of the browser, which is unsafe but efficient

post request

The parameters that can be carried by the request are not limited, the size is not limited, and the data content will not be displayed in the URL address bar of the browser. It is safe, but not efficient

Message header

Accept: Tell the browser what data types it supports
Accept-Encoding: Which encoding format is supported  GBK UTF-8 GB2312
Accept-Language: Tell the browser, locale
Cache-Control: Cache control
Connection: Tell the browser whether to disconnect or remain connected when the request is completed

Http response

Server response client

Cache-Control:private  Cache control
Connection:keep-alive  connect
Content-Encoding:gzip  code
Content-Type:text/html;charset=utf-8  type

Responder

Accept: Tell the browser what data types it supports
Accept-Encoding: Which encoding format is supported  GBK UTF-8 GB2312
Accept-Language: Tell the browser, locale
Cache-Control: Cache control
Connection: Tell the browser whether to disconnect or remain connected when the request is completed
Host: host
refrush: Tell the client how often to refresh
Location: Repositioning web pages

Response status code

200: request response succeeded
3 * *: request redirection to another specified path
4 * *: resource not found
5 * *: server code error

HelloWorld

pom dependency

<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>javax.servlet-api</artfactId>
	<version>4.0.1</version>
</dependency>
<dependency>
	<groupId>javax.servlet.jsp</groupId>
	<artifactId>javax.servlet.jsp-api</artfactId>
	<version>2.3.3</version>
</dependency>
public class HelloServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // Type of response: html
        resp.setContentType("text/html");
        // Gets the output stream of the response
        PrintWriter writer = resp.getWriter();
        writer.println("<html>");
        writer.println("<head>");
        writer.println("<title>hello hupf</title>");
        writer.println("</head>");
        writer.println("<body>");
        writer.println("<h1>hello hupf</h1>");
        writer.println("</body>");
        writer.println("</html>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>


  <!--web.xml Is to configure us web Core application of-->
  <!--register Servlet-->
  <servlet>
    <servlet-name>helloServlet</servlet-name>
    <servlet-class>com.hupf.servlet.HelloServlet</servlet-class>
  </servlet>

  <!--One servlet Corresponding to one mapping: mapping-->
  <servlet-mapping>
    <servlet-name>helloServlet</servlet-name>
    <!--Request path-->
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
</web-app>

servlet interface sun company has two default implementation classes: HttpServlet and GenericServlet

Why mapping is needed: we write a JAVA program, but we need to access it through the browser, and the browser needs to connect to the web server, so we need to register the Servlet we write in the web server and give it a path that the browser can access

Servlet principle

The servlet is called by the web server. After receiving the browser request, the web server will call the servlet and call the service method to return the response to the servlet and the client

Mapping

  1. A servlet can specify a mapping path
<servlet>
    <servlet-name>helloServlet</servlet-name>
    <servlet-class>com.hupf.servlet.HelloServlet</servlet-class>
  </servlet>

  <!--One servlet Corresponding to one mapping: mapping-->
  <servlet-mapping>
    <servlet-name>helloServlet</servlet-name>
    <url-pattern>/hello</url-pattern>
  </servlet-mapping>
  1. A servlet can specify multiple mapping paths
<servlet>
    <servlet-name>helloServlet</servlet-name>
    <servlet-class>com.hupf.servlet.HelloServlet</servlet-class>
  </servlet>

  <!--One servlet Corresponding to one mapping: mapping-->
  <servlet-mapping>
    <servlet-name>helloServlet</servlet-name>
    <url-pattern>/hello1</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>helloServlet</servlet-name>
    <url-pattern>/hello2</url-pattern>
  </servlet-mapping>
  1. A servlet can specify a generic mapping path
<servlet-mapping>
    <servlet-name>helloServlet</servlet-name>
    <url-pattern>/hello/*</url-pattern>
  </servlet-mapping>
  1. Default request path
<servlet-mapping>
    <servlet-name>helloServlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
  1. Specify some suffixes or prefixes, etc
<!--*The path of the project mapping cannot be added before the number-->
<servlet-mapping>
    <servlet-name>helloServlet</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

Keywords: Operation & Maintenance server

Added by gloeilamp on Fri, 22 Oct 2021 10:48:43 +0300