Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2321254
  • 博文数量: 252
  • 博客积分: 5472
  • 博客等级: 大校
  • 技术积分: 3107
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-17 18:39
文章分类

全部博文(252)

文章存档

2012年(96)

2011年(156)

分类: 系统运维

2011-12-14 15:21:19

让用户在登录后直接进入先前的页面  我们要将登陆页面发送给客户端之前保存用户先前访问页面的URL
解决方法:
[1]  获取用户的请求URL和查询字符串  并保存到请求对象中 然后将请求转发 (forward)给登陆页面
  1.                String strQuery=httpReq.getQueryString();
  2.                 if(null!=strQuery)
  3.                 {
  4.                     request_uri=request_uri+"?"+strQuery;
  5.                 }
  6.                 httpReq.setAttribute("origin_uri",request_uri);
  7.                 
  8.                 //将用户请求转发给登录页面。

  9.                 RequestDispatcher rd=httpReq.getRequestDispatcher(logon_page);
  10.                 rd.forward(httpReq,httpResp);

[2]  在登录页面中 需要包含一个隐藏的输入域 他的值是用户先前访问的请求URL

  1. <input type="hidden" name="origin_uri" value="${requestScope.origin_uri}">

 

1  logon.jsp

  1. <%@ page contentType="text/html;charset=GB2312" %>
  2. <form method="post" action="logon.jsp?action=logon">
  3.     <table>
  4.         <tr>
  5.             <td>用户名:</td>
  6.             <td><input type="text" name="name"></td>
  7.         </tr>
  8.         <tr>
  9.             <td>密码:</td>
  10.             <td><input type="password" name="password"></td>
  11.         </tr>
  12.         <tr>
  13.             <input type="hidden" name="origin_uri" value="${requestScope.origin_uri}">
  14.         </tr>
  15.         <tr>
  16.             <td><input type="reset" value="重填"></td>
  17.             <td><input type="submit" value="提交"></td>
  18.         </tr>
  19.     </table>
  20. </form>

