過濾器Filter的使用(以登陸爲例子)

使用過濾器步驟:java

(1)在web.xml文件中添加過濾器(如下例子是過濾多個請求)web

<!-- 用戶登陸過濾 -->
  <filter>
      <filter-name>loginFilter</filter-name>
      <filter-class>com.learning.filter.loginFilter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>loginFilter</filter-name>
      <url-pattern>/classes/classesPageInfo.do</url-pattern>
  </filter-mapping>
  <filter-mapping>
      <filter-name>loginFilter</filter-name>
      <url-pattern>/note/notePageInfo.do</url-pattern>
  </filter-mapping>
  <filter-mapping>
      <filter-name>loginFilter</filter-name>
      <url-pattern>/ask/asksPageInfo.do</url-pattern>
  </filter-mapping>

 

 (2)過濾器類session

package com.learning.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;

public class loginFilter implements Filter{
    private String encoding;

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp,
            FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        HttpSession session = request.getSession();
        String loginName = (String) session.getAttribute("loginName");
        //判斷當前用戶是否已經登陸,若未登陸跳到登陸頁面
        if (loginName == null || loginName=="") {
            String goUrl = request.getRequestURL().toString() + "?" +request.getQueryString().toString();
            session.setAttribute("goUrl", goUrl);
            request.getRequestDispatcher("/user/log.jsp").forward(request, response);
            return ;
        } else {
            filterChain.doFilter(req, resp);
        }    
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        encoding = "utf-8";
    }

}

注:這個主要的做用是將過濾前的地址記錄下來,登陸成功以後跳轉到過濾前的頁面app

String goUrl = request.getRequestURL().toString() + "?"+request.getQueryString().toString();
session.setAttribute("goUrl", goUrl);

 

(3)登陸頁面的form表單代碼jsp

<form action="<%=path%>person/dologin.do" name="loginform"
                            accept-charset="utf-8" id="login_form" class="loginForm"
                            method="post">
                            <input type="hidden" name="did" value="0" /> <input type="hidden"
                                name="to" value="log" />
                            <div class="uinArea" id="uinArea">
                                <label class="input-tips" for="u">帳號:</label>
                                <div class="inputOuter" id="uArea">

                                    <input type="text" id="u" name="username" class="inputstyle" />
                                </div>
                            </div>
                            <div class="pwdArea" id="pwdArea">
                                <label class="input-tips" for="p">密碼:</label>
                                <div class="inputOuter" id="pArea">

                                    <input type="password" id="p" name="password"
                                        class="inputstyle" />
                                </div>
                            </div>
                            <div style="padding-left:50px;margin-top:20px;">
                                <input type="submit" value="登 錄" style="width:150px;"
                                    class="button_blue" />
                            </div>
                        </form>

 

(4)登陸表單提交後經過action跳到控制層,並根據狀況跳到不一樣的地方ide

// 用戶登陸
    @RequestMapping("/dologin.do")
    public String doLogin(@RequestParam("username") String username,
            @RequestParam("password") String password, HttpSession session) {
        Map param = new HashMap();
        param.put("loginName", username);
        param.put("loginPwd", password);
        // 判斷用戶是否存在
        User user = personServiceImpl.findUserInfo(param);
        session.setAttribute("loginName", user.getLoginName());
        session.setAttribute("userPower", user.getUserPower());
        String goUrl = (String) session.getAttribute("goUrl");
        if (goUrl != null) {
            return "redirect:" + goUrl;
        }
        return "redirect:/mainpage/tomainpage.do";
    }
相關文章
相關標籤/搜索