• 周六. 10 月 12th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Java Web Learning (10) — El expression / filter filter

King Wang

1 月 3, 2022

JavaWeb Study ( Ten )—EL expression /Filter filter

01 EL expression

**EL Full name Expression Language.**EL The main role :
1、 get data
EL Expressions are mainly used to replace JSP Script expressions in the page , From various types of web Search in domain java object 、 get data .

grammar :${ identifier }

​ EL When the expression statement is executed , Would call pageContext.findAttribute Method , Use the identifier as the keyword , Respectively from the page、request、session、application Find the corresponding object in the four fields , If found, the corresponding object will be returned , Return if not found ”” ( Be careful , No null, It’s an empty string ).

2、 Perform an operation
utilize EL The expression can be in JSP Some basic relational operations are performed in the page 、 Logical and arithmetic operations , In the JSP Complete some simple logic operations in the page .

grammar :${user==null}
3、 obtain web Develop common objects
EL The expression language defines 11 A hidden object , Using these hidden objects, you can easily get web Some common objects in development , And read the data of these objects .

grammar :${ Implicit object name }: Gets a reference to the object .

​ Implicit objects include :pageContext,pageScope( With scope The at the end generally represents the Map object ),requestScope,sessionScope,applicationScope,param( Represents a… That holds all the request parameters Map object ),paramValues( Represents a… That holds all the request parameters Map object , It’s for a request parameter , Back to a string[]),header,headerValues,cookie,initParam

4、 call Java Method
EL Expressions allow users to develop custom EL function , In the JSP Page through EL Expression call Java Class method .

​ grammar :${prefix:method(params)}

Be careful : stay EL Expressions can only be called Java Class static methods , This Java The static method of the class needs to be in the TLD The document describes , To be EL Expression call .


02 Filter filter

Filter Also called a filter ,WEB Developers through Filter technology , Yes web All of the server management web resources : for example Jsp, Servlet, Static picture file or static html File etc. Intercept , So as to realize some special functions . for example Realization URL Level of access control 、 Filter sensitive words 、 Compress response information and other advanced functions .


Filter The interception principle of :

​ Filter There is one in the interface doFilter Method , When we write Filter, And configure which web After resources are intercepted ,WEB Every time the server calls web Resources service Before method , Metropolis First call once filter Of doFilter Method .web The server Calling doFilter When the method is used , Will deliver a filterChain object Come in ,filterChain Object also provides a doFilter Method , Developers can decide whether to call this method or not based on their needs . call filterChian Object’s doFilter Method ,web The server will check FilterChain Whether there is any filter, If there is , Then call the 2 individual filter, without , Then call the target resource . contrary , If the method is not called ,web Resources will not be accessed .


How to create Filter?

​ establish Filter Must be realized Filter Interface , Realization Filter Interface must override three methods :

​ 1. Filter initialization :init(FilterConfig filterConfig);

​ 2. rewrite doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain);Filter Medium doFilter() Methods mainly write the code to realize the filter function ,doFillter Method can be called by a requirement filterChain Of doFilter Method release .

​ 3. Unregister filter :destroy();

​ besides , Need to be in xml File configuration needs to be filtered web resources .

public class MyFilter implements Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException {

System.out.println("MyFilter Initialize the ");
}
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {

// stay doFilter How to write filters in 
// Unified coding , Avoid random code problems 
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
// Filter users whose user names are sensitive strings , The string is not available 
if (req.getParameter("username")!=null){

System.out.println(req.getParameter("username"));
if (req.getParameter("username").equals(" Yellow wager poison ")){

resp.getWriter().println(" The string is illegal , Please reset ");
}
}
System.out.println(" Execution method filterChain.doFilter(req,resp); front ===========");
// With only one filter , This code will let the target resource release the request 
filterChain.doFilter(req,resp);
System.out.println(" Execution method filterChain.doFilter(req,resp); after ===========");
}
@Override
public void destroy() {

System.out.println("MyFilter Cancelled ");
}
}

Filter Life cycle of

Filter Created and destroyed by WEB The server is responsible for .web Applications Startup time ,web Server will establish Filter Instance object of , And call it init Method , Complete the initialization function of the object , So as to make preparations for interception of subsequent user requests ,filter Object will only be created once ,init The method will only be executed once . The logoff of the filter is turned off as the server is shut down . besides ,Web The container can also call destroy Method The destruction Filter.destroy Method in Filter In the life cycle of Only once . stay destroy In the method , Sure Release Resources used by filters .


Filter Registration and mapping for

<filter>
<filter-name>myFilter</filter-name>
<filter-class>com.hooi.filter.MyFilter</filter-class>
</filter>
<!--url Represents that the request needs to be filtered Web resources
/* wildcard , All requests are filtered
/regist.do Only this request will be filtered
When filtering the log off and initialization with Tomcat Containers start or close together
-->
<filter-mapping>
<filter-name>myFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

发表回复