日期:2014-05-19  浏览次数:20659 次

乱码问题,求救啊 谢谢各位大侠啦
servlet的代码为:
package servlet.wl.com;

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 javax.servlet.http.HttpSession;

import User.wl.com.CheckUser;

public class CheckLoginServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
String username=request.getParameter("username");
String password=request.getParameter("password");
HttpSession session=request.getSession();
session.setAttribute("user", username);
CheckUser user=new CheckUser();
boolean i=user.check(username, password);
out.print(i);
if (i==true){
request.getRequestDispatcher("loginsuccess.jsp").forward(request, response);
}else{
response.sendRedirect("loginError.jsp");
}  


}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

this.doGet(request, response);
}

}



登录成功显示loginsuccess.jsp,代码为
<%@ page language="java" contentType="text/html; charset=UTF-8" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <base href="<%=basePath%>">
   
  <title>My JSP 'loginsuccess.jsp' starting page</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">  
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>
  
  <body>
 登录成功! <br>
  </body>
</html>




------解决方案--------------------
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
替换为:
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
如果form表单提交时method为post,则在doPost里也要加上那两句
试一下吧