Request and response differences in Java

Source: Internet
Author: User

1, response

belongs to the redirect request;

The URL of its address bar will change;

Two requests are sent to the server;

2. Request

belongs to request forwarding;

The URL of its address bar does not change;

Send a request to the server;

Give a simple example of distinguishing them:

A to B to borrow money:

The first type: with response. b no money, the request failed, but B told A,c Rich. So a again to C borrow money, C lent a, request success.

The second type: use Request. B had no money, but B borrowed money from C and then gave a, and the request succeeded. This time a only sent a request, he did not know that the borrowed money is C.

This is done using the response method:

Response.sendredirect ();

Using the Resquest method:

Request.setattribute ("Key", "value");

Request.getrequestdispatcher ("index.jsp"). Forward (Request,response);

The parameters passed by SetAttribute here can only be received by Request.getattribute (). The return value of the Request.getattribute () method is an object type, and you should pay attention to the type conversion when used.

Write a sample code:

JSP code
  1. <%@ page language="java" import="java.util.*" pageencoding= "UTF-8"%>
  2. <! DOCTYPE HTML public "-//W3C//DTD HTML 4.01 transitional//en" >
  3. <base href="<%=basePath%>" >
  4. <title> Landing Page </title>
  5. <body>
  6. <%
  7. String errorCode = (string) request.getattribute ("error");//request.getparameter ("error");
  8. if (ErrorCode! = NULL &&!) Equals ("error") && "ErrorCode" (equals) {
  9. %>
  10. "color:red" > Username or password Error!

  11. <%
  12. }
  13. %>
  14. <form action="login.jsp" method="POST" >
  15. <p> Username: <input type="text" name = "UserName"/><br/></p>
  16. <p> Secret &nbsp;&nbsp; Code: <input type="password" name ="Userpwd"/><br/></p>
  17. <p><input type = "Submit" value = "Login"/><br/></p>
  18. </form>
  19. <a href="reg.jsp" > Register new users </a>
  20. </body>

JSP code
  1. <%@ page language="java" import="java.util.*" pageencoding= "UTF-8"%>
  2. <! DOCTYPE HTML public "-//W3C//DTD HTML 4.01 transitional//en" >
  3. <title> Landing Page </title>
  4. <body>
  5. <%
  6. String name = Request.getparameter ("UserName");
  7. String pwd = Request.getparameter ("userpwd");
  8. if ("Shamuu". Equals (name) && "123". Equals (pwd)) {
  9. %>
  10. "color:red;" > Welcome you! <%=name%>

  11. <%
  12. }else{
  13. Response.sendredirect ("index.jsp?error=01");
  14. Request.setattribute ("error","01");
  15. Request.getrequestdispatcher ("index.jsp"). Forward (Request,response);
  16. }
  17. %>
  18. </body>

Request and response differences in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.