Filter 过滤器 web.xml 求助 牛人帮忙

loveveryb 2009-08-10 05:07:12
我用的MyEclipse,tomcat5.5 想做个验证登陆的filter
部署后,过滤器好像不能起作用,所有网页直接敲地址就能访问。
web.xml

<filter>
<filter-name>filterstation</filter-name>
<filter-class>servlet.FilterStation</filter-class>
</filter>

<filter-mapping>
<filter-name>filterstation</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

FilterStation.java文件
package servlet;
import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;

import java.util.*;

public class FilterStation extends HttpServlet implements Filter
{

private FilterConfig filterConfig;

/**
* Constructor of the object.
*/
public FilterStation() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpSession session = ((HttpServletRequest)request).getSession();
response.setCharacterEncoding("gb2312");
if(session.getAttribute("me")==null)
{
PrintWriter out = response.getWriter();
out.print("<script language=javascrept>alert('您还没有登录!‘);" +
"window.location.href='index.jsp';</script>");
}
else
{
filterChain.doFilter(request, response);
}
}

public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;// TODO Auto-generated method stub
}
}
...全文
584 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaofeng_123 2009-12-08
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 zasui3 的回复:]
web.xml中不是filter的配置在所有servlet前面吗?楼主可以改改试试
[/Quote]hahah
xiaofeng_123 2009-12-08
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 zasui3 的回复:]
web.xml中不是filter的配置在所有servlet前面吗?楼主可以改改试试
[/Quote]hahah
zasui3 2009-12-02
  • 打赏
  • 举报
回复
web.xml中不是filter的配置在所有servlet前面吗?楼主可以改改试试
JavaAlpha 2009-09-23
  • 打赏
  • 举报
回复
我是这么配置的
<!-- 验证是否登录 -->
<filter>
<filter-name>mustLoginFilter</filter-name>
<filter-class>
com.cms.sys.web.MustLoginFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>mustLoginFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
学城 2009-08-12
  • 打赏
  • 举报
回复
补充说明:doFilter方法
HttpSession session = ((HttpServletRequest)request).getSession();
response.setCharacterEncoding("gb2312");
StringBuffer buff= new StringBuffer();
if(session.getAttribute("userCount")==null) //判断用户账号是否为空
{
PrintWriter out = response.getWriter();
buff.append("<script language='javascript'>alert('您还没有登录,请登录系统');");
buff.append("window.location.href='../login.jsp'; </script>");
out.println(buff);
out.flush();
out.close();
}
else
{
filterChain.doFilter(request, response);
}

我使用的配置是Tomcat6.0+Myelcipse6.0+jdk1.5
loveveryb 2009-08-12
  • 打赏
  • 举报
回复
谢谢楼上众人 我再去试试
gowest0131 2009-08-12
  • 打赏
  • 举报
回复
因为是一个简单的Demo,代码中添加和修改的部分没有进行说明。
gowest0131 2009-08-12
  • 打赏
  • 举报
回复
首先,要说明的是Filter和servlet能否together,仅仅取决于对实际业务需求与框架设计的权衡。java就像是门艺术,在继承,封装,多态的基础之上,每个人都能够创造出不同的精彩。在这里LZ的subclass既是控制器又是过滤器,从程序设计的角度上来说,并没有任何的问题,只要在一个class内将过滤和控制的功能分清楚,理明白就可以了。当然,在面对很多企业级开发时,需要按照一定的规范将其分开处理就另当别论了。

针对LZ的问题,给出代码与说明。

1.开发环境:eclipse 3.5 + JDK 1.5 + tomcat 5.5
2.系统环境:Xp sp3
3.代码介绍
a.class(FilterStation.java,此类是在LZ的基础上简单modify出来的,主要是为了让Demo能够比较流畅,并且针对不同类型的request进行标识)

------------------------class start-------------------------------
package test;

