aspose file tools*
The moose likes Servlets and the fly likes Passing List from Servlet to jsp ? - value of list becomes NULL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing List from Servlet to jsp ? - value of list becomes NULL" Watch "Passing List from Servlet to jsp ? - value of list becomes NULL" New topic
Author

Passing List from Servlet to jsp ? - value of list becomes NULL

Steve Adam
Ranch Hand

Joined: Jan 07, 2009
Posts: 40
Hi all

In my servlet, I have following code:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

FileDAO fileDAOInstance = new FileDAO();
List servletList = fileDAOInstance.getDataFromDB();

request.getSession().setAttribute("somename", servletList); //setting the list into request object

String jspPage = "/FileData.jsp";
RequestDispatcher dis = getServletContext().getRequestDispatcher(jspPage);
if(dis != null)
dis.forward(request,response);

}

And in my jsp, I am trying to retrieve it using:

List dataList = (List) request.getSession().getAttribute("somename");
Iterator itr = dataList.iterator();

The value of List is NULL.
Please tell me if anything is wrong in this piece of code.

Thanks !!


Its not what you do, its how well you do it.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Seems similar to this

http://www.coderanch.com/t/450262/JSP/java/when-trying-code-from-HFSJ#2004025

Perhaps the List returned by your DAO was null or you are not going to the JSP via the servlet ?

Can you post a stack trace please? and use CODE tags while displaying your code. Thanks


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Steve Adam
Ranch Hand

Joined: Jan 07, 2009
Posts: 40
Thanks Deepak, and sorry for putting the code this way.

here is the stack trace:

11:22:36,843 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.FileData_jsp._jspService(FileData_jsp.java:110)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:601)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi kunal, tell us your request flow( example:how you trigger the servlet ?). still need details to figure out the problem
Steve Adam
Ranch Hand

Joined: Jan 07, 2009
Posts: 40
Okay. I ll write down the details:
There is a possibility of the List being NULL in servlet itself.
Please help me guys.

I have written a Core Java method that return a List of JavaBean objects.


I am giving this to servlet, in doPost method.


I havent made changes anywhere else in the servlet.

And then I am trying to t\retrieve that List in jsp.


So, is there any problem over here ?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

1.this is redundant in your jsp . just declare it


2. your flow looks fine to me. check where you went wrong and why you get null from your session using by LogWritter
Steve Adam
Ranch Hand

Joined: Jan 07, 2009
Posts: 40
Helloooooooooooooooooo
plese help me
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

I suggest you check inside the servlet that the returned list is null or not (by logging).


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Steve Adam
Ranch Hand

Joined: Jan 07, 2009
Posts: 40
How can I check inside theservlet, if the returned List is NULL or not.
tried, but I am not able to do so .....
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Steve Adam
Ranch Hand

Joined: Jan 07, 2009
Posts: 40
i have my code in doPost() ... but my doPost method is not getting called, tried this by writeng some SOPs.
why is that ?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

Kunal Alshi wrote:i have my code in doPost() ... but my doPost method is not getting called, tried this by writeng some SOPs.
why is that ?


Then how you get to the JSP anyway? First make sure the program flow is working well then you may correct other errors
Steve Adam
Ranch Hand

Joined: Jan 07, 2009
Posts: 40
Vijitha Kumara wrote:
Kunal Alshi wrote:i have my code in doPost() ... but my doPost method is not getting called, tried this by writeng some SOPs.
why is that ?


Then how you get to the JSP anyway? First make sure the program flow is working well then you may correct other errors



yeah madam !
i wrote the code in doGet(). Because in this case, doPost() will never execute.

the program flow is correct, because the static data is from my jsp is getting displayed. Only the List is passed as NULL.
I get the following exception.
I guess solving this exception will solve the problem as well.

Please help me to solve this exception.

Thanks.

