首页 > 代码库 > JSP详细篇——Servlet过滤器和监听器(二)
JSP详细篇——Servlet过滤器和监听器(二)
4.字符编码过滤器
在Java Web开发中,由于Web容器内部所使用的编码格式并不支持中文,所以,处理浏览器请求的中文数据会出现乱码。
Web容器使用的编码格式是ISO-8859-1,所以在Web应用的业务处理中也会使用ISO-8859-1的编码格式。虽然浏览器提交请求使用的中文编码格式是UTF-8,但是经业务处理中的ISO-8859-1编码,就会出现乱码。在开发中,处理此问题的方法是:通过过滤器来处理字符编码。
范例:
实现图书信息的添加功能,并创建字符编码过滤器,避免中文乱码出现。
(1)创建字符编码过滤器,其名称为EncodingFilter,该类实现了javax.servlet.Filter接口,并在doFilter()方法中对请求中的中文的字符编码格式进行设置。
package com.zgy.filter;
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 EncodingFilter implements Filter {
String encoding = null; // 字符编码
@Override
public void destroy() {
// TODO Auto-generated method stub
encoding = null;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
if (encoding != null) {
request.setCharacterEncoding(encoding); //设置resquest字符编码
response.setContentType("text/html;charset=" + encoding); //设置response字符编码
}
chain.doFilter(request, response); //传递给下一个过滤器
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
encoding = filterConfig.getInitParameter("encoding"); //获取初始化参数encoding
}
}
(2)配置过滤器EncodingFilter
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>com.zgy.filter.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
(3)创建名为BookServlet类,该类继承了HttpServlet,是处理添加图书信息请求的Servlet对象。
package com.zgy.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 处理get请求
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter(); //获取PrintWriter
String id = request.getParameter("id");//获取图书编号
String name = request.getParameter("name");//获取图书名称
String author = request.getParameter("author");//获取图书作者
String price = request.getParameter("price");
out.print("<h2>图书信息添加成功</h2><hr>");
out.print("图书编号:"+id+"</br>");
out.print("图书名称:"+name+"</br>");
out.print("图书作者:"+author+"</br>");
out.print("图书价格:"+price+"</br>");
out.flush();
out.close();
}
}
PS:
在Java Web开发中,通常情况下,Servlet所处理的请求是GET或POST,所以可以在doGet()方法中调用doPost()方法,把业务代码写到doPost()方法中,也可以在doPost()方法中调用doGet()方法,把业务处理代码写到doGet()方法中,无论Servlet接受的请求类型是GET还是POST,Servlet都会对其进行处理
(4)配置BookServlet
<servlet>
<servlet-name>BookServlet</servlet-name>
<servlet-class>com.zgy.servlet.BookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookServlet</servlet-name>
<url-pattern>/BookServlet</url-pattern>
</servlet-mapping>
(5)创建book.jsp页面,他是程序中的主页,该页面主要用于放置添加图书信息的表单。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>book page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="BookServlet" method="post">
<table align="center" border="1" width="350">
<tr>
<td class="2" align="center" colspan="2">
<h2>添加图书信息</h2>
</td>
</tr>
<tr>
<td align="right">图书编号:</td>
<td>
<input type="text" name="id">
</td>
</tr>
<tr>
<td align="right">图书名称:</td>
<td>
<input type="text" name="name">
</td>
</tr>
<tr>
<td align="right">图书作者:</td>
<td>
<input type="text" name="author">
</td>
</tr>
<tr>
<td align="right">图书价格:</td>
<td>
<input type="text" name="price">
</td>
</tr>
<tr>
<td class="2" align="center" colspan="2">
<input type="submit" value="添 加"/>
</td>
</tr>
</table>
</form>
</body>
</html>
JSP详细篇——Servlet过滤器和监听器(二)