简体   繁体   English

Java servlet 中的 getAttribute() 方法错误

[英]Java getAttribute() method error in the servlet

I am Facing an issue while trying to get sum element in one servlet from the other by using Request Dispatcher.我在尝试使用 Request Dispatcher 从一个 servlet 中获取另一个 servlet 中的总和元素时遇到问题。

But the getAttribute() method in S2.java is giving error while accessing the sum from Request_Dispatcher_example.java.但是 S2.java 中的 getAttribute() 方法在从 Request_Dispatcher_example.java 访问总和时出错。

I have tried this using HttpSession as well but the same error is occurred there我也使用 HttpSession 尝试过这个,但是那里发生了同样的错误

  **Error:**
    
    Cannot invoke "java.lang.Integer.intValue()" because the return value of "javax.servlet.http.HttpServletRequest.getAttribute(String)" is null

Type Exception Report Message Cannot invoke "java.lang.Integer.intValue()" because the return value of "javax.servlet.http.HttpServletRequest.getAttribute(String)" is null Exception java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because the return value of "javax.servlet.http.HttpServletRequest.getAttribute(String)" is null com.servlet.S2.doGet(S2.java:22) javax.servlet.http.HttpServlet.service(HttpServlet.java:655) javax.servlet.http.HttpServlet.service(HttpServlet.java:764) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)类型异常报告消息无法调用“java.lang.Integer.intValue()”,因为“javax.servlet.http.HttpServletRequest.getAttribute(String)”的返回值为null异常java.lang.NullPointerException:无法调用“java。 lang.Integer.intValue()" because the return value of "javax.servlet.http.HttpServletRequest.getAttribute(String)" is null com.servlet.S2.doGet(S2.java:22) javax.servlet.http.HttpServlet .service(HttpServlet.java:655) javax.servlet.http.HttpServlet.service(HttpServlet.java:764) org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

Code代码

1. Request_Dispatcher_example.java 1. Request_Dispatcher_example.java

public class Request_Dispatcher_example extends HttpServlet

{
    public void processRequest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException 
    {
        String i = req.getParameter("n1");
        String j = req.getParameter("n2");
        
        int nn1= Integer.parseInt(i);
        int nn2 = Integer.parseInt(j);
        
        int s = nn1 + nn2;
        
        req.setAttribute("sum", s);
        
        
        RequestDispatcher rd = req.getRequestDispatcher("s2");
        rd.forward(req, res);
        
    }
}

2. S2.java 2.S2.java

public class S2 extends HttpServlet 
{
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
    {
        
        
        int nn1  = Integer.parseInt(req.getParameter("n1"));
        int nn2  = Integer.parseInt(req.getParameter("n2"));

        int p = nn1*nn2;
        int sum = (Integer) req.getAttribute("sum");
        
        PrintWriter out = res.getWriter();
        out.println("Sum : "+sum);
        out.println("Product : "+p);
        
    }
}

Got the solution for this error:得到了这个错误的解决方案:

In this case, I did not map my first java servlet file Request_Dispatcher_example.java in an xml file.在这种情况下,我没有 map 我的第一个 java servlet 文件 Request_Dispatcher_example.java 在 xml 文件中。

But now after mapping it in xml it is working fine and giving the proper results.但是现在在 xml 中映射它之后,它工作正常并给出了正确的结果。

Specifically: we need to map our all the servlet files to xml to overcome this issue具体来说:我们需要将我们所有的 servlet 文件 map 到 xml 来解决这个问题

Thank you!谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM