标题:jsp页面乱码
只看楼主
liqi
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-7-28
 问题点数:0 回复次数:6 
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 
2007-07-28 18:18
liqi
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-7-28
得分:0 
大家给我看看帮帮忙!!!
2007-07-30 13:16
Gramary
Rank: 4
等 级:贵宾
威 望:13
帖 子:769
专家分:0
注 册:2007-7-9
得分:0 
你这个传值是从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

学编程不能急,要慢慢来。
2007-07-30 13:36
liqi
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-7-28
得分:0 

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

2007-07-30 14:14
Gramary
Rank: 4
等 级:贵宾
威 望:13
帖 子:769
专家分:0
注 册:2007-7-9
得分:0 
......
一般只要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);

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

学编程不能急,要慢慢来。
2007-07-30 14:28
liqi
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-7-28
得分:0 

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

2007-07-30 22:07
liqi
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-7-28
得分:0 

我把它粘下来了大家帮找找
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>


2007-07-30 22:12



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-158804-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.356749 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved