struts提交后,中文显示乱码 我做的是简单的login测试,点提交后,输入的中文变成了乱码,用validation验证的中文能正常显示,page也<%@ page language="java" contentType="text/html; charset=UTF-8" %>这样设定了,还有哪里会出问题呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 如果是tomcat的话可以尝试重新编码new String(q.getBytes("ISO-8859-1"),"UTF-8"); 兄弟,这个问题我刚接触的时候也遇到过,如果是request获得的话要通过request.request.setCharacterEncoding("gb2312");来解决,要是页面显示问题 通过 <%page contentType="text/html; charset=gb2312" "%>来稿定! 上面多了个request 不好意思 这是我的Login.jsp代码:<%@ page language="java" contentType="text/html; charset=UTF-8" %><%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%> <html> <head> <title>JSP for loginForm form</title> </head> <body> <html:form action="/login"> username : <html:text property="username"/><html:errors property="username"/><br/> password : <html:password property="password"/><html:errors property="password"/><br/> <html:submit/><html:cancel/> </html:form> </body></html> 这是我LoginAction的代码:package com.test.struts.action;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;import org.apache.struts.action.ActionForm;import org.apache.struts.action.ActionForward;import org.apache.struts.action.ActionMapping;import org.apache.struts.validator.DynaValidatorForm;public class LoginAction extends Action { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { DynaValidatorForm loginForm = (DynaValidatorForm) form; String username=loginForm.getString("username"); String password=loginForm.getString("password"); if(username.equals("test")||password.equals("test")){ return mapping.findForward("indexGo"); }else{ return mapping.getInputForward(); } }} 写一个myActionServlet来并覆盖ActionServlet中的process()方法。 protected void process(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException { /**@todo Override this org.apache.struts.action.ActionServlet method*/ request.setCharacterEncoding("GB2312");//就加着一行一切都解决了 super.process(request, response); }当然别忘了改一下web.xml里面的配置 <servlet> <servlet-name>action</servlet-name> <servlet-class>strutsdemo.myActionServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet>改一下servlet-class标签中的内容就可以! 应该可以的,你试一下! tianming2002() 我在LoginAction里加了try{ request.setCharacterEncoding("UTF-8");}catch(Exception e){}但是还是没管用。。 自己写一个转换的类import java.io.*;public class ToGBK { public static String toGBK(String instr){ String outstr=null; if(instr!=null){ byte[] bytes = null; try { bytes = instr.getBytes("ISO8859-1"); outstr=new String(bytes,"GBK"); } catch (UnsupportedEncodingException ex) { System.out.println("ToGBK_Error"); } return outstr; }else{ return null; } }} 然后String username=ToGBK.toGBK(loginForm.getString("username")); to tianzhijie11(抓青蛙) 你的方法我刚才试了,也不行啊 用过滤器处理所有的POST提交,设置TOMCAT的ENCODING处理所有GET的提交这样才完美啊 编码方式指定一下,一般的话用UTF-8(国际化),还有就是设置一下你的应用服务器连接池的characterEncoding和8080端口的URIEncoding,最好是自己再加一个Filter zhangj0571(笨鸟飞飞)应用服务器连接池的characterEncoding8080端口的URIEncoding这两个在哪里设定?我是用eclipse的myeclipse插件做的。 chq32(无情)怎么设置能说具体点吗 要看看你数据库的编码是不是也是utf-8 filter也试了,没用!!!继续无奈ING!!! tomcat中解决提交中文乱码问题,针对post 和 get是不同的post方式提交需要设置一个Filter,在里面设置request.setCharacterEncoding("gb2312")就可以了。get方式提交需要在server.xml中connector后面加入URLEncoding="GBK" 我重写了actionservlet的process方法在我的MyActionServlet的名字下提示:The serializable class EncodeActionServlet does not declare a static final serialVersionUID field of type long这个错误不知道怎么解决 。。 还是用了重写process方法,设定字符集!! 写个自己的EncodeActionServlet,设定Process()方法:package com.test.struts;import org.apache.struts.action.*;import javax.servlet.http.*;import javax.servlet.*;import java.io.*;public class EncodeActionServlet extends ActionServlet{ protected void process( HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { HttpSession session = request.getSession(true); if (session.isNew())//session超时 { response.sendRedirect("/login.jsp"); return; } request.setCharacterEncoding("utf-8"); //进行统一的中文转码 super.process(request, response); }}然后在web.xml里加入如下配置:<!-- Action Servlet Configuration --> <servlet> <servlet-name>actionServlet</servlet-name> <servlet-class>com.test.struts.EncodeActionServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- Action Servlet Mapping --> <servlet-mapping> <servlet-name>actionServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>重启tomcat,就OK了. <%@ page language="java" contentType="text/html; charset=UTF-8" %>改成<%@ page language="java" contentType="text/html; charset=GBK" %>试试? 写个filter就不用那么麻烦了 表单自动提交 使用Hibernate Annotation出现的问题 java 抓取网页 struts 2 视频流怎么传输 在线等 求个sql语句 上传图片的问题? 怎么用JSP编写程序 页面上实现html打印 关于WebLogic配置(类文件修改后必需重启)的问题 在线等解! 急:如何将数据字典中的数据项读取到内存中,菜鸟一枚,望大神们指点 EJB出学者的几点疑问。 很急 大家帮帮忙 关于下拉框的问题
new String(q.getBytes("ISO-8859-1"),"UTF-8");
要通过
request.request.setCharacterEncoding("gb2312");来解决,
要是页面显示问题 通过 <%page contentType="text/html; charset=gb2312" "%>
来稿定!
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<html>
<head>
<title>JSP for loginForm form</title>
</head>
<body>
<html:form action="/login">
username : <html:text property="username"/><html:errors property="username"/><br/>
password : <html:password property="password"/><html:errors property="password"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
import javax.servlet.http.HttpServletResponse;import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.DynaValidatorForm;public class LoginAction extends Action { public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
DynaValidatorForm loginForm = (DynaValidatorForm) form;
String username=loginForm.getString("username");
String password=loginForm.getString("password");
if(username.equals("test")||password.equals("test")){
return mapping.findForward("indexGo");
}else{
return mapping.getInputForward();
} }}
/**@todo Override this org.apache.struts.action.ActionServlet method*/ request.setCharacterEncoding("GB2312");//就加着一行一切都解决了
super.process(request, response);
}当然别忘了改一下web.xml里面的配置
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>strutsdemo.myActionServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>2</param-value>
</init-param>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>改一下servlet-class标签中的内容就可以!
应该可以的,你试一下!
try{
request.setCharacterEncoding("UTF-8");
}catch(Exception e){}但是还是没管用。。
import java.io.*;public class ToGBK {
public static String toGBK(String instr){
String outstr=null;
if(instr!=null){
byte[] bytes = null;
try {
bytes = instr.getBytes("ISO8859-1");
outstr=new String(bytes,"GBK");
} catch (UnsupportedEncodingException ex) {
System.out.println("ToGBK_Error");
}
return outstr;
}else{
return null;
}
}}
这样才完美啊
8080端口的URIEncoding这两个在哪里设定?我是用eclipse的myeclipse插件做的。
post方式提交需要设置一个Filter,在里面设置request.setCharacterEncoding("gb2312")就可以了。
get方式提交需要在server.xml中connector后面加入URLEncoding="GBK"
The serializable class EncodeActionServlet does not declare a static final serialVersionUID field of
type long
这个错误不知道怎么解决 。。
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;public class EncodeActionServlet extends ActionServlet
{
protected void process(
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
HttpSession session = request.getSession(true);
if (session.isNew())//session超时
{
response.sendRedirect("/login.jsp");
return;
}
request.setCharacterEncoding("utf-8"); //进行统一的中文转码
super.process(request, response);
}
}
然后在web.xml里加入如下配置:<!-- Action Servlet Configuration -->
<servlet>
<servlet-name>actionServlet</servlet-name>
<servlet-class>com.test.struts.EncodeActionServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> <!-- Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>actionServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>重启tomcat,就OK了.
改成
<%@ page language="java" contentType="text/html; charset=GBK" %>
试试?