Servlet Filter filter Determines whether a user logs on (JSP page) __js

Source: Internet
Author: User
Tags tomcat server

filter servlet class:

Package servlet;
Import java.io.IOException;

Import Java.io.PrintWriter;
Import Javax.servlet.Filter;
Import Javax.servlet.FilterChain;
Import Javax.servlet.FilterConfig;
Import Javax.servlet.RequestDispatcher;
Import javax.servlet.ServletException;
Import Javax.servlet.ServletRequest;
Import Javax.servlet.ServletResponse;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;


	Import javax.servlet.http.HttpSession;

		 public class Loginfilter implements Filter {private static final String Logon_uri = "Logon_uri";

		 private static final String Register_uri = "Register_uri";

		 Private String logon_page;

		 Private String register_page; public void Destroy () {} public void Dofilter (ServletRequest request, servletresponse response, Filterchain C
		  Hain) throws IOException, servletexception {httpservletrequest req = (httpservletrequest) request; HttpServletResponse resp = (HttPservletresponse) response;
		  Resp.setcontenttype ("text/html;");
		  Resp.setcharacterencoding ("Utf-8");
		 HttpSession session = Req.getsession ();
		  PrintWriter out = Resp.getwriter ();
		  Gets the URI String Request_uri = Req.getrequesturi () of the user request;
		  Gets the context path of the Web application String Ctxpath = Req.getcontextpath ();
		  Remove the context path and get the remaining part of the path String uri = request_uri.substring (Ctxpath.length ());
		   Determine if the user is accessing the login page if (uri.equals (logon_page) | | uri.equals (register_page)) {Chain.dofilter (request, response);
		  Return else {//If the access is not a login page, determine if the user has logged on if (null!= session.getattribute ("Curuser") && "!= session.
		    GetAttribute ("Curuser")) {Chain.dofilter (request, response);
		   Return else {String msg = "You are not logged in."
	           ";
	             String url = "Errorer";
	             Req.setattribute ("Err", msg);
	  		     RequestDispatcher dis = req.getrequestdispatcher (URL);
		   Dis.forward (req, resp);
		  }  Out.println ("<script language=/" javascript/">"//+ "parent.location.href=" "+//ctxpath + logon_page
		   + "'"//+ "</script>");
		  Return 
		  }} public void init (Filterconfig config) throws servletexception {//TODO auto-generated a stub
		  Get the login page and the URI logon_page = Config.getinitparameter (Logon_uri) from the deployment descriptor;
		  Register_page = Config.getinitparameter (Register_uri);
		  System.out.println (Logon_page);
		  if (null = = Logon_page | | | null = register_page) {throw new Servletexception ("No login page or homepage found"); }
		 }

	}
To modify the Web core configuration file:

Note: The tomcat server needs to be restarted after saving

<filter>
    <filter-name>loginFilter</filter-name>
    <filter-class>servlet. loginfilter</filter-class>
    <init-param>
          <param-name>LOGON_URI</param-name>
          <param-value>/login.jsp</param-value>
    </init-param>
    <init-param>
          < param-name>register_uri</param-name>
          <param-value>/register.jsp</param-value>
    </init-param>
  </filter>
<filter-mapping>
	<filter-name> charseterencodingfilter</filter-name>
	<url-pattern>/*</url-pattern>
</ filter-mapping>
 <filter-mapping>
   <filter-name>loginFilter</filter-name>
   <url-pattern>*.jsp</url-pattern>
  </filter-mapping>
Errorer Error Information Processing class:
Package servlet;
Import java.io.IOException;

Import Java.io.PrintWriter;
Import Javax.servlet.RequestDispatcher;
Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;

Import Javax.servlet.http.HttpServletResponse; /** * Servlet Implementation Class ERROR */public class Errorer extends HttpServlet {private static final long seria

	Lversionuid = 1L; protected void ProcessRequest (HttpServletRequest request, httpservletresponse response) throws Servletexception, IO
			        Exception {response.setcontenttype ("text/html;charset=utf-8");
			        PrintWriter out = Response.getwriter ();
			        String url= "login.jsp";
			            try {out.println (" 

Effect Display:



Related Article

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.