This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Servlets and the fly likes Getting data in Servlet and passing to jsp but could not display in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Getting data in Servlet and passing to jsp but could not display in jsp" Watch "Getting data in Servlet and passing to jsp but could not display in jsp" New topic
Author

Getting data in Servlet and passing to jsp but could not display in jsp

Matloob Hussain
Ranch Hand

Joined: May 13, 2011
Posts: 37

I created a servlet named ViewAgentsServlet.java which is used to access table from mysql database and store table information in a list and then this list is passed to jsp (named adminViewAgents.jsp) where I want to display.
I have another servlet which is called at the start to varify the user name and password then forward to login panel which is a jsp page(userLogged.jsp). Once user logged and reached to userLogged.jsp where the user has option to call ViewAgentsServlet.java servlet.

but it give me following error

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /adminViewAgents.jsp at line 16

13: </tr>
14: <%Iterator itr;%>
15: <% List mydata= (List)request.getAttribute("data");%>
16: <%for (itr=mydata.iterator(); itr.hasNext(); )
17: {}
18: %>
19: </table>


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:521)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:430)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)


Thanks for your help in advance

regard,
Matloob

Matloob
OCPJP 1.6
OCPWCD in progress.....
dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
Start using Java standard tag libraries(JSTL).
1. Put the list into the session/request in your servlet.Say: request.setAttribute("list","modelData");
2.Request dispatcher-> forward it to the jsp where you would like to display the modelData.
3.Using JSTL iterate through it.
<c:forEach items="list" var="data">
<c: out value="list"/>
</c:forEach>

Just my thoughts!!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

That should be <c:forEach items="${list}" var="data"> - with EL tags around the attribute name. But you are right that <c:forEach> is the way to go.

However, that still doesn't solve the problem. If line 16 threw an exception, the most likely candidate is a NullPointerException. This would mean that there is no request attribute called "data". Matloob, are you sure you've set it? Is it perhaps a session attribute instead of a request attribute?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
Can we have somethng like this say check if its empty or not <c:if test ="${not empty list}"></c:if> will this be of any luck.

OOps missed adding EL previous post.
Matloob Hussain
Ranch Hand

Joined: May 13, 2011
Posts: 37

This is my login servlet used to verify login details:

This is ViewAgentsServlet used to get data from table of mysql database and stored in List named agentList. This is working fine, I check in console, agentList is showing the data.

This is the div which is added to userLogged.jsp. When I enter userID and password to verify login detail and then session is created and redirected to userLogged.jsp. At this stage, it could not get any data of List. An method called on this List type in this jsp page cause the exception. This is I am thinking....

dileep keely
Ranch Hand

Joined: Jun 28, 2010
Posts: 91
I must have done something like below.
There are two jars : jstl.jar and standard.jar you got to download.
refer http://www.coderanch.com/how-to/java/SetupJstlForJsp2
Based on tomcat version you need to add the JSTL tag libraries.refer the above url.

<c:if test ="${not empty data}">
<c:forEach var ="modelData" items = "${data}">
<c: out value="${modelData}"/>

</c:forEach>
</c:if>

Try this out. If every thing works out you can use <c:choose>.


Thats all from my side .
Piyush Mangal
Ranch Hand

Joined: Jan 22, 2007
Posts: 196
I created a servlet named ViewAgentsServlet.java which is used to access table from mysql database and store table information in a list and then this list is passed to jsp (named adminViewAgents.jsp) where I want to display.
I have another servlet which is called at the start to varify the user name and password then forward to login panel which is a jsp page(userLogged.jsp). Once user logged and reached to userLogged.jsp where the user has option to call ViewAgentsServlet.java servlet.


Where are you calling ViewAgentsServlet?
Your LoginServlet redirects the user to userLogged.jsp directly which just checks for user session and tries to display the empty list. You should redirect to ViewAgentsServlet from your LoginServlet which will then set the required data in request or session whatever you are using.
Matloob Hussain
Ranch Hand

Joined: May 13, 2011
Posts: 37

Thank you for your reply, Now it working........ but another issue?
I retrieved data from table of mysql database and stored in List named data using servlet. I used request.setAttribute("data" , data) to set request attributes and dispatched using folloing code, which works fines. I can access the data in userLogged.jsp page.


Now on userLogged.jsp page, I called another servlet named AddAgentServlet.java which will add one more record in table of mysql database (which works perfect) but in this servlet I tried to access this data of type List using request.getAttribute("data") ---> this does not work, it gives me "An Exception has occurred! java.lang.NullPointerExceptio"

But if I do it using session.setAttribute("data" , data) and session.getAttribute("data") ----> it works fine , I can retrieve data of List and use it in this servlet. Could you please explain. And one thing more, shall I use session or request to transfer data from one servlet to another servlet.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

Matloob Hussain wrote:Now on userLogged.jsp page, I called another servlet named AddAgentServlet.java

What do you mean by "call" the servlet? Servlets are not "called". If, as I suspect, you are submitting a request to another servlet, then that's an entirely new request and of course the old request, along with its attributes, is long gone.

The sessions stays around across requests, so scoped variables placed in the session stay around for multiple requests.

You should always use request scope unless you have a good reason not to.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
GeeCON Prague 2014
 
subject: Getting data in Servlet and passing to jsp but could not display in jsp