Servlet request forwarding and redirection

Request forwarding

The browser request is issued only once, in response to receipt of a
forward request to the resource 2 may directly request the data carried in the
request path 3. The browser address bar is submitted by the user
4 only Jump to resources currently applied
servlet1

		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		System.out.println(name);
		System.out.println(age);
		request.getRequestDispatcher("other").forward(request, response);

servlet2

		String name=request.getParameter("name");
		String age = request.getParameter("age");
		System.out.println("name="+name+" age="+age);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write(name+age+"this is pages");

Redirect

The browser request is issued twice, received two response
2. resources can not be redirected to the user submits a request directly to the data
request path 3. The browser address bar is redirected, rather than user submits a request route of. You can prevent duplicate submission form.
4. Redirect can jump to other resources not only the current application, you can also jump to the resources of other applications.
5. Redirect servers can reduce resource consumption.
one servlet

request.setCharacterEncoding("utf-8");
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		System.out.println(name);
		System.out.println(age);
		name = URLEncoder.encode(name, "utf-8");//解决重定向时的乱码问题:编码
		response.sendredircect("other");

other Servet

		String name=request.getParameter("name");
		String age = request.getParameter("age");
		name = URLDecoder.decode(name, "utf-8");//解码
		System.out.println("name="+name+" age="+age);
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write(name+age+"this is pages");
Published 114 original articles · won praise 8 · views 5489

Guess you like

Origin blog.csdn.net/OVO_LQ_Start/article/details/104730570