gpt4 book ai didi

java - HTTP 状态 500 - 带有说明 - 服务器遇到内部错误,导致其无法满足此请求

转载 作者:行者123 更新时间:2023-12-01 11:38:12 25 4
gpt4 key购买 nike

我正在尝试实现监听器程序,在该程序中,一旦我使用将其重定向到 Servlet1 类的 index.html 表单页面提交,就会收到 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);
}

}

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

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

<!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>

]

错误:

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)

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

最佳答案

在转换之前检查 null 条件。

        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);

关于java - HTTP 状态 500 - 带有说明 - 服务器遇到内部错误,导致其无法满足此请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29789285/

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