aspose file tools*
The moose likes JSP and the fly likes Display data from database to jsp page. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Display data from database to jsp page." Watch "Display data from database to jsp page." New topic
Author

Display data from database to jsp page.

hemanth acharya
Ranch Hand

Joined: Aug 10, 2011
Posts: 51
Hello guys,

I am quite tangled on an issue....

The following is what I am trying to do,

I have jsp page as shown below,




I am calling a servlet on click of "go" button after i select one of the "check boxes" in the table,

This servlet calls a method which retrieves data from the database.

Now I want to populate it back to the JSP page from the servlet.


Could anyone lend me their thoughts over this please....


Thanks and regards,
Hemanth.
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

Hello!

1) your scriptlet about the "FormDatas" list does not belong in your jsp...you use jstl anyway, why then use scriptlets?

2) I don't fully understand your problem, but viewing your code...i see that your checkbox and submit button are not encapsulated into the FORM ...
hemanth acharya
Ranch Hand

Joined: Aug 10, 2011
Posts: 51
olivier,

Thanks for early response ..

1) I am using the "FormDatas" in the following line in my jsp,

<c:forEach items="${list}" var="element">

2) yea, sorry, I corrected it now..

My issue is to populate the data retrieved from the Database into various fields present in the form.

Thank you,
hemanth.
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

So, you have a list with data, and form fields above it. You want to select a line in the list, and on selection, you want the above form fields to be filled out with the data from the selected line in the list?
hemanth acharya
Ranch Hand

Joined: Aug 10, 2011
Posts: 51
olivier ,

Yeah, thats pretty much what I want to do, except that, the items are in a table, with check boxes for every row. On select of a particular row, the servlet call should query for that row of info and display it in the form fields.


Thank you,
Hemanth.
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

ok...

First of all, in my opinion, i don't think you need to query your DB again, because your LIST is allready there, with all the data needed! I see that the data in the list is as much as the data in the form. If the list is hold in the session, you can just use it again!

For selection of a row, there is no need for using form elements. You can just, use a html anchor element to create a link to you servlet. Just pass an ID to the servlet, that tells wich row you want to select.

Once the server (your servlet) knows wich row you want, you can get the "x"-row from your List, this will return your "details" instance.

You can choose how you put it back to your client (JSP). You can use Ajax with Json or something, to fill up the fields, or you can just redirect again to your page, using jstl to access the "detail" instance, and put it in your fields "value".
hemanth acharya
Ranch Hand

Joined: Aug 10, 2011
Posts: 51
olivier,

I didn't understand few things here....

1) Should i attach the ${element.id} to id in the anchor tag?

2) After the call to servlet with the above info , how the servlet should be redirected to page to retrieve that particular row from the list?

Thank you,
Hemanth.
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

I will try to keep is as simple as possible.

First, in your datalist on screen, you put anchor tags that go to your servlet.
for example

<a href="./mydataservlet?action=getrow&myid=${element.id}"> click to select </a>

so for example, for each row you get :

<a href="./mydataservlet?action=getrow&myid=1"> click to select </a>
<a href="./mydataservlet?action=getrow&myid=2"> click to select </a>
<a href="./mydataservlet?action=getrow&myid=3"> click to select </a>

When clicked on the link that will be generated, you come in mydataservlet, where you can retrieve the action en myid parameters.

action is, for example, "getrow".
When getrow is triggered, you look for the parameter "myid".

Now, in your servlet, you got the ID of the row that is selected.

if your details LIST is in your session, you can use it again.
get the list, and from that list, get the details-instance.
You can do this because you have "myid"!

now you got your detail instance.
now, for example, you can put that detail instance in the session.

after that, redirect to your jsp page

and in your jsp page you kan use something like :

<input type="text" value="${detail.name}" name="name">

wich for example will be translated into :

<input type="text" value="john doe" name="name">

now, your fields will contain the data from selected row, and your listview will be rendered again.

There are a lot of ways to achieve such a things, but the basics remain the same. And this is a pretty basic method.



hemanth acharya
Ranch Hand

Joined: Aug 10, 2011
Posts: 51
olivier,

Thank you for the example....

I have quite down the same, but not able to display any data in the JSP page... I will place all the relevant codes here, could you please tell me where i am wrong...

My jsp page..



My servlet doGet method..





My class where i have my logic ..




The values are properly getting stored in the list... But i am not able to get any value in "value" of the fields...


Thank you,
Hemanth.
hemanth acharya
Ranch Hand

Joined: Aug 10, 2011
Posts: 51
Hello guys,

In the above code, i am getting a nullPointer Exception, since on load of the jsp page, "lists" is null, it only gets filled up only after i select any one of the row.


Can anyone please help me solve this..


Thanks and regards,
Hemanth.
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

<%
List<Details> lists = (List<Details>) request
.getAttribute("DbDatas");
pageContext.setAttribute("list", lists);
%>

obvious...
"lists" does not exist..."list" does...

as i said before, you should not do this in your jsp, but in your servlet.

and for showing 1 record, you must not use a List, but the details instance itself...
The detail instance itself can be retrieved trough your servlet.

so the only thing you should have is for example :

<input id="txtName" name="txt" type="text" value="${detail.userName} " />
hemanth acharya
Ranch Hand

Joined: Aug 10, 2011
Posts: 51
olivier,

You mean to say that, i need to set all the values in one details instance from the list , and send that detail instance over to the jsp page?

Is my understand of what you are saying is right?


Thank you,
Hemanth.
olivier dutranoit
Ranch Hand

Joined: Aug 20, 2011
Posts: 81

Yes,

for example :

in your servlet :

get a record from your detailsList

--> Detail detail = get detail from list.

put it in session or something

session.setAttribute("detail", detail);

when in the jsp

${detail.Name}

so, you do not need your scriptlet as mentioned befor...

hemanth acharya
Ranch Hand

Joined: Aug 10, 2011
Posts: 51
olivier,

I am getting a null pointer exception...




Here's my modified jsp and servlet pages...






My web xml for reference




Thank you,
Hemanth.
hemanth acharya
Ranch Hand

Joined: Aug 10, 2011
Posts: 51
olivier,

On first load of the page, the value field of the text box will be null , how to counter that.

Thanks and regards,

hemanth
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Display data from database to jsp page.