应用过滤器如下:
=============过滤器:EncodingFilter.java=============
package com.jspdev.ch16;import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.FilterConfig;public class EncodingFilter implements Filter
{
protected FilterConfig filterConfig;
private String targetEncoding = "gb2312";
public void init(FilterConfig config) throws ServletException
{
this.filterConfig = config;
this.targetEncoding = config.getInitParameter("encoding");
}
public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain chain)throws IOException, ServletException
{
HttpServletRequest request = (HttpServletRequest)srequest;
request.setCharacterEncoding(targetEncoding);
chain.doFilter(srequest,sresponse);
} public void setFilterConfig(final FilterConfig filterConfig)
{
this.filterConfig=filterConfig;
}
public void destroy()
{
this.filterConfig=null;
}
}
================web.xml配置文件================
<?xml version="1.0" encoding="gb2312"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
version="2.4"> <description>
Examples for the 'standard' taglib (JSTL)
</description> <filter>
<filter-name>encoding</filter-name>
<filter-class>com.jspdev.ch16.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>http://hellking.com/function</taglib-uri>
<taglib-location>/WEB-INF/functions.tld</taglib-location>
</taglib>
</web-app>
===============JSP应用程序===================
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>表达式语言的使用</title>
</head>
<body>
<%request.setCharacterEncoding("gb2312");%>
姓名:${param.userName}<form action="" method="get" name="form1">
<input type=text name="userName"><br>
<input type=submit value=提交>
</form></body>
</html>
=============程序运行结果====================
http://localhost:8080/ch16/request.jsp在文本框输入:江苏省
地址栏的反应:http://localhost:8080/ch16/request.jsp?userName=%BD%AD%CB%D5%CA%A1
页面显示效果:姓名:??????
=============过滤器:EncodingFilter.java=============
package com.jspdev.ch16;import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.FilterConfig;public class EncodingFilter implements Filter
{
protected FilterConfig filterConfig;
private String targetEncoding = "gb2312";
public void init(FilterConfig config) throws ServletException
{
this.filterConfig = config;
this.targetEncoding = config.getInitParameter("encoding");
}
public void doFilter(ServletRequest srequest, ServletResponse sresponse, FilterChain chain)throws IOException, ServletException
{
HttpServletRequest request = (HttpServletRequest)srequest;
request.setCharacterEncoding(targetEncoding);
chain.doFilter(srequest,sresponse);
} public void setFilterConfig(final FilterConfig filterConfig)
{
this.filterConfig=filterConfig;
}
public void destroy()
{
this.filterConfig=null;
}
}
================web.xml配置文件================
<?xml version="1.0" encoding="gb2312"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
version="2.4"> <description>
Examples for the 'standard' taglib (JSTL)
</description> <filter>
<filter-name>encoding</filter-name>
<filter-class>com.jspdev.ch16.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gb2312</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>http://hellking.com/function</taglib-uri>
<taglib-location>/WEB-INF/functions.tld</taglib-location>
</taglib>
</web-app>
===============JSP应用程序===================
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=gb2312" language="java"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>表达式语言的使用</title>
</head>
<body>
<%request.setCharacterEncoding("gb2312");%>
姓名:${param.userName}<form action="" method="get" name="form1">
<input type=text name="userName"><br>
<input type=submit value=提交>
</form></body>
</html>
=============程序运行结果====================
http://localhost:8080/ch16/request.jsp在文本框输入:江苏省
地址栏的反应:http://localhost:8080/ch16/request.jsp?userName=%BD%AD%CB%D5%CA%A1
页面显示效果:姓名:??????
解决方案 »
- 浏览器地址栏输入URL后能跳转到servlet吗?在线等,,求高手
- 项目出现问题,严重: Exception sending context initialized event to listener instance...
- frame内元素跳出框架问题
- 在struts中如何使用js如onClick,onChange
- 一个SSH结构的一对多问题【菜鸟提问】请大家帮帮忙急。。。。。
- 关于servlet映射路径问题,查了半天实在不知道原因,求助
- Hibernate discriminator 映射问题 求教育
- 求教jsp和servlet的问题
- 怎样编程判断编辑框中输进去的是字母或数字而不能是文字。
- 客户端JavaScipt对servlet的调用问题。各位高手请进!!!
- 不明白是什么错误
- jstl和表达式语言的问题
try
{
temp=chi.getBytes("iso-8859-1");
result = new String(temp);
}
把你这段代码的ISO-8859-1也改为gb2312,我试过可以解决,不过我用的是resin+apache,在TOMCAT中不用改
改为:<%@ page contentType="text/html" language="java"%> 我发现很多人根本不懂得表达式的使用。谢谢各位!