发布于 2016-08-18 05:35:53 | 190 次阅读 | 评论: 1 | 来源: 网友投递
JSP JAVA公司的WEB开发编程语言
JSP全名为Java Server Pages - java服务器页面是由Sun Microsystems公司倡导、许多公司参与一起建立的一种动态网页技术标准。JSP技术有点类似ASP技术,它是在传统的网页HTML(标准通用标记语言的子集)文件(*.htm,*.html)中插入Java程序段(Scriptlet)和JSP标记(tag),从而形成JSP文件,后缀名为(*.jsp)。
jsp代码:
import javax.servlet.*;
import javax.servlet.http.*;
public class CharsetFilter implements Filter
{
public void destroy()
{
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
{
try
{
HttpServletRequest httpRequest = (HttpServletRequest)request;
String method = httpRequest.getMethod().toLowerCase();
if(method.equals("post"))
{
//如果是post,即表单方法,直接设置charset即可
request.setCharacterEncoding("UTF-8");
}
else if(method.equals("get"))
{
//如果是get方法
request.setCharacterEncoding("UTF-8");
request = new HttpServletRequestWrapper((HttpServletRequest)request)
{
public String getParameter(String str)
{
try
{
return new String(super.getParameter(str).getBytes("iso-8859-1"),"GBK");
}
catch(Exception e)
{
return null;
}
}
};
}
chain.doFilter(request, response);
}
catch(Exception e){}
}
public void init(FilterConfig filterConfig)
{
}
}
过滤器配置:
<filter>
<filter-name>CharFilter</filter-name>
<filter-class>CharsetFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>