The problem of ID inconsistency in Java session Save and get verification code get

Source: Internet
Author: User
Tags session id

A few months ago because I learned a part of Java EE, so recently prepared to write a small project, so I ran into some problems for a Java rookie, I need to save

What kind of problem, that is, I added a verification code in the registration and is Chinese, four word idiom verification code,

First use Java to generate verification code and verification Code picture, call the verification Code interface, the verification code using the session to save the client, and send pictures to the front-end display,

Then click on the registration, the front-end to pass the user input verification code to the background, the background to obtain the user input verification code and use session to get saved in the client's verification code comparison, correct or not

But!!!!!!!!!!!!!!!!!!!!!!! But, but, but

Save Session Code

Request.getsession (). SetAttribute ("code_session", word);

Get Session Code

String usercode_session = (string) request.getsession (). getattribute ("Code_session");

I saved, get the time to get is empty, code no problem, for my Java rookie to find backstage colleagues to help me, but others also depressed, others generally use the framework, the bottom of the forgotten, but said code no problem AH

My colleague suggested I use cookies to save, but the cookie should not be safe, the front-end JS can be obtained, but I change is ready to try, first the function completed

Results!!!!!!!!!!!!!!!!!!!!!!! Page News 500

The original cookie does not support Chinese preservation, I remember the front-end seemingly can save Chinese, backstage incredibly not,

Forget it, I'll save it with the session

And then we print the session ID to see

Print the ID code of the session

Request.getsession (). GetId ()

Found two times ID is not an ID, the ID of the saved session has not changed, but the acquisition has been changing

It took three or four hours, I found the cause of the problem, the original people say there is a cross-domain problem

So the solution

1. Add a cross-domain to the front-end Ajax

$.ajax ({    ' POST ',    url:url,    data:data,    dataType:' json ',    success: Callback,    error:function(e) {      console.log (e)    },    xhrfields: {        True    }  );

2. Background code (added in this interface)

        Response.setheader ("Access-control-allow-origin", Request.getheader ("Origin"));

It's settled, flattered.

The problem of ID inconsistency in Java session Save and get verification code get

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.