gpt4 book ai didi

java - doFilter方法没有运行?

转载 作者:行者123 更新时间:2023-12-02 05:54:07 24 4
gpt4 key购买 nike

这是login.jsp页面

  <!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="checklogin" method="Post">
<h2>Login tab</h2> <br>
Login id:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="loginid"><br><br>
Password:&nbsp;&nbsp;&nbsp;&nbsp;<input type="password" name="password"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

这是 checklogin servlet

   protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out=response.getWriter();

javax.servlet.http.HttpSession session = request.getSession(true);
String username = (String)request.getParameter("loginid");
String password = (String)request.getParameter("password");
session.setAttribute("UserName", username);
if(username.equals("Prashant") && password.equals("123"))
{
response.sendRedirect("admin.jsp");
}
else
{
out.println("<h2>"+"Sharam aani chahiye account banaya nahi aur login kar rahe ho"+"<h2>");
}
}

这是我的过滤器,它被映射到 we.xml 中的过滤器中,如下 URL /admin.jsp/*

   public class adminfilter implements javax.servlet.Filter{

@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("Sawagat nahi karoge init method ka");
}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("Sawagat nahi karoge doFilter method ka");
}

@Override
public void destroy() {
System.out.println("Sawagat nahi karoge destroy method ka");
}

}

这是我的 web.xml 文件

  <?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee /web-app_3_1.xsd">
<filter>
<filter-name>adminfilter</filter-name>
<filter-class>Filters_Demo.adminfilter</filter-class>
</filter>
<filter-mapping>
<filter-name>adminfilter</filter-name>
<url-pattern>/admin.jsp/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>checklogin</servlet-name>
<servlet-class>Filters_Demo.checklogin</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>checklogin</servlet-name>
<url-pattern>/checklogin</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

init() 和 destroy() 方法正在运行,但 doFilter 方法没有运行?

最佳答案

在 doFilter 方法中,您应该调用链中的下一个过滤器:

chain.doFilter(request, response);

如果没有这个,您的请求将不会在过滤后得到进一步处理。

另外,我建议在 web.xml 文件中进行更改

<url-pattern>*.jsp</url-pattern>

关于java - doFilter方法没有运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23261281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com