import java.io.IOException;
import java.io.PrintWriter;

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.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
* Servlet Filter implementation class FilterStation
*/
public class FilterStation extends HttpServlet implements Filter {

/**
*
*/
private static final long serialVersionUID = 1L;
private FilterConfig filterConfig;
/**
* Default constructor.
*/

public FilterStation() {
// TODO Auto-generated constructor stub
super();
}

/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
super.destroy();
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println(" <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println(" <HTML>");
out.println(" <HEAD> <TITLE>A Servlet </TITLE> </HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");

out.print(" <script type='text/javascript'>");
out.print(" function back(){");
out.print(" window.history.back(-1);");
out.print(" }</script>");
out.print(" <button type=\"button\" onclick=\"back()\" >back</button>");

out.println(" </BODY>");
out.println(" </HTML>");
out.flush();
out.close();
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(" <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println(" <HTML>");
out.println(" <HEAD> <TITLE>A Servlet </TITLE> </HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");

out.print(" <script type='text/javascript'>");
out.print(" function back(){");
out.print(" window.history.back(-1);");
out.print(" }</script>");
out.print(" <button type=\"button\" onclick=\"back()\" >back</button>");

out.println(" </BODY>");
out.println(" </HTML>");
out.flush();
out.close();
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpSession session = ((HttpServletRequest) request).getSession();
response.setCharacterEncoding("gb2312");
if (request.getParameter("me") == null || "".equals(request.getParameter("me"))) {
PrintWriter out = response.getWriter();
// out.print(" <script language=javascrept>alert('您还没有登录!‘);"
// + "window.location.href='index.jsp'; </script>");
System.out.println("没有系统权限!");
request.getRequestDispatcher("../error.jsp").forward(request, response);
} else {
filterChain.doFilter(request, response);
}
}

public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;// TODO Auto-generated method stub
}
}
------------------------class end-------------------------------


b.jsp(demo中包含index.jsp,succuss.jsp,error.jsp以及needPrivPage.jsp,在这里只列举index.jsp)

------------------------index.jsp start-------------------------------
<body>
welcome Demo
<table align="center" width="500" border="1">
<tr>
<td width="150">servlet Get</td>
<td>
<a href="<%=request.getContextPath()%>/action" />Test servlet-Get</a>
</td></tr>

<tr>
<td>servlet Post</td>
<td>
<form id="f1" method="post">
<script type="text/javascript">
function up(){
//alert(document.forms[0].id);
document.forms[0].action = "<%=request.getContextPath()%>/action";
document.forms[0].submit();
}
</script>
<input type="hidden" name="me" value="test" />
<button type="button" onclick="up()">Test servlet-Post</button>
</form>

</td></tr>
<tr>
<td>with Filter</td>
<td>
<form id="f2">
登录ID:<input type="text" name="me1" />
<script type="text/javascript">
function test(){
var me = document.forms[1].item("me1").value;
window.location.href = "<%=request.getContextPath()%>/test/needPrivPage.jsp?me="+me;
}
</script>
<button type="button" onclick="test()">Test with-filter</button>
</form>
</td></tr>

<tr>
<td>without Filter</td>
<td>
<a href="<%=request.getContextPath()%>/succuss.jsp" />Test without-filter</a>
</td></tr>
</table>
</body>
------------------------index.jsp end-------------------------------


c.xml(关于web.xml这里就不多说了,如果发觉filter没有任何的action without exceptions and errors,那么你就要好好检查检查你的web.xml配置文件是不是内容和格式正确了)
------------------------web.xml start-------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" 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">
<display-name>Web</display-name>

