我有两个文件如下,在运行的时候出现了乱码:
information.html文件
<html>
<head>
<title>花花公子主页</title>
</head>
<body>
<center><h3>请输入您的信息</h3>
<form name="Form1" method="GET" action="JSPDeal.jsp">
姓名:<input type=text name="Name" ><br>
性别:<select name="Sex">
<option value="1" selected>男</option>
<option value"2" > 女</option>
</select><br>
<input type=submit name="Submit" value="提交">
<input type=reset name="Reset" value="重置">
</form>
</center>
</body>
</html>JSPDeal.jsp文件
<%@ page contentType="text/html;charset=GBK" %>
<%
response.setContentType("text/html;charset=GBK");
%>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=GBK" />
<title>您的信息</title>
</head>
<body>
<center>
<h3>Data you Posted</H3>
<%
String name=request.getParameter("Name");
String sex=request.getParameter("Sex");
%>
<table>
<tr>
<td>用户的姓名:</td>
<td><%=name%></td></tr>
<tr>
<td>用户的性别:</td>
<td><% if(sex.equals("1")) out.print("男");
else out.print("女");%></td></tr>
</table>
</center>
</body>
</html>
点击提交时,姓名出了乱码!求高手帮忙!
information.html文件
<html>
<head>
<title>花花公子主页</title>
</head>
<body>
<center><h3>请输入您的信息</h3>
<form name="Form1" method="GET" action="JSPDeal.jsp">
姓名:<input type=text name="Name" ><br>
性别:<select name="Sex">
<option value="1" selected>男</option>
<option value"2" > 女</option>
</select><br>
<input type=submit name="Submit" value="提交">
<input type=reset name="Reset" value="重置">
</form>
</center>
</body>
</html>JSPDeal.jsp文件
<%@ page contentType="text/html;charset=GBK" %>
<%
response.setContentType("text/html;charset=GBK");
%>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=GBK" />
<title>您的信息</title>
</head>
<body>
<center>
<h3>Data you Posted</H3>
<%
String name=request.getParameter("Name");
String sex=request.getParameter("Sex");
%>
<table>
<tr>
<td>用户的姓名:</td>
<td><%=name%></td></tr>
<tr>
<td>用户的性别:</td>
<td><% if(sex.equals("1")) out.print("男");
else out.print("女");%></td></tr>
</table>
</center>
</body>
</html>
点击提交时,姓名出了乱码!求高手帮忙!
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
改为
method="post"
<html>
<head>
<title>花花公子主页 </title>
</head>
<body>
<center> <h3>请输入您的信息 </h3>
<form name="Form1" method="post" action="JSPDeal.jsp">
姓名: <input type=text name="Name" > <br>
性别: <select name="Sex">
<option value="1" selected>男 </option>
<option value"2" > 女 </option>
</select> <br>
<input type=submit name="Submit" value="提交">
<input type=reset name="Reset" value="重置">
</form>
</center>
</body>
</html>
<%@ page contentType="text/html;charset=GBK" %>
<%
response.setContentType("text/html;charset=GBK");
%>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=GBK" />
<title>您的信息 </title>
</head>
<body>
<center>
<h3>Data you Posted </H3>
<%
request.setCharacterEncoding("GBK");
System.out.println("aaaaaaaa");
String name=request.getParameter("Name");
String sex=request.getParameter("Sex");
%>
<table>
<tr>
<td>用户的姓名: </td>
<td> <%=name%> </td> </tr>
<tr>
<td>用户的性别: </td>
<td> <% if(sex.equals("1")) out.print("男");
else out.print("女");%> </td> </tr>
</table>
</center>
</body>
</html>
在JSPDeal.jsp文件
的
<%
String name=request.getParameter("Name");
String sex=request.getParameter("Sex");
%>
里面改为:<%
response.setContentType("text/html;charset=GBK")
String name=request.getParameter("Name");
String sex=request.getParameter("Sex");
%>
<%
response.setContentType("text/html;charset=GBK");
%>改为:<%@ page language="java"contentType="text/html; charset=gb2312" pageEncoding="GB2312" import="java.sql.*" errorPage="error.jsp" %>
<%request.setCharacterEncoding("gb2312");%>
String name=request.getParameter("Name");
String sex=request.getParameter("Sex");
%>改为<%
String name=new String(request.getParameter("Name").getBytes("iso-8859-1"),"gbk");
String sex=new String(request.getParameter("Sex").getBytes("iso-8859-1"),"gbk"); %>
2、写过滤器,设置request.setCharacterEncoding("UTF-8")。
3、javascript脚本里用encodeURI(str)。
4、适当的时候,在jsp里用java.net.URLDecoder.decode(request.getParameter("str"),"UTF-8")。
5、最容易忽略的是使用"":value=" <%=paraFiles%>",一定要有引号。
6、new String(content.getBytes("ISO-8859-1"), "GBK");
我還添加一点就是在JSP头文件设置字符集编码:
<%page@ pageEcoding="UTF-8"%>
个人觉得还是使用过滤器过滤所有请求比较方便!
2
在请求范围要设置编码问题
request.setCharacterEncoding("gbk") ;
响应范围最好也要设置编码问题
response.setCharacterEncoding("gbk"); 3 可以用监听器filter 不过要配置 package filter ;
import javax.servlet.*; public class CharsetFilter implements Filter{
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("gbk") ;
response.setCharacterEncoding("gbk") ;
chain.doFilter(request, response) ;
}
public void init(FilterConfig config) throws ServletException {
}
}
在web.xml中配置
<filter>
<filter-name>Charset </filter-name>
<filter-class>filter.CharsetFilter </filter-class>
</filter>
<filter-mapping>
<filter-name>Charset </filter-name>
<url-pattern>/* </url-pattern>
</filter-mapping>