java HTTP 状态 500 -with description - 服务器遇到内部错误,无法完成此请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29789285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:57:09  来源:igfitidea点击:

HTTP Status 500 -with description -The server encountered an internal error that prevented it from fulfilling this request

java

提问by harsh

I am trying to implement listener program in which i get an HTTP status 500 Error once i submit with the index.html form page which redirect it to Servlet1 class.!

我正在尝试实现侦听器程序,一旦我提交了 index.html 表单页面,将它重定向到 Servlet1 类,我就会收到一个 HTTP 状态 500 错误。!

Mylistener.java

我的监听器

import javax.servlet.annotation.WebListener;
import javax.servlet.*;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;


@WebListener
public class Mylistener implements HttpSessionListener {

  static int total=0, current=0;
  ServletContext ctx=null;
    public void sessionCreated(HttpSessionEvent e)  { 
        total++;
        current++;
       ctx=e.getSession().getServletContext();
       ctx.setAttribute("total users",total);
       ctx.setAttribute("looged users",current);

    }

    /**
     * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
     */
    public void sessionDestroyed(HttpSessionEvent e)  { 
         current--;  
         ctx.setAttribute("currentusers",current);  
    }

}

![enter image description here][1

![在此处输入图像描述][1

Servlet1.java

Servlet1.java

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class Servlet1 extends HttpServlet {



    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("Text/html");
        PrintWriter out=res.getWriter();
        String n= req.getParameter("nname");
        System.out.println("Welcome"+n);
        HttpSession session=req.getSession();  
        session.setAttribute("uname",n);  
        ServletContext ctx= getServletContext();
        int i=(Integer)ctx.getAttribute("total");
        int c=(Integer)ctx.getAttribute("current");
        out.println("total users"+i);
        out.println("Current users"+c);


        out.close();
    }

}

index.html

索引.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="servlet1" method="post">
Enter your name <input type="text" name="nname"><br>
Enter your password : <input type="password" name="npass">
<input type="submit" value="submit">
</form>

</body>
</html>
web.xml

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

  <listener>
  <listener-class>Mylistener</listener-class>
  </listener>

  <servlet>
     <servlet-name>First</servlet-name>
    <servlet-class>Servlet1</servlet-class>
  </servlet>


  <servlet-mapping>
    <servlet-name>First</servlet-name>
    <url-pattern>/servlet1</url-pattern>
  </servlet-mapping>


</web-app>

]

]

Error:

错误:

HTTP Status 500 - 


type Exception report

message 

description The server encountered an internal error that prevented it from fulfilling this request.

exception 
java.lang.NullPointerException
    Servlet1.doPost(Servlet1.java:25)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.59 logs.

注意 Apache Tomcat/7.0.59 日志中提供了根本原因的完整堆栈跟踪。

回答by Zealous System

check null condition before casting.

在转换之前检查空条件。

        int i=0,c=0;
        if(ctx.getAttribute("total") != null){
            i=(Integer)ctx.getAttribute("total");
        }
        if(ctx.getAttribute("current") != null){
            c=(Integer)ctx.getAttribute("current");
        }
        out.println("total users"+i);
        out.println("Current users"+c);