0

I am trying to pass the my DTO object from jsp to servlet using session object but i'm getting null pointer exception initially i tried with request object which is giving same error hence i moved to session object.

in first servlet

request.getSession().setAttribute("datadto", dataDTO);
request.getRequestDispatcher("success.jsp").forward(request, response);

in jsp

<% 
        DataDTO dataDTO = (DataDTO) request.getAttribute("datadto");
        HttpSession session420 = request.getSession();
        session420.setAttribute("object", dataDTO);
%>

in second servlet

HttpSession session=request.getSession(false);
DataDTO dataDTO = (DataDTO) session.getAttribute("object");

MyService myService = ServiceFactory.getMyService();
myService.generateExcel(dataDTO); <--nullpointerexception

on googling i found the following link I implemented as he said still i'm getting null pointer exception

in jsp

DataDTO dataDTO = myService.getData(keyword, nor);
String myObjectId = UUID.randomUUID().toString();
request.getSession().setAttribute(myObjectId, dataDTO);
request.setAttribute("myObjectId", myObjectId);

in second servlet

String myObjectId = request.getParameter("myObjectId");
Object myObject = request.getSession().getAttribute(myObjectId);
DataDTO dataDTO = (DataDTO) myObject;
request.getSession().removeAttribute(myObjectId);

please help me.

Community
  • 1
  • 1
rock
  • 115
  • 1
  • 2
  • 17

2 Answers2

0

Your DTO is already set in session in your first servlet. So, just remove this code from your JSP because it's actually removing the DTO from session by setting it to null.

<% 
    DataDTO dataDTO = (DataDTO) request.getAttribute("datadto");
    HttpSession session420 = request.getSession();
    session420.setAttribute("object", dataDTO);
%>

And retrieve the DTO in your second servlet by using the original key name datadto

HttpSession session=request.getSession(false);
DataDTO dataDTO = (DataDTO) session.getAttribute("datadto");
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • i'm getting nullpointer exception in the second servlet at exportExcel() – rock Feb 27 '15 at 09:40
  • I removed as you said and placed below code HttpSession session=request.getSession(false); DataDTO dataDTO = (DataDTO) session.getAttribute("datadto"); in second servlet – rock Feb 27 '15 at 09:43
  • Are you sure datadto is not null in the first servlet? Show us more code? – Ravi K Thapliyal Feb 27 '15 at 09:48
  • in first servlet DataDTO dataDTO = myService.getData(keyword,nor); request.getSession().setAttribute("datadto", dataDTO); if(dataDTO !=null) request.getRequestDispatcher("success.jsp").forward(request, response); else request.getRequestDispatcher("failure.jsp").forward(request, response); – rock Feb 27 '15 at 09:59
  • Please share your full code. The error could be at someplace else. Perhaps you're overwriting the value someplace else too. – Ravi K Thapliyal Feb 27 '15 at 10:37
  • yes you are right error at some other MyService class – rock Feb 27 '15 at 10:53
  • how to convert object to string and string to object as said in the following link http://stackoverflow.com/questions/4253660/passing-object-from-jsp-to-servlet – rock Feb 27 '15 at 10:54
0

In the JSP you are saying .

 DataDTO dataDTO = (DataDTO) request.getAttribute("datadto");

Instead it should be request.getSession().getAttribute("datadto");

Your code modified in the below line

<% 
        DataDTO dataDTO = (DataDTO) request.getSession().getAttribute("datadto");
        HttpSession session420 = request.getSession();
        session420.setAttribute("object", dataDTO);
%>
Avinash Reddy
  • 2,204
  • 3
  • 25
  • 44
  • in first servlet ---------------- request.getSession().setAttribute("datadto", dataDTO); in jsp ------ <% DataDTO dataDTO = (DataDTO) request.getSession().getAttribute("datadto"); HttpSession session420 = request.getSession(); session420.setAttribute("object", dataDTO); %> in second servlet ----------------- HttpSession session=request.getSession(false); DataDTO dataDTO = (DataDTO) session.getAttribute("object"); – rock Feb 27 '15 at 09:54
  • in first servlet ---------------- request.getSession().setAttribute("datadto", dataDTO); in jsp ------ <% DataDTO dataDTO = (DataDTO) request.getSession().getAttribute("datadto"); HttpSession session420 = request.getSession(); session420.setAttribute("object", dataDTO); %> in second servlet ----------------- HttpSession session=request.getSession(false); DataDTO dataDTO = (DataDTO) session.getAttribute("object"); – rock Feb 27 '15 at 10:04
  • @rock : I was referring your code as follows. In the first servlet you are setting the value as follows request.getSession().setAttribute("datadto", dataDTO); and in the first jsp you are saying DataDTO dataDTO = (DataDTO) request.getAttribute("datadto");. how will u get the value. In the servlet you are setting the value in session and in the jsp you are fetching the value from request. That is what i have shown in my answer. Please correct me if i am wrong – Avinash Reddy Feb 27 '15 at 11:42