gpt4 book ai didi

java - 过滤器 servlet 不适用于 Netbeans 中的 glassfish

转载 作者:太空宇宙 更新时间:2023-11-04 06:56:19 25 4
gpt4 key购买 nike

这是我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

<filter>
<display-name>MyFilter</display-name>
<filter-name>MyFilter</filter-name>
<filter-class>AB_DB.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/MyFilter</url-pattern>
</filter-mapping>
<servlet>
<description></description>
<display-name>Profile</display-name>
<servlet-name>Profile</servlet-name>
<servlet-class>AB_DB.Profile</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Profile</servlet-name>
<url-pattern>/Profile</url-pattern>
</servlet-mapping>
</web-app>

这是我尝试过的过滤器 servlet

package AB_DB;


import java.io.IOException;
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.HttpServletRequest;

public class MyFilter implements Filter {

public MyFilter() {
}

public void destroy() {
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String requri = ((HttpServletRequest) request).getRequestURI().substring(((HttpServletRequest) request).getContextPath().length() + 1);
System.out.println(requri);
//if request uri starts with user/ then system will forward the request to Profile servlet
if (requri.startsWith("user/")) {
//get userid from request uri
String id = requri.substring(5);
System.out.println(id);

//set attribute "user" with userid value
request.setAttribute("user", id);

//forward the request to Profile servlet
request.getRequestDispatcher("/Profile")
.forward(request, response);
} else {
chain.doFilter(request, response);
}
}

public void init(FilterConfig fConfig) throws ServletException {
}

}

但是在 GlassFish 4 服务器日志中,运行项目时根本没有错误。 URI 甚至没有用“System.out.println(id);”打印出来所以我假设过滤器甚至没有被注入(inject)。

任何人都可以看到我的代码有任何问题吗? sevlet 配置文件未运行

最佳答案

您已将您的过滤器映射到 /MyFilter

<url-pattern>/MyFilter</url-pattern>

与您请求的 URL 不匹配

/user/Surname1993

这样你的过滤器就不会被调用

关于java - 过滤器 servlet 不适用于 Netbeans 中的 glassfish,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22701406/

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