JSP details-Servlet filter and listener (2), servlet listener
4. character encoding filter
In Java Web development, because the encoding format used inside the Web container does not support Chinese characters, Chinese Data Processing browser requests may contain garbled characters.
The encoding format used by Web containers is ISO-8859-1, so the ISO-8859-1 encoding format is also used in the business processing of Web applications. Although the Chinese encoding format used by the browser to submit the request is UTF-8, but the ISO-8859-1 encoding in the business processing, there will be garbled. In development, you can use a filter to process character encoding.
Example:
Add the library information and create character encoding filters to avoid Chinese garbled characters.
(1) create a character encoding Filter named EncodingFilter, which implements the javax. servlet. Filter interface and sets the Chinese character encoding format in the request in the doFilter () method.
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; // character encoding
@ 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); // sets the resquest character encoding.
Response. setContentType ("text/html; charset =" + encoding); // set the response character encoding
}
Chain. doFilter (request, response); // pass to the next Filter
}
@ Override
Public void init (FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
Encoding = filterConfig. getInitParameter ("encoding"); // get the initialization parameter 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) create a BookServlet class that inherits HttpServlet and is a Servlet object for processing the request for adding a book information.
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 {
// Process get requests
DoPost (request, response );
}
Protected void doPost (HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response. getWriter (); // get PrintWriter
String id = request. getParameter ("id"); // obtain the library id
String name = request. getParameter ("name"); // get the book name
String author = request. getParameter ("author"); // obtain the author
String price = request. getParameter ("price ");
Out. print ("
Out. print ("book no.:" + id + "</br> ");
Out. print ("book name:" + name + "</br> ");
Out. print ("author:" + author + "</br> ");
Out. print ("book price:" + price + "</br> ");
Out. flush ();
Out. close ();
}
}
PS:
In Java Web development, generally, the Servlet processes GET or POST requests. Therefore, you can call the doPost () method in the doGet () method, write the Business Code to the doPost () method, or call the doGet () method in the doPost () method to write the business processing code to the doGet () method, whether the Servlet accepts the GET or POST request type, the Servlet will process it.
(4) Configure 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) create a book. jsp page, which is the homepage of the program. This page is mainly used to place the form for adding the book information.
<% @ 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> Add Book Information
</Td>
</Tr>
<Tr>
<Td align = "right"> book no.: </td>
<Td>
<Input type = "text" name = "id">
</Td>
</Tr>
<Tr>
<Td align = "right"> book name: </td>
<Td>
<Input type = "text" name = "name">
</Td>
</Tr>
<Tr>
<Td align = "right"> author: </td>
<Td>
<Input type = "text" name = "author">
</Td>
</Tr>
<Tr>
<Td align = "right"> book price: </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>