Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Matloob Hussain
Ranch Hand
Posts: 37
Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
dileep keely
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
dileep keely
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64613
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic