最近httpservletresponse 中的sendRedirect()的方法,测试其中文乱码。
测试方法是用一个简单的登录界面Login.java(纯servlet的),登录成功的话,在LoginDeal.java(纯servlet的), 中使用 httpservletresponse 中的sendRedirect()方法的重定位,重定位的目的也是个servlet文件叫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("<h1>用户登录</h1>");
out.println("<form action='/servlettest/LoginDeal' method=post>");
out.println("用户名:<input type='text' name='username'> <br>");
out.println("密  码:<input type='password' name='pwd'> <br>");
out.println("<input type='submit' value='login'> <br>");
out.println("</form>");
}
}
LoginDeal.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;
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");//这个地方要特别注意,千万不能有其他设置了!!!!!不然会出现很让人不能理解的乱码!!
PrintWriter out = response.getWriter();
String uname = request.getParameter("username");
String username = MyTools.getNewCodeString(uname);
String passwd = request.getParameter("pwd");
System.out.println("LoginDeal中uname="+uname);
System.out.println("LoginDeal中username="+username);
System.out.println("LoginDeal中passwd="+passwd);
//如果登录成功,将用户名和密码传给MainFrame
if("你好".equals(username)&&"123".equals(passwd)){
response.sendRedirect("/servlettest/MainFrame?uname="+uname+"&pwd="+passwd);//传统方式
}else{
//如果用户名和密码不正确,则跳回到登录界面
response.sendRedirect("Login");
}
}
}
MainFrame.java 代码如下:
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");//此处就这样设置就行了~~~~
PrintWriter out = response.getWriter();
//使用传统的方法接受用户名和密码
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("MainFrame中username:"+username);
//使用传统的方法显示用户名和密码
//out.println("MainFrame中uname:"+uname+"<br>");
out.println("MainFrame中username="+username);
//out.println("password="+password);
}
}