按照课程代码,我的项目在运行后有时500空指针,有时200但无内容显示
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="3.1">
<filter>
<filter-name>firstFilter</filter-name>
<filter-class>com.imooc.filter.FirstFilter</filter-class>
</filter>
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>com.imooc.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter>
<filter-name>urlPatternFilter</filter-name>
<filter-class>com.imooc.filter.UrlPatternFilter</filter-class>
</filter>
<filter>
<filter-name>filterA</filter-name>
<filter-class>com.imooc.filter.chain.FilterA</filter-class>
</filter>
<filter>
<filter-name>filterB</filter-name>
<filter-class>com.imooc.filter.chain.FilterB</filter-class>
</filter>
<filter>
<filter-name>filterC</filter-name>
<filter-class>com.imooc.filter.chain.FilterC</filter-class>
</filter>
<filter>
<filter-name>deviceAdapterFilter</filter-name>
<filter-class>com.imooc.filter.DeviceAdapterFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>firstFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>urlPatternFilter</filter-name>
<url-pattern>/v1/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>filterA</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>filterB</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>filterC</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>deviceAdapterFilter</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>
<listener>
<listener-class>com.imooc.WebListener</listener-class>
</listener>
</web-app>
package com.imooc.filter;
import javax.servlet.*;
import java.io.IOException;
public class FirstFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("FirstFilter已被初始化");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("firstFilter过滤器已生效");
//将请求响应向后传递,被后续的filter或servlet进行处理
chain.doFilter(request,response);
}
@Override
public void destroy() {
System.out.println("FirstFilter已被销毁");
}
}
package com.imooc.filter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class CharacterEncodingFilter implements Filter {
private String encoding;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
String encoding = filterConfig.getInitParameter("encoding");
System.out.println("Encoding:" + encoding);
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
req.setCharacterEncoding(encoding);
resp.setContentType("text/html;charset=" + encoding);;
chain.doFilter(req,resp);
}
@Override
public void destroy() {
}
}
package com.imooc.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/v1/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().println("<h1>你好 世界</h1>");
}
}
HTTP状态 500 - 内部服务器错误
类型 异常报告
描述 服务器遇到一个意外的情况,阻止它完成请求。
例外情况
java.lang.NullPointerException
org.apache.tomcat.util.buf.B2CConverter.getCharset(B2CConverter.java:60)
org.apache.catalina.connector.Request.setCharacterEncoding(Request.java:1705)
org.apache.catalina.connector.RequestFacade.setCharacterEncoding(RequestFacade.java:329)
com.imooc.filter.CharacterEncodingFilter.doFilter(CharacterEncodingFilter.java:20)
com.imooc.filter.FirstFilter.doFilter(FirstFilter.java:16)
):注意 主要问题的全部 stack 信息可以在 server logs 里查看
Apache Tomcat/8.5.66
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星