很急!jsp乱码,100分求帮助
login.jsp的内容是<!-- http://127.0.0.1:8080//FightLandlordServer/jsp/umg/login.jsp -->
<%@ page language="java" import="java.util.*" pageEncoding="GBK"
isELIgnored="false"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":"
+ request.getServerPort() + path + "/";
request.setCharacterEncoding("gbk");
response.setCharacterEncoding("gbk");
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<style>
#userName {
max-width: 140px;
background: url(images/bg.gif);
}
#password {
max-width: 140px;
background: url(images/bg.gif);
}
.btn {
background: url(images/btn.gif);
}
body {
color: #008888;
/*background:#336600;*/
}
.login {
position: absolute;
top: 50%;
left: 60%;
}
tr {
padding: 20px 0px;
}
.loginLogo {
position: absolute;
top: 26%;
left: 26%;
}
</style>
</head>
<body>
<img src="images/loginLogo.gif" class="loginLogo" />
<div class="login">
<form action="LoginServlet" method="get">
用户名
<input type="text" name="userName" id="userName" />
<br />
<br />
密 码
<input type="password" name="password" id="password" />
<br />
<br />
用户类型
<select name="userType">
<option selected="selected">
玩家
</option>
<option>
管理员
</option>
</select>
<br />
<br />
<input type="submit" name="submit" value="登录" class="btn" />
<input type="reset" " name="reset" value="重置" class="btn" />
</form>
<font color="red">${message}</font>
</div>
</body>
</html>
----------------------------------------------------------
后台处理servlet是LoginServlet.java
package com.ddz.control;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 功能说明:处理http://127.0.0.1:8080//FightLandlordServer/jsp/umg/login.jsp的请求
* @author Administrator
*
*/
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException {
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
HttpSession session = request.getSession();
String userName= new String(request.getParameter("userName").getBytes("ISO-8859-1"),"gbk");
String password= request.getParameter("password");
String userType= new String(request.getParameter("userType").getBytes("ISO-8859-1"),"gbk");
System.out.println("userType:"+userType);
System.out.println("userName:"+userName);
String loginFlag=GameServlet.dsi.loginVerify(userName, password);
request.setAttribute("message", loginFlag);
if(loginFlag.equals("success")){
session.setAttribute("user", GameServlet.dsi.getNameByUserName(userName));
System.out.println("userType:"+userType);
if(userType.equals("玩家")){
request.getRequestDispatcher("jsp/umg/player.jsp").forward(request, response);
}else{
request.getRequestDispatcher("jsp/umg/manager.jsp").forward(request, response);
}
}else{//登录失败
request.getRequestDispatcher("jsp/umg/login.jsp").forward(request, response);
}
}
}
--------------------编程问答-------------------- 楼主什么也不说,说粘一大堆代码上来。
这样改下试试:
tomcat/conf/server.xml文件下:
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="GBK"/> --------------------编程问答-------------------- IOException {
request.setCharacterEncoding("GBK");
response.setCharacterEncoding("GBK");
HttpSession session = request.getSession();
String userName= new String(request.getParameter("userName").getBytes("ISO-8859-1"),"gbk");
String password= request.getParameter("password");
String userType= new String(request.getParameter("userType").getBytes("ISO-8859-1"),"gbk"); --------------------编程问答--------------------
+1
或者把提交方式改为POST试试。 --------------------编程问答-------------------- 改成pageEncoding="UTF-8" 国际编码
Get提交一般容易乱码
如果仍然乱码,你可以试着去解码两次编码的文字
--------------------编程问答-------------------- request.setCharaterEncoding,response.setCharactorEncoding,要写在开头。《meta》标签里面设置编码,
最好用UTF-8,文本文件的编码格式通过propertise修改为一致的类型。 --------------------编程问答--------------------
++1 --------------------编程问答-------------------- <%@ page language="java" contentType="text/html;charset=GBK" import="java.util.*" pageEncoding="GBK"
isELIgnored="false"%>
然后使用 1# 或者2#的方法 ,应该可以
--------------------编程问答--------------------
+++1 --------------------编程问答-------------------- 这么多人回复了,楼主也不说句话,真是的 --------------------编程问答-------------------- request.setCharacterEncoding("GBK");
String userName= new String(request.getParameter("userName").getBytes("ISO-8859-1"),"gbk");
只要一个试试, 前面已经过滤了, 后面又把它转回去了. 另外如果在linux上 , gbk 肯定不行, 全都换成utf-8吧 --------------------编程问答-------------------- 好像不管同什么编码格式 只要统一就不会乱码 --------------------编程问答-------------------- 你到底是哪里乱码了??数据调到后台去乱码吗?? --------------------编程问答-------------------- 希望你下次把问题交代清楚,是前端乱了,还是服务器端
是页面乱了,试着把浏览器的显示编码格式改成国标k,
页面也设置为国标k,
如果是服务器乱了,把提交方式改为post,
request.setcharsetEncoding(“gbk"); --------------------编程问答-------------------- --------------------编程问答-------------------- 在tomcat配置文件里面加上URIEncoding="utf-8" --------------------编程问答-------------------- 你把pageEncoding="GBK"改成pageEncoding="GB2312" --------------------编程问答-------------------- 传参乱码还是获参乱码。统一下。。。获参乱可以转。 --------------------编程问答-------------------- 表单提交请使用post传递:
而且也没有人愿意在url地址栏上能看到自己的账户和密码、
<form action="LoginServlet" method="post">
你用get方式传递肯定还是会乱码的、
除非设置服务器tomcat的编码集、 --------------------编程问答-------------------- <%
request.setCharacterEncoding("gbk");
response.setCharacterEncoding("gbk");
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":"
+ request.getServerPort() + path + "/";
%>
这样试试 --------------------编程问答-------------------- 写一个Filter直接搞定服务器端,本地我看见那个改tomcat配置文件的,已经有人回答了,都改了之后就没有问题了,想要servlet的话联系我。
补充:Java , Java EE