<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>Insert title here</title>
</head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<body>
<form action="params.jsp">
请输入学员 ID:<br>
<input type ="text" name="myid"><br>
请输入学员 name:<br>
<input type ="text" name="myname"><br>
请输入学员性别:<br>
<input type="radio" name="mysex" value="m" checked>男
<input type="radio" name="mysex" value="f">女<br>
<input type="submit" name="mysubmit"><br>
<input type="reset" value="重新填写" ><br>
</form>
</body>
</html>===========
jsp部分
=========
<%@ page language="java" contentType="text/html; charset=GBK"
%><html>
<head><title>Insert title here</title>
</head>
<body>
<%@ page import="java.util.*" %>
<%
String current_param="";
request.setCharacterEncoding("GBK");
Enumeration e=request.getParameterNames();
while(e.hasMoreElements()){
current_param=(String)e.nextElement();
out.println("name:"+current_param+" ");
out.println("values:"+request.getParameter(current_param)+"<br>");
}
%>
</body>
</html>执行以后jsp调用无法显示汉字
<html>
<head><title>Insert title here</title>
</head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<body>
<form action="params.jsp">
请输入学员 ID:<br>
<input type ="text" name="myid"><br>
请输入学员 name:<br>
<input type ="text" name="myname"><br>
请输入学员性别:<br>
<input type="radio" name="mysex" value="m" checked>男
<input type="radio" name="mysex" value="f">女<br>
<input type="submit" name="mysubmit"><br>
<input type="reset" value="重新填写" ><br>
</form>
</body>
</html>===========
jsp部分
=========
<%@ page language="java" contentType="text/html; charset=GBK"
%><html>
<head><title>Insert title here</title>
</head>
<body>
<%@ page import="java.util.*" %>
<%
String current_param="";
request.setCharacterEncoding("GBK");
Enumeration e=request.getParameterNames();
while(e.hasMoreElements()){
current_param=(String)e.nextElement();
out.println("name:"+current_param+" ");
out.println("values:"+request.getParameter(current_param)+"<br>");
}
%>
</body>
</html>执行以后jsp调用无法显示汉字
我没尝试这么做过。我喜欢写一个过滤器。
public class SetCharacterEncodingFilter implements Filter {
/** 字符编码 */
protected String encoding = null;
/** 过滤器配置器,init初始化过滤器方法中使用它取得web.xml中的配置参数 */
protected FilterConfig filterConfig = null; /**
* 初始化过滤器方法
* @param filterConfig FilterConfig对象,系统初始化本类的实例时调用该方法
* 并传递它,用于读取web.xml中的配置参数。
* @exception ServletException
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
} /**
* Filter类的核心方法,过滤器的功能,在这个方法中
*
* @param request 请求对象
* @param response 相应对象
* @param chain 过滤器链
*
* @exception IOException, ServletException
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
if (request instanceof HttpServletRequest
&& "GET".equalsIgnoreCase(((HttpServletRequest) request)
.getMethod())) {
// 当请求是GET放式时使用UTF-8字符集
request.setCharacterEncoding("UTF-8");
} else {
// 使用web.xml中设置的字符集
request.setCharacterEncoding(encoding);
}
//把请求和响应传递给下一个Filter或者其他JSP/Servlet等资源
chain.doFilter(request, response);
}
/**
* 系统销毁本过滤器前调用本方法,执行清理操作
* 将encoding和filterConfig属性删除,释放占用的系统资源
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
}配置web.xml <filter>
<filter-name>SetCharacterEncodingFilter</filter-name>
<filter-class>。(包)</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
第二个jsp页面 <%@ page language= "java " contentType= "text/html; charset=GBK " % >
用的是 gbk编码 不出乱码 才神了
都改称 一样的 用 gbk 或者 gb2312 就不会有问题了
我是用eclipse调试的,感谢fashi1000