为什么我的servlet 文件夹过滤器web.xml中的配置参数传不进去呢

UFOUFO777 2010-02-02 09:05:08
ListFilter.java代码如下
package common;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.*;
public class ListFilter implements Filter
{
private FilterConfig filterConfig = null;
private String type="";
private String Log_Page="/error.jsp";
public void init(FilterConfig filterConfig) throws ServletException
{
this.filterConfig = filterConfig;
//获取初始化参数
this.type = filterConfig.getInitParameter("type");
}
public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse res=(HttpServletResponse)response;
HttpSession session=req.getSession();
String membertype="";
if(session.getAttribute("membertype")!=null)
membertype=(String)session.getAttribute("membertype");
if(membertype.equals(type))
{
//通过,继续处理。
chain.doFilter(request,response);
}
else
{
res.sendRedirect(Log_Page);
}
}
public void destroy()
{
this.type = null;
this.Log_Page = null;
this.filterConfig = null;
}
}

web.xml代码如下
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<!-- JSPC servlet mappings start -->

<!-- JSPC servlet mappings end -->
<filter>
<filter-name>ListFilter</filter-name>
<filter-class>common.ListFilter</filter-class>
<init-param>
<param-name>type</param-name>
<param-value>业务员</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>ListFilter</filter-name>
<url-pattern>/list/*</url-pattern>
</filter-mapping>


<!-- SESSION TIME OUT -->
<session-config>
<session-timeout>-1</session-timeout>
</session-config>

<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/rot</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>


</web-app>
运行的时候,不论是否符合条件,一律指向了error.jsp,这说明,if(membertype.equals(type))始终是非。如果我把servlet中的 if(membertype.equals(type))换成 if(membertype.equals("业务员"))就能正常过滤了,这说明是type没有把参数值传到servlet进去,哪儿的原因呢,请高手指点如果我把servlet中的 if(membertype.equals(type))换成 if(membertype.equals("业务员"))就能正常过滤了,这说明是type没有把参数值传到servlet进去,哪儿的原因呢,请高手指点
...全文
140 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
final_xt 2010-02-03
  • 打赏
  • 举报
回复
貌似就是空格的原因。。。。
andyan_2008 2010-02-03
  • 打赏
  • 举报
回复
在init函数打印或者设置断点,看下init函数中是否取到了type的值
doniks 2010-02-03
  • 打赏
  • 举报
回复
最好不要用汉字,也许会有乱码的问题
UFOUFO777 2010-02-03
  • 打赏
  • 举报
回复
原因找到了,是汉字的原因,我把TYPE值的汉字变成数字就成功了
UFOUFO777 2010-02-02
  • 打赏
  • 举报
回复
应该不是空格的原因,你看其它的后面全有空格的,在我的记事本中,程序也是没有空格的,出现空格应该是本论坛的原因
zhj92lxs 2010-02-02
  • 打赏
  • 举报
回复
是不是<param-name>type </param-name> type后面有空格,
其他的好像真的没什么错了
UFOUFO777 2010-02-02
  • 打赏
  • 举报
回复
我在listfilt.java中加入sym.out.println(type)显示空白,估计是null
zhj92lxs 2010-02-02
  • 打赏
  • 举报
回复
你可以把type输出来看看,是null还其他的值
UFOUFO777 2010-02-02
  • 打赏
  • 举报
回复
membertype,是登陆的时候从用户数据库中传来的,是会员类型,比如分成“业务员” “经理”之类的,然后登陆的时候根据不同的类别进不同的文件夹,if(membertype.equals(type))换成 if(membertype.equals("业务员"))就能正常过滤了说明membertype这个参数是正常的了。现在主要是type这个值没有取到
zhj92lxs 2010-02-02
  • 打赏
  • 举报
回复
membertype
这个你是什么时候设置的呢

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