所以我有一个非常简单的Java EE Web App。它由Servlet和jsp组成。 Servlet位于名为" Servlets"的软件包中。过滤另一个名为"过滤器"。
过滤器是这样的:
@WebFilter(filterName="AuthFilter",
urlPatterns={"/ProgettoWeb2018/*","/profile/*"})
public class AuthFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("Hello from filter");
}
}
现在有了这个设置,我得到以下错误:
java.lang.ClassNotFoundException:Filters.AuthFilter
如果我在" Servlets"中移动过滤器包装它工作得很好。 怎么样?
答案 0 :(得分:0)
为了更好地理解 Servlet Filter
项目视图如下:
Servlet类是:
package com.whodesire.demos;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/WelcomeServlet")
public class WelcomeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public WelcomeServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { }
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>" + request.getParameter("username") + ", Welcome to Secured Servlet<h1>");
out.println("<hr/>");
out.close();
}
}
Servlet过滤器类是:
package com.whodesire.demos;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
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.annotation.WebFilter;
@WebFilter("/AuthenticationFilter")
public class AuthenticationFilter implements Filter {
public AuthenticationFilter() { }
public void destroy() {
System.out.println("destroy method is called in " + this.getClass().getName());
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("init method is called in " + this.getClass().getName());
String username = request.getParameter("username");
String password = request.getParameter("passwd");
String ipAddress = request.getRemoteAddr();
System.out.println("\n username and password is : " + username + ", " + password + "\n");
//Either you can write source here to fetch here DAO object for validation
if(username.equals("wsuser") && password.equals("wspassword")) {
System.out.println("User logged in " + ipAddress + " at " + (new Date().toString()));
// pass the request along the filter chain
chain.doFilter(request, response);
}else {
//if failed the servlet filter validation
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h2>Login request rejected in " + ipAddress + " at " + (new Date().toString()) + "</h2>");
out.close();
}
}
public void init(FilterConfig fConfig) throws ServletException {
System.out.println("init method is called in " + this.getClass().getName());
}
}
Login.html初始页面和CSS(放置为login.css):
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<link rel="stylesheet" type="text/css" href="login.css"/>
<title>Login Form</title>
</head>
<body>
<h2>User Login Form</h2>
<hr/>
<form name="loginForm" method="POST" action="WelcomeServlet">
<div class="login">
<input type="text" placeholder="Username" name="username"/>
<input type="password" placeholder="Password" name="passwd"/>
<input type="submit" value="Login"/>
<a href="#" class="forgot">forgot password?</a>
</div>
</form>
</body>
</html>
@charset "ISO-8859-1";
h2 {
display: block;
font-size: 1.8em;
font-family: sans-serif;
margin-top: 0.83em;
margin-bottom: 0.83em;
margin-left: 0;
margin-right: 0;
font-weight: bold;
}
.login {
height:110px;
width:190px;
margin:auto;
border:1px #CCC solid;
padding:10px;
}
.login input {
padding:5px;
margin:5px
}
最后是web.xml Servlet调度程序配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>servletFilterDemo</display-name>
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
<filter>
<filter-name>Authentication</filter-name>
<filter-class>com.whodesire.demos.AuthenticationFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Authentication</filter-name>
<url-pattern>/WelcomeServlet</url-pattern>
</filter-mapping>
</web-app>
希望这能让您对Servlet Filter示例有一个简单的了解。