您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> Web开发 >> (jsp)POST提交表单中文乱码,搞了一下午了。求助

(jsp)POST提交表单中文乱码,搞了一下午了。求助

来源:网络整理     时间:2016/5/31 20:35:34     关键词:提交表单,post

关于网友提出的“(jsp)POST提交表单中文乱码,搞了一下午了。求助”问题疑问,本网通过在网上对“(jsp)POST提交表单中文乱码,搞了一下午了。求助”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题:(jsp)POST提交表单中文乱码,搞了一下午了。求助
描述:

Javapost请求乱码

前台JSP:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
。。。。
。。。。
。。。。



用 户 名:


密  码:

 

确认密码:

 

Email地址:

 





后台servlet:
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String username=req.getParameter("username");
System.out.println(username);
}
过滤器:
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding( "utf-8" );
MyRequest myRequest=new MyRequest((HttpServletRequest)req);
String path = myRequest.getContextPath();
String basePath = myRequest.getScheme()+"://"+myRequest.getServerName()+":"+myRequest.getServerPort()+path+"/";
req.setAttribute("base", basePath);
resp.setContentType("text/html;charset=UTF-8");
chain.doFilter(myRequest, resp);
}
过滤器用到的MyRequest :
package com.shop.util;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class MyRequest extends HttpServletRequestWrapper {
public MyRequest(HttpServletRequest request) {
super(request);
}
public String getParameter(String name) {
String value=super.getParameter(name);
if(value==null){
value="";
}
try {
value=new String(value.getBytes("ISO-8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return value;
}
@Override
public Cookie[] getCookies() {
// TODO Auto-generated method stub
return super.getCookies();
}
}
我就把部分代码贴出来了。这个过滤器我以前做项目就用的,都没问题。这次不知为何中文通过post提交就变成“??”,用get方式提交则没有问题。求助各位,万分感谢!


解决方案1:

jsp文件头添加 c/html;charset=utf-8",编码最好统一,要么都大写,要么都小写


以上介绍了“(jsp)POST提交表单中文乱码,搞了一下午了。求助”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/1438005.html

相关图片

相关文章