如何在eclipse中修复HTTP 500错误消息

时间:2018-04-02 03:02:07

标签: java eclipse http servlets log4j

所以,我在网络应用程序中使用Log4J进行简单的练习。这是我的代码:

package tot;

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;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 * Servlet implementation class pro
 */
@WebServlet("/pro")
public class pro extends HttpServlet {
private static final long serialVersionUID = 1L;

private Logger logg = null;

public void inut() {
    logg = Logger.getRootLogger();
    BasicConfigurator.configure();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    logg.setLevel(Level.FATAL);
    logg.debug("debug");
    logg.info("info");
    logg.warn("warn");
    logg.error("error");
    logg.fatal("fatal");


    response.setContentType("text/html");
    PrintWriter printwriter = response.getWriter();
    printwriter.println("<html>");
    printwriter.println("<head><title>log4j</title></head>");
    printwriter.println("<body>");
    printwriter.println("no config <br>");
    printwriter.println("logger name: " + logg.getName());
    printwriter.println("</body>");
    printwriter.println("</html>");
}

}

我使用apache tomcat 9.0.6运行时创建了一个动态Web项目。我还创建了一个servlet。当我运行我的代码时,我收到以下错误消息:

HTTP Status 500 – Internal Server Error


Type Exception Report

  Description The server encountered an unexpected condition that prevented it 
from fulfilling the request.

Exception
java.lang.NullPointerException
    tot.pro.doGet(pro.java:31)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)


Note The full stack trace of the root cause is available in the server logs.


Apache Tomcat/9.0.6

此外,以下错误消息将打印到控制台:

java.lang.NullPointerException
    at tot.pro.doGet(pro.java:31)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:407)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1376)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

我期待发生的是http页面显示标题和正文消息;并且记录器消息“致命”应该打印到控制台。根据我的研究,我发现的解决方案是清除缓存,网页浏览历史记录,并刷新页面(我已经完成)。我觉得错误是从我如何设置服务器产生但我找不到我做错了什么。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

内部服务器错误是由于尝试访问尚未初始化的对象的属性时发生的空指针异常引起的。

在此代码中,记录器尚未初始化:

logg.setLevel(Level.FATAL);

尚未调用inut方法,这意味着未初始化记录器,导致logg的值为NULL。这会导致空指针异常。为了解决这个问题,你需要调用inut()方法,或者我们可以创建一个静态变量:

final static Logger logger = Logger.getLogger(classname.class);

有关静态变量的更多信息,请参阅 What does the 'static' keyword do in a class?