Topic: Servlet Filter做验证 转向功能不起作用

  Print this page

1.Servlet Filter做验证 转向功能不起作用 Copy to clipboard
Posted by: blueoxygen
Posted on: 2005-05-09 18:03

配置等都没有问题 因为受限目录确实得到了保护 但是,问题时,无法访问的时候我设计的是转向另外一叶,但是现在情况时一片空白,就是阻止访问之后没有动作了 请问原因
codes r here:

package com.jspdev;
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 SignonFilter implements Filter{
final private String LOGIN_PAGE= "http://www.sina.com.cn";
protected FilterConfig filterConfig;
/* (non-Javadoc)
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
public void init(FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
this.filterConfig = config;
}

/* (non-Javadoc)
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest hreq = (HttpServletRequest)req;
HttpServletResponse hres = (HttpServletResponse)res;
String isLogin = "";
HttpSession session = hreq.getSession();
try{
isLogin = session.getAttribute("isLogin").toString();
if(isLogin.equals("ture"))
{
System.out.println("access valid");
chain.doFilter(req,res);
}
else{
hres.sendRedirect(LOGIN_PAGE); //就是此处转向的
}
}
catch(Exception e)
{
e.printStackTrace();
}
}

/* (non-Javadoc)
* @see javax.servlet.Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
this.filterConfig = null;
}
public void setFilterConfig(final FilterConfig filterConfig)
{
this.filterConfig = filterConfig;
}

2.Re:Servlet Filter做验证 转向功能不起作用 [Re: blueoxygen] Copy to clipboard
Posted by: bluepure
Posted on: 2005-05-09 19:30

代码有问题:

修改一下:


public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest hreq = (HttpServletRequest)req;
HttpServletResponse hres = (HttpServletResponse)res;
String isLogin = "";
HttpSession session = hreq.getSession();
try{
isLogin = String.valueOf(session.getAttribute("isLogin"));//你那样写会有空指针错误。
if(isLogin.equals("ture"))
{
System.out.println("access valid");
chain.doFilter(req,res);
}
else{
hres.sendRedirect(LOGIN_PAGE); //就是此处转向的
}
}
catch(Exception e)
{
e.printStackTrace();
}
}


3.Re:Servlet Filter做验证 转向功能不起作用 [Re: blueoxygen] Copy to clipboard
Posted by: blueoxygen
Posted on: 2005-05-10 09:12

3q very much...
The filter goes on right way...


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923