Java httpservletresponse 中的sendRedirect()方法的重定位乱码的总结

最近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("密&nbsp&nbsp码:<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);
}


}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值