我试图实现Servlet过滤器功能。基本上它将是Filter-> Servlet-> Filter。但是,部署没有发生。我完全不知道发生了什么,因为我对它很陌生。
这是我迄今为止所做的事情 -
MyFilter.java
package p;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
public class MyFilter implements Filter {
@Override
public void init(FilterConfig cfg) throws ServletException{
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("Filter is invoked before.");
chain.doFilter(request, response);
out.println("Filter is invoked after.");
}
}
@Override
public void destroy()
{
}
}
HelloServlet.java
package p;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>\n"+
"<head></head>\n"+
"<body>\n"+
"<p><h1>Servlet<h1><p>\n"+
"</body></html>");
}
}
}
的index.html
<html>
<head></head>
<body>
<a href="HelloServlet">Click here</a>
</body>
</html>
&安培;最后是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">
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>p.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>/HelloServlet</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
&安培;最后一些错误消息
Starting Tomcat process...
Waiting for Tomcat...
Tomcat server started.
Incrementally deploying http://localhost:8084/WebApp5
Completed incremental distribution of http://localhost:8084/WebApp5
Incrementally redeploying http://localhost:8084/WebApp5
Deploy is in progress...
deploy?config=file%3A%2FC
%3A%2FUsers%2FSANDEE%7E1%2FAppData%2FLocal%2FTemp%
2Fcontext264440372445081080
3.xml&path=/WebApp5
FAIL - Deployed application at context path /WebApp5 but context
failed to start
C:\Users\SANDEEP ROY\Documents\NetBeansProjects\WebApp5\nbproject\build-
impl.xml:1045: The module has not been deployed.
See the server log for details.
BUILD FAILED (total time: 9 seconds)
我正在玩其他项目,似乎这个特定项目没有被部署,其他项目工作正常。
此外,当我构建这个(失败)时,我试图从Firefox访问本地主机,甚至不加载。所以看来这个项目对tomcat也有一些伤害。完全感觉停电。