Java Web request common API-3

Insert picture description here

Request

Get request line data

GET localhost:8080/servletDemo/demo1?id=3 HTTP/1.1

The above code is a simple URL path , that is, the request line, which is sent to the specified background through the request, and the parameters are obtained through the request object

  1. Get request method: GET
    String getMethod()
  2. Get the virtual directory: /servletDemo
    String getContextPath()
  3. Get the Servlet path:/demo1
    String getServletPath()
  4. Get request parameters in get mode: id=3
    String getQueryString()
  5. Get request URI:/servletDemo/demo1
    String getRequestURI()
  6. Get the full request path:/http://localhost:8080/serveltDemo/demo1
    String getRequestURL()
  7. Obtain the agreement and version:/HTTP/1.1
    String getProtocol()
  8. Obtain the client's IP address:192.168.0.0
    String getRemoteAddr()

Get request header

Look at the request header information sent by the browser, as shown in the example belowF12operating…

Insert picture description here

  1. Get request header data, get value by request header name
    String getHeader(String name)
  2. Get all request header information
    Enumeration<String> getHeaderNames()
// 示例
protected void doGet(HttpServletRequest request,HttpServletResponse response){
	Enumeration<String> headerNames = request.getHeaderNames();
	while(headerNames.hasMoreElements()){
		// 获取请求头名称
		String name = headerNames.nextElement();
		// 获取请求头名称对应的参数
		String value = request.getHeader(name);
		System.out.println(name+"---"+value);
	}
}

Insert picture description here

Get request body

Only the POST request method has a request body. The request body encapsulates the request parameters of the POST request. The flow object is first obtained, and the parameters are taken from the flow object.

  1. Get stream object-get character input stream, can only manipulate character data
    BufferedReader getReader()
  2. Get stream object-get byte input stream, can operate on both bytes and characters
    ServletInputStream getInputStream()

Write an input box
Insert picture description here

Code behind

// 示例
protected void doPost(HttpServletRequest request,HttpServletResponse response){
	BufferedReader reader = request.getReader();
	String line = null;
	while((line = reader.getLine()) != null){
		System.out.println(line);
	}
}

Console printing will put the parameters on one line
Insert picture description here

Other general functions

It is also some commonly used APIYou can get it regardless of get / post

  1. General way to get request parameters (get parameter value according to parameter name) username=zhangsan
    String getParameter(String name)
  2. Get request parameter name Get array of parameter values hobby=xx&hobby=game
    String[] getParameters(String name)
  3. Get request parameter name (similar to get request header method)
    Enumeration<String> getParameterNames()
  4. Get a Map collection of all parameters
    Map<String,String> getParameterMap()

Code behind

// 示例
protected void doPost(HttpServletRequest request,HttpServletResponse response){
	Map<String,String> map = request.getParameterMap();
	Set<String,String> set = map.keySet();
	for(String name:set){
		System.out.println(name);
		String value = request.getParameter(name);
		System.out.println(value);
		
		System.out.println("---------")
	}
}

Insert picture description here
The result is the same. . . The purpose is the same, but there are some differences in writing

Chinese garbled problem, Tomcat8 has solved the garbled problem of GET request method, but the Chinese garbled problem of POST method still exists, you need to configure it yourself
before obtaining parameters

request.setCharacterEncoding("UTF-8");

Forward

A resource jump method inside the server, which can berequest,responseForward to other corresponding Servlet

step:

  • Get the request forwarding server through the request object:
    RequestDispatcher getRequestDispatcher(String path);
  • Use == RequestDispatcher object to forward
    forward(ServlertRequest request,ServletResponse response);

Features:

  • The browser address has not changed
  • Can only access the internal resources of the current server
  • Forward is a request

Share data

Domain object: an object with scope, which can share data within
the scope

method:

  • Storing data
    setAttribute(String name,Object obj)
  • Get value by key
    Object getAttribute(String name);
  • Remove the value corresponding to the key by key
    removeAttribute(String name)
@WebServlet("/requestDemo01")
public class RequestDemo01 extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest request,HttpServletResponse response){
		request.setAttribute("name","Zhan San");
		request.getRequestDispatcher("/requestDemo02").forword(request,response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request,HttpServletResponse response){
		this.doGet(request,response);
	}
}
@WebServlet("/requestDemo02")
public class RequestDemo02 extends HttpServlet{
	
	@Override
	protected void doGet(HttpServletRequest request,HttpServletResponse response){
		String name = (String)request.getAttribute("name","Zhan San");
		Systme.out.println(name);
	}
	
	@Override
	protected void doPost(HttpServletRequest request,HttpServletResponse response){
		this.doGet(request,response);
	}
}

Steps:

  • access /requestDemo01 Set up shared data and forward to /requestDemo02 In resources
  • enter /requestDemo02 Remove the shared data from the resource and print
  • The browser initiates a request like a server, and the server internally forwards it, which is equivalent to performing an operation between two resources
Published 24 original articles · praised 33 · visits 2391

Guess you like

Origin blog.csdn.net/weixin_41241629/article/details/104625628
Recommended