Java Web knowledge essential
web concept review
- Software architecture
- C/S: client / server
- B/S: Browser / server
- Resource classification
- Static resource: all users get the same result after accessing, which is called static resource. Static resources can be directly parsed by the browser, such as html, css and JavaScript
- Dynamic resources: each user may get different results after accessing the same resources. It is called dynamic resource. After the dynamic resources are accessed, they need to be converted to static resources, such as servlet, jsp and php
- Three elements of network communication
- IP
- Port number
- transport protocol
web server software
- Server: the computer on which the server software is installed
- Server software: receive the user's request, process the request and respond
- web server software: receive the user's request, process the request and respond
- In web server software, web projects can be deployed to allow users to access these projects through a browser
- web container
- Common java related web server software
- Tomcat: Apache Foundation
- Tomact file
- Tomact startup:
- bin/startup.bat, double-click to run the file
- Access: Browser input: http: / / 127.0.0.1:8080enter
- to configure:
- Just put the project under webapps
- Simple deployment: package the project into a war package, and then place the war package in the webapps directory. The war package will be decompressed automatically
- Tomact file
- Tomcat: Apache Foundation
Servlet: server applet
- Concept: applet running on the server
- Servlet is an interface that defines the rules for JAVA classes to be accessed by the browser (recognized by tomact)
- In the future, we will customize a class to implement Servlet interface and replication method
- quick get start
- Create JAVA EE project
- Define a class to implement Servlet
- Implementing abstract methods in interfaces
- Configure Servlet (in web.xml)
HTTP:
Concept: Hyper Text Transfer Protocol
*Transmission protocol: defines the format of data sent when the client and server communicate
*Features:
1. Advanced protocol based on TCP/IP
2. Default port number: 80
3. Based on request response model: one request and one response
4. Stateless: each request is independent of each other and cannot interact with data
Request message data format
1. Request line
Request mode request URL request protocol / version
GET /login.html HTTP/1.1
*Request method:
*HTTP protocol has seven request modes, two of which are commonly used
* GET:
1. The request parameter is in the request line, after the URL
2. The length of the requested URL is limited
* POST:
1. The request parameter is in the request body
2. The length of the requested URL is unlimited
2. Request header: the client browser tells the server to access the information of the browser used
3. Request blank line: used to split the request header and request body of a POST request
4. Request body: the request parameter that encapsulates the POST request information
Request :
-
- The Request object and the reply object are created by the server, and we use them
- The Request object is used to get the Request message, and the Response object is used to set the Response message
- Request function
1. Get request line data
2. Get request header data
3. Get request body data
Request line
String method = req.getMethod();//Get request method System.out.println(method); String contextPath = req.getContextPath();//Get virtual path System.out.println(contextPath); String servletPath = req.getServletPath();//Get Servlet path System.out.println(servletPath); String queryString = req.getQueryString();//Get request parameters System.out.println(queryString); String requestURI = req.getRequestURI();//Gets the URI of the request System.out.println(requestURI); String protocol = req.getProtocol();//Get protocol version System.out.println(protocol); String remoteAddr = req.getRemoteAddr();//Get the ip address of the client System.out.println(remoteAddr);
Request header
public class RequestDemo04 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //Get header String header = req.getHeader("user-agent"); if(header.contains("Chrome")) { System.out.println("Chrome"); } else if(header.contains("Firefox")) { System.out.println("Firefox"); } }
Request body
- Only the POST mode has a request body, which encapsulates the request parameters of the POST request
- step
- Get stream object
- Then get the data from the stream object
BeanUtils tool class to simplify data encapsulation
- JAVABean: standard JAVA class
- requirement
- Class must be public decorated
- Constructor that must provide null arguments
- Member variables must be decorated with private
- Provide public setter s and Getters
- Function: encapsulate data
- Properties, not member variables, are manipulated
Continuous update~~~
- requirement