我正在尝试实现一个侦听器程序,在这个程序中我获得了一个HTTP状态500错误一旦我使用index.html表单页面将其重定向到Servlet1类。!
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日志中提供了根本原因的完整堆栈跟踪。
答案 0 :(得分:1)
在投射前检查空条件。
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);