当前位置: 代码迷 >> Java Web开发 >> jsp页面乱码
  详细解决方案

jsp页面乱码

热度:130   发布时间:2007-07-28 18:18:14.0
jsp页面乱码

jsp页面单独运行不乱码,从servlet传值就出现乱码!!下面是jsp和servlet的代码!!
<%request.setCharacterEncoding("GBK");%>
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>

<%
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><%
request.setCharacterEncoding("UTF-8");
if(request.getAttribute("smsg")!=null)
{
String smsg=request.getAttribute("smsg").toString();
out.print(smsg);
}
else
out.print("成功页");

%></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>
<h1 align="center">恭喜、<%=request.getAttribute("suc")==null?"恭喜":request.getAttribute("suc").toString()%></h1>
<hr color="grey" width="100%">
<p align="center"><a href=<%=request.getAttribute("sback")==null?"#":request.getAttribute("sback").toString()%>>返回</a> </p>
</body>
</html>
================================================================
request.setCharacterEncoding("GBK");
request.setAttribute("smsg", "注册成功");
request.setAttribute("suc", "注册成功!");
request.setAttribute("sback", "userlogin.jsp");
this.getServletContext().getRequestDispatcher("/Success.jsp").forward(request, response);

搜索更多相关主题的帖子: jsp页面  乱码  request  java  

----------------解决方案--------------------------------------------------------
大家给我看看帮帮忙!!!
----------------解决方案--------------------------------------------------------
你这个传值是从Servlet传到JSP是吧``
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> 你这个JSP页面是GBK
<%request.setCharacterEncoding("GBK");%> 你在最上面是用的这个编码 ,这是对的
但是
<title><%
request.setCharacterEncoding("UTF-8");
if(request.getAttribute("smsg")!=null)
{
String smsg=request.getAttribute("smsg").toString();
out.print(smsg);
} 你在下面又用 UTF-8编码 显示的时候肯定是乱码 把这个去掉 或者改成GBK

----------------解决方案--------------------------------------------------------

我把第一句的和request.setCharacterEncoding("UTF-8");
都删除掉了,还是乱码!!
改成request.setCharacterEncoding("GBK");
之后也还是乱码.
这是为什么呢!!
我用response.sendRedirect();跳转就不会乱码,但是值带不过去!!


----------------解决方案--------------------------------------------------------
......
一般只要request.setCharacterEncoding("GBK");这个就不会是乱码了``
那你把request.setCharacterEncoding("GBK"); 和 request.setCharacterEncoding("UTF-8");都删除了
然后 String smsg= (String)request.getAttribute("smsg").toString();
String smsg1 = new String(smsg.getBytes("ISO-8859-1"),"gbk");
out.print(smsg1);

你在试一下看``要是还是乱码我就不清楚了``

----------------解决方案--------------------------------------------------------

还是不行,我真是服这个页面了!!
是jsp页面的问题还是servlet的问题,我别的页面都没有什么问题,只有这个页!!


----------------解决方案--------------------------------------------------------

我把它粘下来了大家帮找找
package servlet;

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 bbsBean.User;
import userImp.userimp;
public class Registuser extends HttpServlet {

/**
* Constructor of the object.
*/
public Registuser() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
request.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");

out.close();
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
request.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
String username=request.getParameter("uid");
String password=request.getParameter("password");
String email=request.getParameter("email");
String nickname=request.getParameter("nickname");
int degree=1;
String registerdate = "sysdate";
User user=new User();
user.setName(username);
user.setPassword(password);
user.setEmail(email);
user.setNickname(nickname);
user.setDegree(degree);
user.setRegisterdate(registerdate);
userimp uimp=new userimp();
try
{
if(uimp.userAdd(user))
{
request.setAttribute("smsg", "注册成功");
request.setAttribute("suc", "注册成功");
request.setAttribute("sback", "userlogin.jsp");
//response.sendRedirect("Success.jsp");
request.getRequestDispatcher("Success.jsp").forward(request, response);
}
else
{

}

}
catch(Exception ex)
{
System.out.println(ex.getMessage());
}

out.close();
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}

}
jsp页
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>

<%
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><%
request.setCharacterEncoding("GBK");
if(request.getAttribute("smsg")!=null)
{
String smsg=request.getAttribute("smsg").toString();
out.print(smsg);
}
else
out.print("成功页");

%></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>
<h1 align="center">恭喜、<%=request.getAttribute("suc")==null?"恭喜":request.getAttribute("suc").toString()%></h1>
<hr color="grey" width="100%">
<p align="center"><a href=<%=request.getAttribute("sback")==null?"#":request.getAttribute("sback").toString()%>>返回</a> </p>
</body>
</html>



----------------解决方案--------------------------------------------------------
  相关解决方案