Request Request Request Basic Knowledge

The actual behavior of the Tomcat server:

Because: Enter in the browser's address bar: localhost:8080/ProjectName / resource path
 Where the resource path:
	It's the resource path in @WebServlet("/demo3")
1. Through this path, the class pointed by this path can be loaded and created.
2. Then create the Request,Response object, pass in the service method of the object, and execute it.
   Request object encapsulates request data and Response object encapsulates response data.
3. Request can be used to get request data and set Response response data, while browser can get response data through Response.

ServletResquest -> 
	HttpServletResquest -> 
		Resquest Facad (implements HttpServletRequest interface)
			Internal holding of the org.apache.catalina.connector.Request object
			And execute a series of methods for this object

package  package org.apache.coyote 
	:This class does not apply to user code,from tomcat Internal use:final class

package package org.apache.catalina.connector
		Implementing HttpServletRequest interface
		This class holds the org.apache.coyote.Request class object internally.
		And execute a series of methods for this object

Functions:

1. Get the request data:
	1. Get request row data	
	2. Get the request header data 
	3. Getting Request Body Data
 2. Other:
	1. General way of obtaining request parameters
	2. Request forwarding
	3. Sharing data
	4. Get ServletContext

1. Get request row data

Get request row data
	//Request mode, virtual directory, Servlet path, request parameters,
		//Request URL, protocol and version, client ip address

//Give an example:
	//http://localhost:8080/WebServlet/RequestDemo1?age=12

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("Method:"+request.getMethod());
				//	:GET
		System.out.println("Virtual directory:"+request.getContextPath());
				//  :/WebServlet
		System.out.println("Servlet Route:"+request.getServletPath());
				//	:/RequestDemo1
		System.out.println("Request parameters:"+request.getQueryString()); 
				//	Connect the first parameter, & Connect the other parameters //age=12
		System.out.println("URL:"+request.getRequestURL());
				//	:http://localhost:8080/WebServlet/RequestDemo1
		System.out.println("Protocol and Version:"+request.getProtocol());
				//	:HTTP/1.1
		System.out.println("ip address:"+request.getLocalAddr());
				//	ip address: 0:0:0:0:0:0:0:0:1
	}

2. Get the request header data

Get the request header name: request.getHeaderNames()
	host
	connection
	upgrade-insecure-requests
	user-agent
	accept
	dnt
	accept-encoding
	accept-language
	cookie
	referer

//Get the respective request header information: request.getHeader(name)

	String referer = request.getHeader("referer");
	String uagent = request.getHeader("user-agent");
	Cookie[] cookies = request.getCookies();
	System.out.println("referer:"+referer);
	System.out.println("user-agent:"+uagent);
	System.out.print("cookies:");
	for(Cookie c:cookies) {
		System.out.print(c);
		if(cookies[cookies.length-1].equals(c)) {
			break;
		}
		System.out.print(",");
	}

3. Posting method for obtaining requester data is effective

Byte stream: getInputStream();
//Character stream: getReader();

username=eeee&password=sdfea

//Examples: 1
	BufferedReader reader = request.getReader();
	String line = null;
	while((line = reader.readLine())!= null){
		System.out.println(line);
	}	
		
	2
	ServletInputStream inputStream = request.getInputStream();
	StringBuffer sBuffer = new StringBuffer();
	byte[] bs = new byte[4096];
	while(inputStream.read(bs) != -1) {
		sBuffer.append(new String(bs));
	}
	System.out.println(sBuffer.toString());

2. Other

1. General way of obtaining request parameters
String request.getParameter(name);
String[] request.getParameterValues(name);
// Check box checkbox
Map<String, String[]> request.getParameterMap();
// Key-value pairs
Enumeration request.getParameterNames();
// Gets the name and returns a value similar to an iterator

Give an example:
	request.setCharacterEncoding("utf-8");
	Map<String, String[]> parameterMap = request.getParameterMap();
	Set<String> keySet = parameterMap.keySet();
	for (String string : keySet) {
		System.out.print("key: "+string+" values: ");
		String[] strings = parameterMap.get(string);
		for (String string2 : strings) {
			System.out.print(string2);
			if(string2.equals(strings[strings.length-1])) {
				break;
			}
			System.out.print(",");
		}
		System.out.println();
	}

2. Request forwarding

1. The method that the internal resources of the current server can be forwarded and multiple resources can be forwarded at one time without changing the address:

Within the method, you have the HttpServletRequest request, HttpServletResponse response parameter
	request.getRequestDispatcher("/demo3").forward(request, response);

	request.getRequestDispatcher("/index.jsp").forward(request, response);
Among them:
	"/demo3": Another resource path		
	request: request object
	response: response object

3. Shared data (forwarded to other servlet s for data)

The transmitted equivalent is then a key-value pair:
	request.setAttribute("name", "libai");  
	request.getAttribute("name")
	request.removeAttribute(name);

4. Get ServletContext

req.getServletContext()

Keywords: Apache Tomcat encoding JSP

Added by enlight on Wed, 28 Aug 2019 06:39:21 +0300