ServletContext-java.lang.NullPointerException

时间:2019-05-27 21:36:01

标签: java servlets nullpointerexception

该程序应该计算会话中的总用户和当前用户。

收听者代码

int fibonacciNumberOrder = 1;
do {
    System.out.print(fibonacci(fibonacciNumberOrder) + " ");
    fibonacciNumberOrder++;
} while (true);

servlet:

import javax.servlet.ServletContext;  
import javax.servlet.http.HttpSessionEvent;  
import javax.servlet.http.HttpSessionListener;  

public class MyListener implements HttpSessionListener{  
    ServletContext ctx;
    static int total=0,current=0;  

    public void sessionCreated(HttpSessionEvent e) {  
    total++;  
    current++;  

    ctx=e.getSession().getServletContext();  
    ctx.setAttribute("totalusers", total);  
    ctx.setAttribute("currentusers", current);  

    }  

    public void sessionDestroyed(HttpSessionEvent e) {  
        current--;  
        ctx.setAttribute("currentusers",current);  
    }  

}  

我正在关注本教程-https://www.javatpoint.com/HttpSessionEvent,我不应该收到此异常。

踪迹

import java.io.IOException;  
import java.io.PrintWriter;  

import javax.servlet.ServletContext;  
import javax.servlet.ServletException;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;  

public class First extends HttpServlet {  
public void doGet(HttpServletRequest request,  
 HttpServletResponse response)  
    throws ServletException, IOException {  

        response.setContentType("text/html");  
        PrintWriter out = response.getWriter();  

        String n=request.getParameter("username");  
        out.print("Welcome "+n);  

        HttpSession session=request.getSession();  
        session.setAttribute("uname",n);  

        //retrieving data from ServletContext object  
        ServletContext ctx=getServletContext();  
        int t=(Integer)ctx.getAttribute("totalusers");  //java.lang.NullPointerException 
        int c=(Integer)ctx.getAttribute("currentusers");  
        out.print("<br>total users= "+t);  
        out.print("<br>current users= "+c);  

        out.print("<br><a href='logout'>logout</a>");  

        out.close();  
    }
} 

到达打印用户名然后出现java.lang.NullPointerException的地步。

1 个答案:

答案 0 :(得分:0)

抛出

NullPointerException,因为方法getServletContext()返回null。 尝试如下重写servlet中的init()方法。希望这会工作。

public class First extends HttpServlet {

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String n=request.getParameter("username");
        out.print("Welcome "+n); HttpSession 
        session=request.getSession();
        session.setAttribute("uname",n);
        //retrieving data from ServletContext object 
        ServletContext ctx=getServletContext(); int t=(Integer)ctx.getAttribute("totalusers");
        int c=(Integer)ctx.getAttribute("currentusers");
out.print("<br>total users= "+t);
        out.print("<br>current users= "+c);
        out.print("<br><a href='logout'>logout</a>");
        out.close();
    }
}