Java 如何使运行servlet过滤器?
对于我的web应用程序,我已经创建了登录页面。为了阻止对其他页面的访问,我设置了一个过滤器。但是在运行web应用程序时,它给出了Java 如何使运行servlet过滤器?,java,servlets,servlet-filters,Java,Servlets,Servlet Filters,对于我的web应用程序,我已经创建了登录页面。为了阻止对其他页面的访问,我设置了一个过滤器。但是在运行web应用程序时,它给出了Servlet类com.pricar.grid.AuthenticationFilter不是javax.Servlet.Servlet 我也不能得到正确的结果 下面是我的代码:web.xml中的过滤器配置 <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//S
Servlet类com.pricar.grid.AuthenticationFilter不是javax.Servlet.Servlet
我也不能得到正确的结果
下面是我的代码: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>
<display-name>Staff Management</display-name>
<filter>
<filter-name>AuthenticationFilter</filter-name>
<display-name>AuthenticationFilter</display-name>
<description></description>
<filter-class>com.pricar.grid.AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>AuthenticationFilter</filter-name>
<url-pattern>/StaffManagementSystem/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>DWR Servlet</display-name>
<description>Direct Web Remoter Servlet</description>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>UserLogin.html</welcome-file>
</welcome-file-list>
</web-app>
我尝试测试的URL是
我使用jetty作为服务器
任何建议都将不胜感激!!!
提前谢谢强>
最终更新:
package com.pricar.grid;
public class AuthenticationFilter implements Filter {
public AuthenticationFilter() {
// TODO Auto-generated constructor stub
}
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
WebContext ctx = WebContextFactory.get();
HttpServletRequest req = ctx.getHttpServletRequest();
HttpSession session = req.getSession(false);
String name = (String) session.getAttribute("userName");
System.out.print ("Within Simple Filter ... ");
System.out.println ("Filtering the Request ...");
System.out.print ("Within Simple Filter ... ");
System.out.println ("Filtering the Response ...");
if ( name == null ){
//I have to redirect to the person to index page.
((HttpServletResponse) response).sendRedirect("index.html");
}
chain.doFilter(request, response);
}
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
上面提到的所有更改都已完成。它正在被编译。我甚至无法在控制台中获得“sysout”输出。它只是传递URL 如果您的URL是
http://localhost:8080/StaffManagementSystem/EmployeeManagement.html
您不打算将此模式用于过滤器映射吗:
<url-pattern>/StaffManagementSystem/*</url-pattern>
(不是带有HttpServlet*******的版本)
您应该避免在web应用程序中使用System.out。相反,选择Logger并检查servlet容器的日志。如果URL是
http://localhost:8080/StaffManagementSystem/EmployeeManagement.html
您不打算将此模式用于过滤器映射吗:
<url-pattern>/StaffManagementSystem/*</url-pattern>
(不是带有HttpServlet*******的版本)
您应该避免在web应用程序中使用System.out。相反,选择Logger并检查servlet容器的日志。不确定,但我认为您有一个问题: if(name==null){ //我必须重定向到“要索引的人”页面。 ((HttpServletResponse)请求).sendRedirect(“index.html”)
将其更改为响应不确定,但我认为您有一个问题: if(name==null){ //我必须重定向到“要索引的人”页面。 ((HttpServletResponse)请求).sendRedirect(“index.html”)
将其更改为响应你在web.xml中有任何servlet映射吗?@matt b:现在更新了我的问题。检查!!!你在web.xml中有任何servlet映射吗?@matt b:现在更新了我的问题。检查!!!@Colin HEBERT:ea..我看到了..并删除了它。我以为是问题制造者。对吗??!@BalusC:主要原因是什么问题?@Colin:这些与实际问题无关。问题仍然存在(OP在编辑过程中删除了提示)。它是否将请求视为响应?
((HttpServletResponse)请求)
或WebContext?((WebContext)请求)
我猜真正的问题是一个程序员不理解他/她正在使用的API:-)@rsp:OP后来改变了这一点,这显然是天方夜谭。现在的代码甚至没有编译。@Colin-HEBERT:ea..我看到了..并删除了它。我以为是问题制造者。是这样吗??!@BalusC:t是什么主要问题?@Colin:这些问题与实际问题无关。问题仍然存在(OP在编辑过程中删除了提示)。它是将请求视为响应吗?((HttpServletResponse)请求)
还是视为WebContext?((WebContext)请求)
我猜真正的问题是一个程序员不理解他/她正在使用的API:-)@rsp:OP后来改变了这一点,这显然是天方夜谭。现在的代码甚至无法编译。