14:16:17,593 ERROR [STDERR] java.lang.ClassNotFoundException: com.mysql.jdbc.Driver from BaseClassLoader@6adf2f{VFSClassLoaderPolicy@6288ed{name=vfszip:/C:/Program%20Files/JBoss/jboss-5.0.1.GA/server/default/deploy/SampleWebProject.war/ domain=ClassLoaderDomain@bffcc0{name=vfszip:/C:/Program%20Files/JBoss/jboss-5.0.1.GA/server/default/deploy/SampleWebProject.war/ parentPolicy=AFTER_BUT_JAVA_BEFORE parent=ClassLoaderDomain@996cca{DefaultDomain}} roots=[MemoryContextHandler@17531354[path= context=vfsmemory://5c4o033-8j4ejr-fw4no7b5-1-fw4nohfq-1x real=vfsmemory://5c4o033-8j4ejr-fw4no7b5-1-fw4nohfq-1x], ZipEntryHandler@8012028[path=SampleWebProject.war/WEB-INF/classes context=file:/C:/Program%20Files/JBoss/jboss-5.0.1.GA/server/default/deploy/ real=file:/C:/Program%20Files/JBoss/jboss-5.0.1.GA/server/default/deploy/SampleWebProject.war/WEB-INF/classes]] delegates=null exported=[com.file.sample] <IMPORT-ALL>NON_EMPTY}}
14:16:17,593 ERROR [STDERR] at org.jboss.classloader.spi.base.BaseClassLoader.loadClass(BaseClassLoader.java:422)
14:16:17,593 ERROR [STDERR] at java.lang.ClassLoader.loadClass(Unknown Source)
14:16:17,593 ERROR [STDERR] at java.lang.ClassLoader.loadClassInternal(Unknown Source)
14:16:17,593 ERROR [STDERR] at java.lang.Class.forName0(Native Method)
14:16:17,593 ERROR [STDERR] at java.lang.Class.forName(Unknown Source)
14:16:17,593 ERROR [STDERR] at com.file.sample.ConnectionUtil.getConnection(ConnectionUtil.java:13)
14:16:17,593 ERROR [STDERR] at com.file.sample.FileDAO.getDataFromDB(FileDAO.java:31)
14:16:17,593 ERROR [STDERR] at com.file.sample.FileServlet.doGet(FileServlet.java:36)
14:16:17,593 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
14:16:17,593 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
14:16:17,593 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
14:16:17,593 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
14:16:17,593 ERROR [STDERR] at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
14:16:17,593 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
14:16:17,593 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
14:16:17,593 ERROR [STDERR] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
14:16:17,593 ERROR [STDERR] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
14:16:17,593 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
14:16:17,593 ERROR [STDERR] at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
14:16:17,593 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
14:16:17,593 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
14:16:17,593 ERROR [STDERR] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
14:16:17,593 ERROR [STDERR] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
14:16:17,593 ERROR [STDERR] at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
14:16:17,593 ERROR [STDERR] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
14:16:17,593 ERROR [STDERR] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
14:16:17,593 ERROR [STDERR] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
14:16:17,593 ERROR [STDERR] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:601)
14:16:17,593 ERROR [STDERR] at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
14:16:17,593 ERROR [STDERR] at java.lang.Thread.run(Unknown Source)
14:16:17,593 ERROR [STDERR] java.lang.NullPointerException
14:16:17,593 ERROR [STDERR] at com.file.sample.FileDAO.getDataFromDB(FileDAO.java:36)
14:16:17,593 ERROR [STDERR] at com.file.sample.FileServlet.doGet(FileServlet.java:36)
14:16:17,593 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
14:16:17,593 ERROR [STDERR] at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
14:16:17,593 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
14:16:17,593 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
14:16:17,593 ERROR [STDERR] at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
14:16:17,593 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
14:16:17,593 ERROR [STDERR] at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
14:16:17,593 ERROR [STDERR] at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
14:16:17,593 ERROR [STDERR] at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
14:16:17,593 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
14:16:17,593 ERROR [STDERR] at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
14:16:17,593 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
14:16:17,593 ERROR [STDERR] at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
14:16:17,593 ERROR [STDERR] at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
14:16:17,593 ERROR [STDERR] at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
14:16:17,593 ERROR [STDERR] at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
14:16:17,593 ERROR [STDERR] at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
14:16:17,593 ERROR [STDERR] at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
14:16:17,593 ERROR [STDERR] at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
14:16:17,593 ERROR [STDERR] at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:601)
14:16:17,593 ERROR [STDERR] at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
14:16:17,593 ERROR [STDERR] at java.lang.Thread.run(Unknown Source)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

add the mysql connector jar in your class path
Steve Adam
Ranch Hand

Joined: Jan 07, 2009
Posts: 40
it is already added
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

not loaded properly
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver


it is already added

Where is it located(Path) in the web application ?
Steve Adam
Ranch Hand

Joined: Jan 07, 2009
Posts: 40
C:\Program Files\mysql-connector-java-5.1.7\mysql-connector-java-5.1.7-bin.jar

I am loading it from this location.

I guess this wont work for servlets, right ??
where should I put this jar file, so as to include in classpath ?
Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
doPost() will get hit only if you specifically mention "method=POST" in the form declaration inside your JSP page, from where the control initially came to the servlet. Once inside the servlet, there are a lot of ways to know if the list returned is null :

1) if(null == servletList) sysout("list is null")
2) if(null == servletList && servletList.isEmpty()) sysout("list is not null, but empty")
etc.....
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3855

