A页面:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@page pageEncoding="GB2312"%>
<%request.setCharacterEncoding("GB2312");%>
<input name="query" type="text" size="50" maxlength="50" />
<input type="submit" value="检索"/>B页面:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,com.net.*" errorPage="" %>
<%@page pageEncoding="GB2312"%>
<%request.setCharacterEncoding("GB2312");%>
String title = request.getParameter("query");
System.out.println(title);其中输出完全是问号加乱码,不知如何解决,谢谢!
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<%@page pageEncoding="GB2312"%>
<%request.setCharacterEncoding("GB2312");%>
<input name="query" type="text" size="50" maxlength="50" />
<input type="submit" value="检索"/>B页面:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,com.net.*" errorPage="" %>
<%@page pageEncoding="GB2312"%>
<%request.setCharacterEncoding("GB2312");%>
String title = request.getParameter("query");
System.out.println(title);其中输出完全是问号加乱码,不知如何解决,谢谢!
<%request.setCharacterEncoding("GB2312");%> 这两个不要写B里面写String title = new String(request.getParameter("query").getBytes("UTF-8"),"GBK");
java.net.URLDecoder.decode(request.getParameter("query"),"UTF-8");
A页面改成
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<from action="B.jsp" method="post">
<input name="query" type="text" size="50" maxlength="50" />
<input type="submit" value="检索"/>
</from> B页面改成
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,com.net.*" errorPage="" %>
<%String title = request.getParameter("query");
System.out.println(title); %>页面传页面一般不用转码
String title = new String(request.getParameter("query").getbytes("ISO8859-1"),"GBK"); 转换一下就行了
1.在每个页面的头部加上<%@page contentType="text/html;charset="GBK"%>。
2.在服务器的配置文件上加上URIEncoding="GBK"这一句,例如tomcat下的server.xml文件,具体加在什么地方呢?简单的说就是在配置端口的XML元素未尾,呵呵。
3.配置filter,此时需要几个类文件,其实在tomcat文件里面可以找到源码的,具体怎么操作我在这里不多说了,这么晚了想睡了,详细介绍我博客里有,有兴趣的话可以去看看,http://hi.baidu.com/rascal_hu。
做完以上步骤,问题应该不会存在了。
<Connector port="8080" protocol="HTTP/1.1" maxThreads="150" connectionTimeout="20000"
redirectPort="8443" />中把URIEncoding="GBK"加上去试试看吧。然后还需要做个过滤器。
2、加一个过滤器 public class MyFilter extends HttpServlet implements Filter { private static final long serialVersionUID = 1L;
private FilterConfig filterConfig;
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
} public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) {
try {
request.setCharacterEncoding("UTF-8"); //设置中文
filterChain.doFilter(request, response);
} catch (ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
} catch (IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
}
public void destroy() {
}
}
不过还有一点,你的整个程序要使用相同的编码
<from action="" method="post">
<input type="text"/>
</form>
应该是没有设成post..
import java.io.IOException;import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;public class CharacterEncodingFilter implements Filter {
protected String encoding = null; protected FilterConfig filterConfig = null; protected boolean ignore = true; public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else
this.ignore = false; } public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException { // Conditionally select and set the character encoding to be used
if (ignore) {
if (encoding != null)
request.setCharacterEncoding(encoding);
} // Pass control on to the next filter
chain.doFilter(request, response); } public void destroy() {
this.encoding = null;
this.filterConfig = null;
}}
#web.xml 加载进去<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>
com.hwadee.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
<init-param>
<param-name>ignore</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<input type="text"/>
</form>
2、在每个Jsp页面添加如下代码
view plaincopy to clipboardprint?
<%@ page pageEncoding=”gb2312″%>
<%@ page contentType=”text/html;charset=gb2312″%>
<%request.setCharacterEncoding(”gb2312″);%> <%@ page pageEncoding=”gb2312″%><%@ page contentType=”text/html;charset=gb2312″%><%request.setCharacterEncoding(”gb2312″);%>编码方式也可以改成GBK,支持繁体中文。重启Tomcat,这一步很重要,否则可能看不到效果。这种方法对post测试成功。建议两种同步使用。二、使用JSP的scriptLetview plaincopy to clipboardprint?
<%=new String(elValue.getBytes("iso-8859-1"),"GBK")%> <%=new String(elValue.getBytes("iso-8859-1"),"GBK")%>三、使用自定义JSTL第一,编写自定义标签
第二编写tag文件
第三部署tag文件
String title = new String(request.getParameter("query").getbytes("ISO8859-1"),"utf-8"); 要想一劳用逸的话就写一个filter