"Filter" solves the coding problem of post transmission with filter filters and simplifies parameter passing by using EL Expression

Source: Internet
Author: User

The encoding problem of post delivery, there are two ways to fix it, one is to modify the Tomcat configuration file, this way I object, because you can not guarantee that your project can run in any tomcat, of course, if something happens later, processing changes to the Tomcat configuration file, nothing else , it is not too late to modify, and the other is to use before the page is evaluated:

Request.setcharacterencoding ("Utf-8");  
It's nothing like this, just a line of code? However, in the current JSP programming constantly stressed that the page must never appear in the back-end code trend, you can not use the "JSP" using post and get to correctly pass form information and garbled problem (click on the link) to get the value, you have to use more powerful technology. Here's an example of how to use the filter filter to solve the encoding problem of post delivery and simplify parameter passing with El expressions.


I. BASIC OBJECTIVES

Or a base can not be the basis of the page, do not tell others that they have learned the server Web programming,

As shown below, set a filter to ensure that post delivery is normal, but the Get method can only keep the English pass is normal, do not have obsessive-compulsive disorder here, after all, generally do not get to pass important information, are some numbers, English and so on, with the small function of paging.



II. Basic Preparation

The network directory structure diagram is shown below, or the jar package that is put into the servlet,


To avoid using JSP code on the page, filter filters, and do not have to take each value each time:

Request.setcharacterencoding ("Utf-8");

So write a Encodingfilter.java set all the pages to go through this Java, equivalent to the role of the macro compilation of the Web site, set the filter in the Web. xml file, do not understand the filter, you can refer to my previous written "Filter" Interceptor filter (Click to open link)

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "><filter><filter-name>encodingfilter</ Filter-name><filter-class>encodingfilter</filter-class></filter><filter-mapping> <filter-name>encodingfilter</filter-name><url-pattern>/*</url-pattern></ Filter-mapping></web-app>


third, the production process

1, the filter code is as follows:

Import java.io.*;import javax.servlet.*;import javax.servlet.http.*;p ublic class Encodingfilter implements Filter {// The config variable is also not required, and the constructor and destructor can be null @overridepublic void Destroy () {//TODO auto-generated method stub}//equivalent to the execution of each URL before All the code to go through this method until you read Chain.dofilter (request, response), so you don't have to worry about the encoding problem with post @overridepublic void DoFilter ( ServletRequest request, Servletresponse Response,filterchain chain) throws IOException, servletexception {//TODO Auto-generated method Stubrequest.setcharacterencoding ("Utf-8"); Chain.dofilter (request, response);} @Overridepublic void init (Filterconfig filterconfig) throws Servletexception {//TODO auto-generated method stub}}

2, form.jsp Nothing special, is a common can not be more common forms:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" Utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
3. Expressiongetpost.jsp can use El expression to simplify parameter passing, El's Chinese name is the expression language, El expression must be used in JSP2.2 version, but notice that our web. XML contains the value of the version is equal to 2.5, so you can use without scruple, farewell:

Request.getparameter ("xx");
At the same time can be like PHP similar to a ${} will be finished. But notice that the check box is different from the other way around, and what the check box passes over is an array. Other things are just a normal value.

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "    pageencoding=" Utf-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >
The EL expression can also have the following value methods:


Note, however, that the El expression can only be used to take a value, and cannot be set to the value of an item in the session container like PHP.

Filter solves the encoding problem of post delivery with filter filter and simplifies parameter passing using El expressions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.