- 爱易网页
-
Java教程
- 为什么get方法无法显示中文,post方法就可以呢?解决思路
日期:2014-05-18 浏览次数:20746 次
为什么get方法无法显示中文,post方法就可以呢?
我把register.jsp的 <form> 中的method定义为get时,输入中文会出现乱码,定义为post时就不会显示乱码了。这两个方法存在对中文的支持问题吗?
代码如下:
先建一个Studnet类
package school;
public class Student {
private String name;
private String num;
private String sex;
public void setName(String name)
{
this.name=name;
}
public void setNum(String num)
{
this.num=num;
}
public void setSex(String sex)
{
this.sex=sex;
}
public String getName()
{
return name;
}
public String getNum()
{
return num;
}
public String getSex()
{
return sex;
}
}
下面是两个JSP文件
register.jsp
<%@page language= "java " contentType= "text/html; charset=gb2312 "%>
<%request.setCharacterEncoding( "gb2312 "); %>
<%@page pageEncoding= "gb2312 " %>
<jsp:useBean id= "stu " scope= "session " class= "school.Student " />
<jsp:setProperty name= "stu " property= "* "/>
<%if(request.getParameter( "name ")!=null)
{
response.sendRedirect( "stuInfo.jsp ");
}
%>
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> 欢迎您,请输入个人信息 </title>
</head>
<body>
<h1> 欢迎您,请输入个人信息 </h1>
<form method= "get " action= "register.jsp ">
<p> 姓名: <input type= "text " name= "name " size= "25 "> </p>
<p> 学号: <input type= "text " name= "num " size= "25 "> </p>
<p> 性别: <input type= "text " name= "sex " size= "25 "> </p>
<input type= "submit " value= "提交 ">
<input type= "reset " value= "重置 ">
</form>
</body>
</html>
stuInfo.jsp文件
<%@ page language= "java " contentType= "text/html; charset=gb2312 "%>
<%request.setCharacterEncoding( "gb2312 ");%>
<%@ page pageEncoding= "gb2312 " %>
<jsp:useBean id= "stu " scope= "session " class= "school.Student "/>
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> 欢迎您, <%=stu.getName()%> </title>
</head>
<body>
<p> 姓名: <%=stu.getName() %> </p>
<p> 学号: <%=stu.getNum() %> </p>
<p> 性别: <%=stu.getSex() %> </p>
</body>
</html>
frame中获取session有关问题