2  LogonFilter.java

 

  1. package org.sunxin.ch16.filter;

  2. import java.io.*;
  3. import javax.servlet.*;
  4. import javax.servlet.http.*;

  5. public class LogonFilter implements Filter
  6. {
  7.     private static final String LOGON_URI="logon_uri";
  8.     private static final String HOME_URI="home_uri";
  9.     
  10.     private String logon_page;
  11.     private String home_page;
  12.     
  13.     public void init(FilterConfig filterConfig) throws ServletException
  14.     {
  15.         //从部署描述符中获取登录页面和首页的URI。

  16.         logon_page=filterConfig.getInitParameter(LOGON_URI);
  17.         home_page=filterConfig.getInitParameter(HOME_URI);
  18.         
  19.         if(null==logon_page || null==home_page)
  20.             throw new ServletException("没有指定登录页面或主页!");
  21.     }
  22.    
  23.     public void doFilter(ServletRequest request,
  24.                          ServletResponse response,
  25.                          FilterChain chain)
  26.                   throws IOException, ServletException
  27.     {
  28.         //将请求对象和响应对象的类型转换为

  29.         //HttpServletRequest和HttpServletResponse。

  30.         HttpServletRequest httpReq=(HttpServletRequest)request;
  31.         HttpServletResponse httpResp=(HttpServletResponse)response;
  32.         HttpSession session=httpReq.getSession();
  33.         
  34.         //得到用户的请求URI。

  35.         String request_uri=httpReq.getRequestURI();
  36.         //得到Web应用程序的上下文路径。

  37.         String ctxPath=httpReq.getContextPath();
  38.         //去除上下文路径,得到剩余部分的路径。

  39.         String uri=request_uri.substring(ctxPath.length());
  40.         
  41.         //判断用户访问的是否是登录页面

  42.         if(logon_page.equals(uri))
  43.         {
  44.             //如果是登录页面,则通过查看是否有附加的请求参数,来判断用户

  45.             //是访问登录页面,还是提交登录信息。

  46.             String strLogon=httpReq.getParameter("action");
  47.             
  48.             if("logon".equals(strLogon))
  49.             {
  50.                 //如果是提交登录信息,则对用户进行验证。

  51.                 String name=httpReq.getParameter("name");
  52.                 String password=httpReq.getParameter("password");
  53.                 
  54.                 if("zhangsan".equals(name) && "1234".equals(password))
  55.                 {
  56.                     //验证通过后,在Session对象中设置isLogon属性为true。

  57.                     session.setAttribute("isLogon","true");
  58.                     //在Session对象中保存用户名。

  59.                     session.setAttribute("user",name);
  60.                     
  61.                     //从请求对象中取出用户先前访问页面的URI。

  62.                     String origin_uri=httpReq.getParameter("origin_uri");
  63.                     //如果origin_uri不为空,则将客户端重定向到用户先前访问的页面,

  64.                     //否则将客户端重定向到首页。

  65.                     if(null!=origin_uri && !"".equals(origin_uri))
  66.                     {
  67.                         httpResp.sendRedirect(origin_uri);
  68.                     }
  69.                     else
  70.                     {
  71.                         httpResp.sendRedirect(ctxPath+home_page);
  72.                     }
  73.                     return;
  74.                 }
  75.                 else
  76.                 {
  77.                     //如果验证失败,则从请求对象中获取用户先前访问页面的URI。

  78.                     //如果该URI存在,则再次将它作为origin_uri属性的值保存

  79.                     //到请求对象中。

  80.                     String origin_uri=httpReq.getParameter("origin_uri");
  81.                     if(null!=origin_uri && !"".equals(origin_uri))
  82.                     {
  83.                         httpReq.setAttribute("origin_uri",origin_uri);
  84.                     }
  85.                     httpResp.setContentType("text/html;charset=GB2312");
  86.                     PrintWriter out=httpResp.getWriter();
  87.                     out.println("

    用户名或密码错误,请重新输入。

    "
    );
  88.                     RequestDispatcher rd=httpReq.getRequestDispatcher(logon_page);
  89.                     rd.include(httpReq,httpResp);
  90.                     return;
  91.                 }
  92.             }
  93.             else
  94.             {
  95.                 //如果用户不是提交登录信息,则调用chain.doFilter()方法,

  96.                 //调用登录页面。

  97.                 chain.doFilter(request, response);
  98.                 return;
  99.             }
  100.         }
  101.         else
  102.         {
  103.             //如果访问的不是登录页面,则判断用户是否已经登录。

  104.             String isLogon=(String)session.getAttribute("isLogon");
  105.             if("true".equals(isLogon))
  106.             {
  107.                chain.doFilter(request, response);
  108.                return;
  109.             }
  110.             else
  111.             {
  112.                 //如果用户没有登录,则将用户的请求URI作为origin_uri属性的值

  113.                 //保存到请求对象中。

  114.                 String strQuery=httpReq.getQueryString();
  115.                 if(null!=strQuery)
  116.                 {
  117.                     request_uri=request_uri+"?"+strQuery;
  118.                 }
  119.                 httpReq.setAttribute("origin_uri",request_uri);
  120.                 
  121.                 //将用户请求转发给登录页面。

  122.                 RequestDispatcher rd=httpReq.getRequestDispatcher(logon_page);
  123.                 rd.forward(httpReq,httpResp);
  124.                 return;
  125.             }
  126.         }
  127.     }
  128.     
  129.     public void destroy(){}
  130. }

 

3   home.jsp

 

  1. <%@ page contentType="text/html;charset=gb2312" %>

  2. ${sessionScope.user},欢迎你。

 

4   web.xml

 

  1. <filter>
  2.         <filter-name>LogonFilter</filter-name>
  3.         <filter-class>org.sunxin.ch16.filter.LogonFilter</filter-class>
  4.         
  5.         <init-param>
  6.             <param-name>logon_uri</param-name>
  7.             <param-value>/logon.jsp</param-value>
  8.         </init-param>
  9.         <init-param>
  10.             <param-name>home_uri</param-name>
  11.             <param-value>/home.jsp</param-value>
  12.         </init-param>
  13.     </filter>
  14.     
  15.     <filter-mapping>
  16.         <filter-name>LogonFilter</filter-name>
  17.         <url-pattern>/*</url-pattern>
  18.     </filter-mapping>
阅读(3113) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~