Kunal Alshi wrote:C:\Program Files\mysql-connector-java-5.1.7\mysql-connector-java-5.1.7-bin.jar
I am loading it from this location.I guess this wont work for servlets, right ??where should I put this jar file, so as to include in classpath ?


Where are you deployed this servlet? You have to put the JDBC jar file in a place where web/application server can find, usually inside your application's lib directory.
Steve Adam
Ranch Hand

Joined: Jan 07, 2009
Posts: 40
Thanks for the help everyone.

The exception is gone by placing the connectorJ jar in the lib folder of application server and including it in the classpath.

But I am still having some issues with displaying the List elements on jsp page.
I will keep this thread updated for the solution.

Thanks a lot !!

Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
why dont you just place these dependent jars inside

<webserverRoot>/webapps/<your_webapplication>/WEB-INF/lib

folder, restart the web server/servlet container, and check if you are still getting the classnotfound ???
Loading the class from a different location other than the web server path is a bad idea.
Steve Adam
Ranch Hand

Joined: Jan 07, 2009
Posts: 40
Hi everyone
Can anyone please say me what is wrong with these statements ?

Servlet code:



JSP code:



"dataList" has the value of NULL, even though "servletList" is NOT NULL.

please help !!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It's impossible to help with just those two statements in isolation.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

You seem to be solving one problem after another but not exactly in the way desired. When posting your next question please also post a related stack trace and a specific cause for the problem. That way the answers will help you a lot more.

"dataList" has the value of NULL, even though "servletList" is NOT NULL.


This is an impossibility, unless the session attributes were removed for some reason.

Hit the servlet.
Confirm if the list was null
The servlet redirects to the JSP
The value should be printed.

As simple as that. Your libraries should ideally not be present on a container level classpath
Steve Adam
Ranch Hand

Joined: Jan 07, 2009
Posts: 40
Deepak Bala wrote:When posting your next question please also post a related stack trace and a specific cause for the problem. That way the answers will help you a lot more.


Here it is:

08:58:43,687 ERROR [[jsp]] Servlet.service() for servlet jsp threw exception
java.lang.NullPointerException
at org.apache.jsp.FileData_jsp._jspService(FileData_jsp.java:112)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:601)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)



Hit the servlet.
Confirm if the list was null
The servlet redirects to the JSP
The value should be printed.


On hitting the servlet, the List is displayed. On hitting the jsp, List is not displayed.
So, the List is not getting passed to the jsp.

What is the problem with code ?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Okay, now show the code where the null pointer occurs (again, if it's the same code as before). Better yet, show *more* of the code in question. Things don't just disappear out of session for no reason, and this back-and-forth isn't very efficient.
Steve Adam
Ranch Hand

Joined: Jan 07, 2009
Posts: 40
Hi All
Thanks for your concerns.

The real problem was that I was hitting the jsp url, instead of the servlet, which I later came to know was a mistake in itself.
My program got running by hitting the servlet url.

Thank you ALL.
appreciate your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing List from Servlet to jsp ? - value of list becomes NULL