我有一个servlet、一个过滤器和一个login.jsp。我填写用户名和密码,然后单击登录按钮,不调用过滤器,只调用 servlet。servlet 和过滤器位于不同的包中,我认为这是问题所在。如果我将 servlet 和过滤器放在同一个包中,则过滤器被成功调用。但我想在不同的包中使用它们。我应该怎么办?提前致谢!
筛选:
package log.reg.myfilter;
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.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
@WebFilter("/LoginFilter")
public class LoginFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
PrintWriter out = response.getWriter();
HttpServletRequest req = (HttpServletRequest) request;
String username = req.getParameter("username");
String pass = req.getParameter("password1");
System.out.println(username);
if ((username.length() > 6) && (pass.length() > 3)) {
System.out.println("In filter");
chain.doFilter(request, response);
}
else
out.println("Invalid Input");
}
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
小服务程序:
package log.reg;
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("/firstServlet")
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("In servlet");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
out.println("Welcome " + username);
}
}
登录.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="firstServlet" method="get">
<table style="background-color: lightgreen; margin-left: 20px; margin-top: 20px">
<tr>
<td>
<h3 style="color: red">Login Page!!!</h3>
</td>
</tr>
<tr>
<td>UserName : </td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>Password : </td>
<td><input type="password" name="password1"></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="login"></td>
</tr>
</table>
</form>
</body>
</html>
web.xml
<?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>Servlet ex29 - filter</display-name>
<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>