aspose file tools*
The moose likes Struts and the fly likes How to retrieve the data from the database and display it on a jsp using struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to retrieve the data from the database and display it on a jsp using struts" Watch "How to retrieve the data from the database and display it on a jsp using struts" New topic
Author

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

Vidya Gupta
Ranch Hand

Joined: Mar 18, 2012
Posts: 98
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

Joined: Jan 30, 2011
Posts: 329

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



Have Fun with Java
little,little.. little by little makes a lot..
Vidya Gupta
Ranch Hand

Joined: Mar 18, 2012
Posts: 98


i also tried by using request scope as well.. but i didn't get.. please suggest me..
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

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.


Thanks & Regards, Sumeet
SCJP 1.4, SCWCD 5, LinkedIn Profile
Vidya Gupta
Ranch Hand

Joined: Mar 18, 2012
Posts: 98


debugging is not completing properly..
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

Vidya Gupta wrote:
debugging is not completing properly..

You can use System.out.println() to check.
Vidya Gupta
Ranch Hand

Joined: Mar 18, 2012
Posts: 98

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

Joined: May 25, 2009
Posts: 296

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

Joined: Mar 18, 2012
Posts: 98


hey the table contains records.. i dnt knw why it is displaying the errors.. Anyway thanks alot..
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

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

Joined: May 25, 2009
Posts: 296

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

Joined: Mar 18, 2012
Posts: 98


i kept in try n catch only.. Here in this post i removed it..
S Subbu
Ranch Hand

Joined: Mar 20, 2012
Posts: 87

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

Joined: Mar 18, 2012
Posts: 98

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

Joined: May 25, 2009
Posts: 296

Have you tried accessing the "listname" attribute inside your servlet (session.getAttribute("listname"))?
Vidya Gupta
Ranch Hand

Joined: Mar 18, 2012
Posts: 98


no i didn't tried to get the list in action class..
S Subbu
Ranch Hand

Joined: Mar 20, 2012
Posts: 87

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

Joined: Mar 18, 2012
Posts: 98

Error Message:
---------------
S Subbu
Ranch Hand

Joined: Mar 20, 2012
Posts: 87

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

Joined: Mar 18, 2012
Posts: 98
RetrieveDataActionClass.java
-----------------

Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1479
    
    6

Vidya,

In your class you are using the request attribute name as samplelist, but in your jsp you are using listname.


Swastik
S Subbu
Ranch Hand

Joined: Mar 20, 2012
Posts: 87

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

Joined: Mar 18, 2012
Posts: 98

No.. i posted wrong name.. thats it..
S Subbu
Ranch Hand

Joined: Mar 20, 2012
Posts: 87

Now Your Code Working.

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

Vidya Gupta
Ranch Hand

Joined: Mar 18, 2012
Posts: 98

did you checked my code? if you did any changes please post here..
Vidya Gupta
Ranch Hand

Joined: Mar 18, 2012
Posts: 98


can i send my complete code?
S Subbu
Ranch Hand

Joined: Mar 20, 2012
Posts: 87




Please Change JSP Page Also , i already sent xterday.
S Subbu
Ranch Hand

Joined: Mar 20, 2012
Posts: 87

If it not work, then you send complete code.
Vidya Gupta
Ranch Hand

Joined: Mar 18, 2012
Posts: 98

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

Joined: Mar 18, 2012
Posts: 98


how can i send complete code? is there any option to check attach the file?
S Subbu
Ranch Hand

Joined: Mar 20, 2012
Posts: 87

I dont know.may be not that option (i think so)
Vidya Gupta
Ranch Hand

Joined: Mar 18, 2012
Posts: 98


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

Joined: May 25, 2009
Posts: 296

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

Joined: Mar 18, 2012
Posts: 98

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

Joined: May 25, 2009
Posts: 296

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

Joined: Mar 20, 2012
Posts: 87

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

Joined: Mar 20, 2012
Posts: 87

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

Joined: Mar 18, 2012
Posts: 98

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

Joined: Mar 18, 2012
Posts: 98


thanks subbu.. ill check it..
Vidya Gupta
Ranch Hand

Joined: Mar 18, 2012
Posts: 98


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..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to retrieve the data from the database and display it on a jsp using struts