Summary of the relocated garbled characters of the sendredirect () method in Java httpservletresponse

Source: Internet
Author: User

Recently, the sendredirect () method in httpservletresponse is used to test its Chinese garbled characters.

The test method is to use a simple logon interface login. java (servlet-only), if the login is successful, in logindeal. in Java (servlet-only), the sendredirect () method in httpservletresponse is used for relocation. The purpose of the relocation is also to call a Servlet File mainframe. java:

Login. Java:

Import java. Io. ioexception;
Import java. Io. printwriter;

Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;

Public class login extends httpservlet {

Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {

This. dopost (request, response );
}
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Response. setcontenttype ("text/html; charset = UTF-8 ");
// Response. setcharacterencoding ("UTF-8 ");
Printwriter out = response. getwriter ();
Out. println ("Out. println ("<form action = '/servlettest/logindeal' method = post> ");
Out. println ("User name: <input type = 'text' name = 'username'> <br> ");
Out. println ("password & nbsp code: <input type = 'Password' name = 'pwd'> <br> ");
Out. println ("<input type = 'submit 'value = 'login'> <br> ");
Out. println ("</form> ");

}
}

The logindeal. Java code is as follows:

Import java. Io. ioexception;
Import java. Io. printwriter;

Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;

Import tools. mytools;
Import com. ZY. model .*;

Public class logindeal extends httpservlet {
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {

This. dopost (request, response );
}

Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {

Response. setcontenttype ("text/html"); // pay special attention to this place. No other settings are allowed !!!!! Otherwise, there will be confusing Garbled text !!

Printwriter out = response. getwriter ();

String uname = request. getparameter ("username ");
String username = mytools. getnewcodestring (uname );
String passwd = request. getparameter ("PWD ");

System. Out. println ("uname =" + uname) in logindeal );
System. Out. println ("username =" + username) in logindeal );
System. Out. println ("passwd =" + passwd) in logindeal );

// If the logon is successful, pass the user name and password to mainframe
If ("hello". Equals (username) & "123". Equals (passwd )){
Response. sendredirect ("/servlettest/mainframe? Uname = "+ uname +" & Pwd = "+ passwd); // Traditional Method

} Else {
// If the user name and password are incorrect, jump back to the logon page
Response. sendredirect ("login ");
}
}
}

The mainframe. Java code is as follows:

Package com. ZY. view;
Import java. Io. ioexception;
Import java. Io. printwriter;
Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;
Import javax. servlet. http. httpservletrequest;
Import javax. servlet. http. httpservletresponse;

Import com. Zhengyi. Tools. mytools;
Import com. ZY. model. users;
Public class mainframe extends httpservlet {
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {

This. dopost (request, response );
}

Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Response. setcontenttype ("text/html; charset = UTF-8"); // you can set it here ~~~~

Printwriter out = response. getwriter ();

// Use the traditional method to accept the user name and password
String uname = request. getparameter ("uname ");
String username = mytools. getnewcodestring (uname );
// String uname = (string) request. getattribute ("uname ");

System. Out. println ("mainframe uname:" + uname );
System. Out. println ("username:" + username) in mainframe );

// Display the user name and password in the traditional way
// Out. println ("mainframe uname:" + uname + "<br> ");
Out. println ("username =" + username in mainframe );
// Out. println ("Password =" + password );
}

}

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.