怎么解决servlet中接收到的中文乱码问题?

Bonjourss 2013-03-23 08:28:52
index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<title>login.html</title>
</head>
<body>
<form action="a" method="post">
用户名:<input type="text" name="name"><br>
密  码:<input type="password" name="password"><br>
性  别:<input type="radio" name="sex" value="男">男
<input type="radio" name="sex" value="女">女<br>
<input type="submit" value="提交">
<input type="reset" value="重置">
</body>
</html>

servelt:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
//import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class servlet extends HttpServlet {

public void dopost(HttpServletRequest request,

HttpServletResponse response)
throws ServletException, IOException {

PrintWriter out=response.getWriter();
request.setCharacterEncoding("gb2312");
response.setContentType("text/html");
out.print("<html>");
out.print("<head><title>dsgvf</title></head>");
out.print("<body>");
out.print(request.getParameter("name")+"<br>");
out.print(request.getParameter("password")+"<br>");
out.print(request.getParameter("sex")+"<br>");
out.print("</body>");
out.print("</html>");
out.close();
}

}


web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>servlet</servlet-name>
<servlet-class>servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet</servlet-name>
<url-pattern>/a</url-pattern>
</servlet-mapping>
</web-app>


我把servlet中和jsp中方法都改成post了结果出现405错误。如果都是get方法则服务器端接收的内容会有乱码出现。
...全文
2008 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
hao911115 2015-05-21
  • 打赏
  • 举报
回复
你这是response乱码问题 你在doPost()方法的最前面加上 response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); 在把request.setCharacterEncoding("gb2312"); 改为request.setCharacterEncoding("UTF-8"); 应该就能解决问题了
hao911115 2015-05-21
  • 打赏
  • 举报
回复
request对象中的乱码问题: 关于post请求的乱码问题: 我们只需要在获得客户请求参数之前使用request对象中的 setCharacterEncoding(String env)方法就行 例: request.setCharacterEncoding("UTF-8"); String str=request.getParameter("name"); System.out.println(str); 注:setCharacterEncoding(String env)只对客户的post请求有效,对客户get请求无效 关于get请求的乱码问题: 例: String str=request.getParameter("name"); str=new String(str.getBytes("iso8859-1"),"UTF-8"); System.out.println(str); String 的构造方法 String(byte[] bytes, String charsetName) 通过使用指定的 charset 解码指定的 byte 数组, 构造一个新的 String。
lpx12301124 2014-05-01
  • 打赏
  • 举报
回复
引用 8 楼 huanlin08 的回复:
新手吧,什么都不用改,你jsp页面用utf-8,现在只要把request.setCharacterEncoding("gb2312"); 改成 request.setCharacterEncoding("utf-8"); 就行了
这个不行啊 我的jsp和servlet都用的是utf-8 但还是有问题
huanlin08 2013-03-24
  • 打赏
  • 举报
回复
新手吧,什么都不用改,你jsp页面用utf-8,现在只要把request.setCharacterEncoding("gb2312"); 改成 request.setCharacterEncoding("utf-8"); 就行了
Bonjourss 2013-03-23
  • 打赏
  • 举报
回复
做出来了。没有乱码问题了,,研究了一天。。各种贴各种看,总结一点经验 1.出错原因是doPost方法用于处理前台提交的数据,当form表单属性method="post"时,doPost方法才被调用。当用servlet类的URL直接访问servlet时,发送的是HTTP请求,servlet默认调用doGet()方法,而源程序里没有重写父类的doGet()方法,父类的doGet()方法不被此种URL支持,所以出现HTTP method GET is not supported by this URL这种错误。 2.jsp里边的编码格式要跟servlet里边统一 jsp:<%@ page contentType="text/html" pageEncoding="utf-8"%> servlet:request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); 3.还有要注意web.xml里边的路径对不对。url地址要和jsp里边action一致。 4.注意<form action="q" method="post">里边的method是post,在servlet里边相应的就调用doPost方法,然后在调用doGet方法 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); this.doGet(request, response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out=response.getWriter(); out.print("<html>"); out.print("<head><title>dsgvf</title></head>"); out.print("<body>"); out.print(request.getParameter("name")+"<br>"); out.print(request.getParameter("password")+"<br>"); out.print(request.getParameter("sex")+"<br>"); out.print("</body>"); out.print("</html>"); out.close(); }
Bonjourss 2013-03-23
  • 打赏
  • 举报
回复
引用 1 楼 ycw20111225 的回复:
编码不统一,你的jsp页面是utf-8的,请求的是gb2312的
统一完了还是错的啊。。是把servlet里边的改为UTF-8了
getdate 2013-03-23
  • 打赏
  • 举报
回复
1:编码不统一,将编码统一即可; 2:可以把页面上的汉字用其他方式标示,如英文缩写;
leeCross 2013-03-23
  • 打赏
  • 举报
回复
你使用的是 tomcat服务器话 把server.xml文件中的 增加 URLencoding="GBK"
「已注销」 2013-03-23
  • 打赏
  • 举报
回复
wjx630216732 2013-03-23
  • 打赏
  • 举报
回复
引用 1 楼 ycw20111225 的回复:
编码不统一,你的jsp页面是utf-8的,请求的是gb2312的
+1
yangxxxxx 2013-03-23
  • 打赏
  • 举报
回复
编码不统一,你的jsp页面是utf-8的,请求的是gb2312的

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