<filter>
<description>
</description>
<display-name>FilterStation</display-name>
<filter-name>FilterStation</filter-name>
<filter-class>test.FilterStation</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterStation</filter-name>
<url-pattern>/test/*</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>action</servlet-name>
<servlet-class>test.FilterStation</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>/action</url-pattern>
</servlet-mapping>


<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
------------------------web.xml end-------------------------------


上述demo经过测试可以运行,
jsp中针对servlet的get方法和post方法的调用,以及filter的调用和没有调用的情况都进行了操作,
除index以外的jsp,因为篇幅的问题没有在这里罗列。
如果需要可以联系我,msn:rita_fantasy@hotmail.com.我们可以相互交流。
抽象四维 2009-08-11
  • 打赏
  • 举报
回复
没发现有啥问题:不过建议
第一,继承HttpServlet没用,删除相关代码
第二:将doFilter()内代码全部删除,加入request.setAttribute("name","小张");
在测试页面获取这个植¥{requestScope.name}来验证是Filter配置问题或者Tomcat版本问题,建议用Tomcat6.0以上
azlq850920 2009-08-11
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xuecheng_520 的回复:]
楼主,我调试了你的代码,web.xml配置如下:(过滤器的名字我改了)
  <servlet>
    <description>This is the description of my J2EE component </description>
    <display-name>This is the display name of my J2EE component </display-name>
    <servlet-name>CheckLogin </servlet-name>
    <servlet-class>servlet.CheckLogin </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>CheckLogin </servlet-name>
    <url-pattern>/jsp/CheckLogin </url-pattern>
  </servlet-mapping>
<filter>
    <filter-name>CheckLogin </filter-name>
    <filter-class>servlet.CheckLogin </filter-class>
  </filter>
<filter-mapping>
      <filter-name>CheckLogin </filter-name>
      <url-pattern>/jsp/* </url-pattern> <!-- 这里可以修改过滤的范围,这里指当访问工程目录下的webroot下的jsp文件夹的页面时会过滤-->
  </filter-mapping>

本人还意外发现你的这句代码中out.print(" <script language=javascrept>alert('您还没有登录!‘);"
language后面单词写错了 应该是javascript
[/Quote]

我也佩服
APOLLO_TS 2009-08-11
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xuecheng_520 的回复:]
楼主,我调试了你的代码,web.xml配置如下:(过滤器的名字我改了)
  <servlet>
    <description>This is the description of my J2EE component </description>
    <display-name>This is the display name of my J2EE component </display-name>
    <servlet-name>CheckLogin </servlet-name>
    <servlet-class>servlet.CheckLogin </servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>CheckLogin </servlet-name>
    <url-pattern>/jsp/CheckLogin </url-pattern>
  </servlet-mapping>
<filter>
    <filter-name>CheckLogin </filter-name>
    <filter-class>servlet.CheckLogin </filter-class>
  </filter>
<filter-mapping>
      <filter-name>CheckLogin </filter-name>
      <url-pattern>/jsp/* </url-pattern> <!-- 这里可以修改过滤的范围,这里指当访问工程目录下的webroot下的jsp文件夹的页面时会过滤-->
  </filter-mapping>

本人还意外发现你的这句代码中out.print(" <script language=javascrept>alert('您还没有登录!‘);"
language后面单词写错了 应该是javascript
[/Quote]

佩服!!
学城 2009-08-11
  • 打赏
  • 举报
回复
楼主,我调试了你的代码,web.xml配置如下:(过滤器的名字我改了)
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>CheckLogin</servlet-name>
<servlet-class>servlet.CheckLogin</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CheckLogin</servlet-name>
<url-pattern>/jsp/CheckLogin</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CheckLogin</filter-name>
<filter-class>servlet.CheckLogin</filter-class>
</filter>
<filter-mapping>
<filter-name>CheckLogin</filter-name>
<url-pattern>/jsp/*</url-pattern><!-- 这里可以修改过滤的范围,这里指当访问工程目录下的webroot下的jsp文件夹的页面时会过滤-->
</filter-mapping>

本人还意外发现你的这句代码中out.print(" <script language=javascrept>alert('您还没有登录!‘);"
language后面单词写错了 应该是javascript
zxj828282 2009-08-11
  • 打赏
  • 举报
回复
既然是过滤器就不应该继承HttpServlet了
machao299 2009-08-11
  • 打赏
  • 举报
回复
如果 你新建一个 class 继承 filter 呢 配置也就那样 适当的改对路径。
我记不清了 你试试吧。
loveveryb 2009-08-11
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 min123456520 的回复:]
楼主不妨把报的错误,展示出来给我们看看咯
[/Quote]
哪里看错误啊?problem那里没有错误了
loveveryb 2009-08-11
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 aywrenyue 的回复:]
检查你的web.xml配置,filter和filter-mapping的顺序是否正确,tomcat5.5好像是有限制的。
[/Quote]
是不是先filter 后 filter-mapping? 我的就是这么写的啊
qsrock 2009-08-11
  • 打赏
  • 举报
回复
既然是过滤器就不应该继承HttpServlet了
loveveryb 2009-08-11
  • 打赏
  • 举报
回复
谢谢 位置写错了吗? 还是路径?[Quote=引用 15 楼 wsgzxwt 的回复:]
<filter-class>servlet.FilterStation </filter-class>
貌似是你的class写错了
[/Quote]
wsgzxwt 2009-08-11
  • 打赏
  • 举报
回复
<filter-class>servlet.FilterStation </filter-class>
貌似是你的class写错了
myloveyoyo1314 2009-08-11
  • 打赏
  • 举报
回复
没研究过 路过看看
加载更多回复(7)

67,541

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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