求助!一个过滤器的问题.

michael9527 2007-07-27 01:30:37
要求:一个Login的Servlet,用来显示登陆的用户名. 再这之前,会先显示一个过滤器,它要求输入用户名,然后将用户名发送给Login,最后由Login将用户名显示出来,打出提示信息为: "欢迎"+username 这样的内容.但到了chain.doFilter(req,resp); 这步骤似乎出了些问题,代码如下:

Login:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public final class Login extends HttpServlet {

public void service(ServletRequest req,ServletResponse resp)
throws ServletException,IOException {
req.setCharacterEncoding("gb2312");
String user = req.getParameter("user");
resp.setContentType("text/html;charset=gb2312");
PrintWriter out = resp.getWriter();

out.println("欢迎您"+user);
out.close();
}
}

过滤器:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public final class FilterTest2 implements Filter {

public void init(FilterConfig parm1) throws ServletException {

}


public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
resp.setContentType("text/html;charset=gb2312");

PrintWriter out = resp.getWriter();
out.println("<html><head><title>过滤器登陆页面</title></head><body>");
out.println("<form action=Login method=get>");
out.println("<p>请输入用户名:");
out.println("<input type=text name=user><p>");
out.println("<input type=submit value=提交>");
out.println("<input type=reset value=重填>");
//chain.doFilter(req,resp); 这里注释掉后就可以正常运行
out.close();
}


public void destroy() {

}
}

关键的一步,chain.doFilter(req,resp); 把注释去掉后,输入http://localhost:8080/ch10/Login会显示错误页面:

HTTP Status 404 - /ch10/Login

--------------------------------------------------------------------------------

type Status report

message /ch10/Login

description The requested resource (/ch10/Login) is not available.


--------------------------------------------------------------------------------



Apache Tomcat/5.0.28


web.xml主要内容如下:
<filter>
<filter-name>filtertest2</filter-name>
<filter-class>FilterTest2</filter-class>
</filter>

<filter-mapping>
<filter-name>filtertest2</filter-name>
<url-pattern>/Login</url-pattern>
</filter-mapping>

...全文
194 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
alex_homer 2007-07-27
  • 打赏
  • 举报
回复
问题是不是在这里?
chain.doFilter(req,resp);
->out.close();
michael9527 2007-07-27
  • 打赏
  • 举报
回复
呵呵,我只是试试过滤器的用法,但是加了chain.doFilter(req,resp) 就报错,不知道为什么
zhangsatan 2007-07-27
  • 打赏
  • 举报
回复
我就想不通你为什么要把登录信息写在过滤器里面~~~应该先是一个servlet提交,然后调用过滤器过滤,再用一个servlet或jsp来显示的顺序吧,过滤器在中间要过滤两次的
jianghao08 2007-07-27
  • 打赏
  • 举报
回复
也就是说chain.doFilter(req,resp)之后就到了/Login,不能显示你要显示的内容。
jianghao08 2007-07-27
  • 打赏
  • 举报
回复
chain.doFilter(req,resp)是让程序继续走一去的意思,感觉你这个不应该用Filter,也应该用一个Servlet
little06 2007-07-27
  • 打赏
  • 举报
回复
HTTP Status 404
表示不存在
是不是地址出现问题,还是跳转到其他地方了

81,094

社区成员

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

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