Getting Started with filter

Source: Internet
Author: User

Testfilter.java

Package com.cdsxt.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;

/**
* @author Administrator
* Test Filter
*
* Implementation of a specific interface
*/
public class Testfilter implements filter{


The filter's Init method is loaded by default as the server starts to load

@Override
public void init (Filterconfig filterconfig) throws Servletexception {
TODO auto-generated Method Stub
System.out.println ("Testfiler.init ()");

}

@Override
public void DoFilter (ServletRequest request, Servletresponse Response,filterchain chain) throws IOException, servletexception {
TODO auto-generated Method Stub
System.out.println ("Testfiler.dofilter ()");
Chain.dofilter (request, response);
System.out.println ("Testfiler1.dofilter ()");
}

@Override
public void Destroy () {
TODO auto-generated Method Stub
System.out.println ("Testfiler.destroy ()");
}

}

Testfilter1.java

Package com.cdsxt.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;
Import Javax.servlet.http.HttpServletRequest;

/**
* @author Administrator
* Test Filter
*
* Implementation of a specific interface
*/
public class TestFilter1 implements filter{


Filter is loaded by default

@Override
public void init (Filterconfig filterconfig) throws Servletexception {
TODO auto-generated Method Stub
System.out.println ("testfiler.init1 ()");

}

@Override
public void DoFilter (ServletRequest request, Servletresponse Response,filterchain chain) throws IOException, servletexception {
TODO auto-generated Method Stub
System.out.println ("Testfiler.dofilter1 ()");
SYSTEM.OUT.PRINTLN (Request instanceof HttpServletRequest);

}

@Override
public void Destroy () {
TODO auto-generated Method Stub
System.out.println ("testfiler.destroy1 ()");
}

}

Loginfilter.java

Package com.cdsxt.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;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;

Import Com.cdsxt.po.Users;


public class Loginfilter implements filter{

@Override
public void init (Filterconfig filterconfig) throws Servletexception {
TODO auto-generated Method Stub

}

@Override
public void DoFilter (ServletRequest request, servletresponse response,
Filterchain chain) throws IOException, Servletexception {
/**
* If the request is logged in, then go directly or jump to the login page
*/
HttpServletRequest req = (httpservletrequest) request;
HttpServletResponse resp = (httpservletresponse) response;
Get the session to determine if there are users in the session
HttpSession session = Req.getsession ();

String uri = Req.getrequesturi ();

Object users = Session.getattribute ("admin");
Logged-On
if (users!=null && users instanceof users) {
Chain.dofilter (request, response);
}else{
if (Uri.endswith ("login.jsp") | | | uri.endswith ("logincontroller") | | uri.endswith (". css") | | uri.endswith (". js") | | Uri.endswith (". jpg")) {
Chain.dofilter (request, response);
}else{
Resp.sendredirect (Req.getcontextpath () + "/views/login.jsp");
}
}
}

@Override
public void Destroy () {
TODO auto-generated Method Stub

}

}

Xml

<?xml version= "1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xmlns : web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "id=" webapp_id "version=" 2.5 ">
<display-name>LoginSystem</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>


<!--

The order in which the filter is executed is related to the order of the declared filter-mapping from the top down
-
<!--<filter>
<filter-name>test-filter</filter-name>
<filter-class>com.cdsxt.filter.TestFilter</filter-class>
</filter>

<filter>
<filter-name>test-filter1</filter-name>
<filter-class>com.cdsxt.filter.TestFilter1</filter-class>
</filter>
<filter-mapping>
<filter-name>test-filter1</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>test-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>login-filter</filter-name>
<filter-class>com.cdsxt.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>login-filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>logout-filter</filter-name>
<filter-class>com.cdsxt.filter.LogoutFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>logout-filter</filter-name>
<url-pattern>/logout</url-pattern>
</filter-mapping>

<servlet>
<description></description>
<display-name>UsersController</display-name>
<servlet-name>UsersController</servlet-name>
<servlet-class>com.cdsxt.controller.UsersController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UsersController</servlet-name>
<url-pattern>/users/usersController</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LoginController</servlet-name>
<servlet-class>com.cdsxt.controller.LoginController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginController</servlet-name>
<url-pattern>/login/loginController</url-pattern>
</servlet-mapping>
</web-app>

Getting Started with filter

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.