Intercept the JSP page and check whether the page is logged on

Source: Internet
Author: User

Recently, when using struts2 for a project, you need to intercept JSP to verify whether it has been logged in. However, the filter chain of struts2 can only filter action. Later, we can add the javax. servlet. Filter Implementation class filters in web. xml to Filter JSP. See the code Filter Implementation class [java] <span style = "font-size: 18px;"> package com. common; 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. httpServlet; import javax. servlet. http. httpServletRequest; import Javax. servlet. http. httpServletResponse; public class LoginFilter extends HttpServlet implements Filter {private static final long serialVersionUID = 1L; @ Override public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, servletException {HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) respo Neuron; String path = req. getContextPath (); String indexPath = req. getScheme () + ": //" + req. getServerName () + ":" + req. getServerPort () + path + "/pages/login. jsp "; if (req. getRequestURI (). endsWith ("login. jsp ") {chain. doFilter (request, response); return;} Object loginuser = req. getSession (). getAttribute ("<strong> <span style =" color: # ff0000; "> loginSession </span> </strong>"); if (loginuser = null) {res. sendRedir Ect (indexPath); return;} chain. doFilter (request, response) ;}@ Override public void init (FilterConfig arg0) throws ServletException {// TODO Auto-generated method stub }</span> logon method: [java] <span style = "font-size: 18px;"> public String login () {Login login = new Login ();//....... verify the logon information ActionContext ctx = ActionContext. getContext (); Map <String, Object> session = ctx. getSession (); session. put ("<Strong> <span style =" color: # ff0000; "> loginSession </span> </strong>", login); return "success ";} </span> web. xml configuration [html] <filter> <filter-name> setlog </filter-name> <filter-class> com. common. loginFilter </filter-class> </filter> <filter-mapping> <filter-name> setlog </filter-name> <url-pattern>/pages/home. jsp </url-pattern> <url-pattern>/pages/channel/* </url-pattern> <url-pattern>/pages/content/* </url-pa Ttern> <url-pattern>/pages/product/* </url-pattern> <url-pattern>/pages/vodtask/* </url-pattern> </filter- mapping> note, if the framework is used on the page, the session may be timed out and displayed on the logon page. The logon page is displayed on a subpage of the framework. To solve this problem, add the following code to the logon page. [Html] <span style = "font-size: 18px;"> <script language = "javascript"> if (top! = Window) top. location. href = window. location. href; </script> </span>

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.