The JSP page cannot obtain the value saved by the session in the servlet.

Source: Internet
Author: User

Login. jsp

Code:
  1. <Body>
  2. <%
  3. String [] userinfo = {"",""};
  4. Cookie [] cookie = request. getcookies ();
  5. If (cookie! = NULL ){
  6. For (INT I = 0; I <cookie. length; I ++ ){
  7. If (cookie [I]. getname (). Equals ("logininfo ")){
  8. Userinfo = cookie [I]. getvalue (). Split ("#");
  9. }
  10. }
  11. }
  12. %>
  13. <Form action = "Servlet/login" method = "Post">
  14. Account: <input type = "text" class = "TXT" name = "username" value = <% = userinfo [0] %>/> <br/>
  15. Password: <input type = "password" class = "TXT" name = "password" value = <% = userinfo [1] %>/> <br/>
  16. Allow cookie writing: <input type = "radio" name = "agree" value = "yes" checked/> Yes <input type = "radio" name = "agree" value = "no"/> no <br/>
  17. <Input type = "Submit" value = "Submit"/> <input type = "reset" value = "cancel"/>
  18. </Form>
  19. </Body>

Dopost method in Servlet

If response. sendredirect (request. getcontextpath () + "/login. jsp") is used in the dopost method, null is always obtained in result. jsp.

That is to say, the value in the session is never obtained, and the solution has not been found online for a long time.

Code:
  1. Public void dopost (httpservletrequest request, httpservletresponse response)
  2. Throws servletexception, ioexception {
  3. Response. setcontenttype ("text/html ");
  4. Request. setcharacterencoding ("UTF-8 ");
  5. Response. setcharacterencoding ("UTF-8 ");
  6. String username = request. getparameter ("username ");
  7. String Password = request. getparameter ("password ");
  8. String agree = request. getparameter ("agree ");
  9. User u = new user ();
  10. U. setname (username );
  11. U. setpassword (password );
  12. Httpsession session = NULL;
  13. If (U. canpass ()){
  14. If (agree. Equals ("yes ")){
  15. Cookie mycookie = new cookie ("logininfo", username + "#" + password );
  16. Mycookie. setmaxage (60*60*24*7 );
  17. Response. addcookie (mycookie );
  18. Session = request. getsession ();
  19. Session. setattribute ("user", (User) U );
  20. }
  21. Servletcontext SC = getservletcontext ();
  22. Requestdispatcher RD = NULL;
  23. RD = SC. getrequestdispatcher ("/result. jsp"); // targeted page
  24. Rd. Forward (request, response );
  25. } Else {
  26. Response. sendredirect (request. getcontextpath () + "/login. jsp ");
  27. Return;
  28. }
  29. }

Result page

Code:
  1. <Body>
  2. <%
  3. User u = (User) Session. getattribute ("user ");
  4. System. Out. println (U );
  5. If (u = NULL ){
  6. U = new user ();
  7. U. setname ("");
  8. U. setpassword ("");
  9. }
  10. %>
  11. Current Login User information: Account (<% = U. getname () %>) password (<% = U. GetPassword () %>)
  12. </Body>

 

Related Article

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.