Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use chain.doFilter in Java filter

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Java filter in the use of chain.doFilter is how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

Chain.doFilter in filter uses the function of chain.doFilter

1. Generally speaking, filter is a chain, and there are several configured in web.xml. Linked together one by one

Request-> filter1-> filter2-> filter3->... . -> request resource.

2.chain.doFilter forwards the request to the next filter in the filter chain. If there is no filter, it is the resource you requested.

Give examples to illustrate

1.input.jsp is used to submit input: when submitted, the filter detects name and age.

two。 If normal, it will be submitted to output.jsp. If not, it will be submitted to erroroutput.jsp.

3. Here, there is also a filter to prevent garbled problems, which detects whether a page has character encoding set, and if not, sets it.

1.input.jsp

Name age

2.web.xml

Encodefilter encodefilter servletbean.encodefilter encoding GB2312 encodefilter / * myfilter myfilter servletbean.myfilter myfilter / output.jsp

3.encodefilter.java

Package servletbean;public class encodefilter implements Filter {public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {if (request.getCharacterEncoding () = = null) {System.out.println (encoding); request.setCharacterEncoding (encoding);} chain.doFilter (request, response); / / to the next chain} public void init (FilterConfig fConfig) throws ServletException {this.config=fConfig Encoding=fConfig.getInitParameter ("encoding"); / / get the target coding format}}

3.myfilter.java

Package servletbean;import javax.swing.JOptionPane;public class myfilter implements Filter {public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {response.setContentType ("text/html"); response.setCharacterEncoding ("GB2312"); PrintWriter out=response.getWriter (); String name= ""; String age= ""; int age1; name=request.getParameter ("name"); age=request.getParameter ("age") RequestDispatcher dispatch=request.getRequestDispatcher ("erroroutput.jsp"); if (name==null | | name== "" | | name== "" | | age==null) {JOptionPane.showMessageDialog (null, "user name and age entered incorrectly!") ; dispatch.forward (request, response); return;} else {try {age1=Integer.parseInt (age);} catch (Exception e) {/ / JOptionPane.showMessageDialog (null, "Age must be numeric!") ; dispatch.forward (request,response); return / / if it is an error page, go to erroroutput.jsp} / / this means that it is correct, that is to say, he goes back to find the next chain, but there is no more under it, so he will go to the jump page. The page to jump to is action= "output.jsp" chain.doFilter (request, response);}} understanding of chain.doFilter (req, res) in Filter

In writing code, every time I see chain.doFilter (req, res) in Filter (filter); I don't know why to add this sentence, what is its function?

It is also available under the code:

@ Override public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {ContentCachingRequestWrapper req=new ContentCachingRequestWrapper ((HttpServletRequest) request); ContentCachingResponseWrapper res=new ContentCachingResponseWrapper ((HttpServletResponse) response); long startTime = System.currentTimeMillis (); String serverUrl = req.getServletPath (); / / do not understand the meaning of chain.doFilter (req, res); long endTime = System.currentTimeMillis (); try {int status = res.getStatus (); String charset=res.getCharacterEncoding () / / response body String responsePayload = getPayLoad (res.getContentAsByteArray (), res.getCharacterEncoding ()); res.copyBodyToResponse (); if (writeLogService==null) {writeLogService= (WriteLogService) ServiceLocator.getInstance (). GetCtx (). GetBean ("writeLogServiceImpl");} writeLogService.writeLog (startTime,endTime,serverUrl,postparams,status,responsePayload,charset);} catch (Exception e) {e.printStackTrace ();}} usage

In the doFilter () method, the code before chain.doFilter () is usually a filtering operation performed on the request

The code after chain.doFilter () is usually the operation performed on response

Chain.doFiter () executes the next filter or business processor.

If chain.doFilter () is not written in the doFilter () method, the business cannot proceed.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report