Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to retrieve the data from the database and display it on a jsp using struts

 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am new to struts. I want to display all the details from a table on a jsp to user. Please help in doing these.

thanks in advance,
Vidya

In my action class am adding the data in database to a list and forwarding to a jsp page.

RetrieveDataActionClass.java
--------------------



In my jsp am getting the data from the list and displaying using tag.

viewstudents.jsp
----------


Am getting an exception if i execute the above code,

Error message
----------------
 
Shankar Tanikella
Ranch Hand
Posts: 329
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vidya,
As the exception says it all, it cannot find the bean "listname" in any scope. The default scope used should be "request scope".
So to solve the problem you have three ways [if you are using struts 1.x]

1. Put the list in the related[would have mentioned this in struts configuration file] form and get the list from the form ->best way

2. Put the list in the request in the action class not into the session
3. mention the scope explicitly in the JSP, like

 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i also tried by using request scope as well.. but i didn't get.. please suggest me..
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please debug the code and see if the code flow is going into the while loop and setting the "listname" in session?
A simple SOP will help.
 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


debugging is not completing properly..
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vidya Gupta wrote:
debugging is not completing properly..

You can use System.out.println() to check.
 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

ya i checked it by writing println() statement. It is not displaying. I think it is not going into the loop.
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vidya Gupta wrote:
ya i checked it by writing println() statement. It is not displaying. I think it is not going into the loop.

This means the table is empty . Please check.
 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


hey the table contains records.. i dnt knw why it is displaying the errors.. Anyway thanks alot..
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey check the code, your Statement object is not initialized (or have you missed to put the code where statement is initialized via connection.createStatement()).

This code should throw a NullPointerException since stmt is not initialized.
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, please put all the database related code inside try catch block and at the end close ResultSet, Connection and the Statement.
 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i kept in try n catch only.. Here in this post i removed it..
 
S Subbu
Ranch Hand
Posts: 87
jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Vidhya , If This is Your Code .



Check Once You are assigning set attribute in while loop , that is not correct.
Do One thing create Another List and add the values List to newly created list , at the end of while loop set the newly Created list to request attribute.


At the Jsp Page


Instead of above Iteration do

 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

still am facing the same problem with the application.. am having a doubt that in my application for the above RetrieveDataActionClass, i created a empty form bean class. Is that a problem..?
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried accessing the "listname" attribute inside your servlet (session.getAttribute("listname"))?
 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


no i didn't tried to get the list in action class..
 
S Subbu
Ranch Hand
Posts: 87
jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Vidya From which jsp you are accessing this action class that contains form tag. No need to form bean for your requirement .

Is is possible paste the code form which jsp you start for accessing the given action class.

Or paste Your Error message .
 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Error Message:
---------------
 
S Subbu
Ranch Hand
Posts: 87
jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok,

Your action is executed fine,i mean request coming to our action class.

If fine Paste Action Classs Code.

Problem in Your Action Class Only
 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RetrieveDataActionClass.java
-----------------

 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vidya,

In your class you are using the request attribute name as samplelist, but in your jsp you are using listname.
 
S Subbu
Ranch Hand
Posts: 87
jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Do that Correction given by Swastik Dey

And You want to display the resul as

name phone email state city
name phone email state city
name phone email state city
name phone email state city
name phone email state city

That was not possible with your code. you have to change code to get display result as above and you need to your jsp page also.

 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

No.. i posted wrong name.. thats it..
 
S Subbu
Ranch Hand
Posts: 87
jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now Your Code Working.

If not send the whole code once from starting jsp to ending jsp .

 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

did you checked my code? if you did any changes please post here..
 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


can i send my complete code?
 
S Subbu
Ranch Hand
Posts: 87
jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Please Change JSP Page Also , i already sent xterday.
 
S Subbu
Ranch Hand
Posts: 87
jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it not work, then you send complete code.
 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks again.. but i changed the action class like above n jsp also as you said yesterday only.. but am not getting the output in my system..
 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


how can i send complete code? is there any option to check attach the file?
 
S Subbu
Ranch Hand
Posts: 87
jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont know.may be not that option (i think so)
 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The problem is in my action class. It is not adding list to the specified scope. First i want to know whether the list contains tha values or not..



here my doubt is whether the statement is returning the different names is that column or not? How to see the different names in that column?
I used statement. But it didn't work. Please help me..
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you created a formbean? If yes, use its property to get the details in jsp page.
See this link for more details.
Do let us know in case of any issue.
 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i created an empty formbean without any properties.. because am getting the details from the database, so i thought there is no need of formbean.
 
Sumit Patil
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just follow the link, the author has given step by step details for displaying the data in jsp using logic iterate.
Even though you are getting the details from the db, you need to create the formbean and populate its properties from db results since logic iterate will look for this formbean in the request scope (Experts please correct me if i am wrong).
You also need to define this form bean in the struts-config.xml file.
 
S Subbu
Ranch Hand
Posts: 87
jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println("rs.getString("name"));



Hi, why you are using double quote just print as

System.out.println(rs.getString("name"));

For Your Requirement Don't need Form Bean?You have Empty Form Bean that's not a problem.

To Check Your List has elements or not just print list after while loop

System.out.println(resultList);

Check First resultList contains elements or not.

 
S Subbu
Ranch Hand
Posts: 87
jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
logic iterate will look for this formbean in the request scope


Sumit , logic:iterate iterate like for loop, it takes array,list,map etc..

No need to populate db result to form beans.
 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

am sorry to say this again.. still the data is not displaying.. I changed my code as per the above link,

Student.java
-------------


RetrieveFormBean.java
-----------------


RetrieveDataActionClass.java
-----------------------



viewstudents.jsp
--------------------
// using formbean



Am extremely sorry for irritating you all like this for a small requirement. But am new to struts and trying to learn.. Thanks..
 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


thanks subbu.. ill check it..
 
Vidya Gupta
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


the values are not displaying on the console. I think it is not at all going to Action class. because not only this statement System.out.println(resultList); is not executing.. simple println statements like System.out.println("executing"); also not displaying the message on the console. I don't know whether it is going to action class or not..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic