File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Populate form in a jsp with the data got from a servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Populate form in a jsp with the data got from a servlet" Watch "Populate form in a jsp with the data got from a servlet" New topic
Author

Populate form in a jsp with the data got from a servlet

ankit mahajan
Greenhorn

Joined: Jul 30, 2013
Posts: 7

I want to populate my index.jsp page in a java application with the data from database.
The values should be inserted depending on the form submitted from another page submit.jsp page.
The index page will be the first page which will be loaded on running application.
there will be a link to go to submit.jsp.
when the form from submit.jsp is submitted, it will go to a servlet which will connect with database to retrieve data corresponding to the values submitted.

the fields in the index.jsp are quite a number so i want to use something easy and compact.
Singh Anisha
Ranch Hand

Joined: May 09, 2012
Posts: 100

you can use:
RequestDispatcher, and forward object of request and response of jsp to servlet.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Singh Anisha wrote:you can use:
RequestDispatcher, and forward object of request and response of jsp to servlet.
No. One should never forward from a JSP to a servlet.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

ankit mahajan wrote:The index page will be the first page which will be loaded on running application.

That's a bad idea. A servlet controller should be the first thing executed, that obtains the data and then forwards it to the JSP.
Singh Anisha
Ranch Hand

Joined: May 09, 2012
Posts: 100

I am not a expert..
May i ask why we should not from jsp to servlet.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

JSP is an end point; they are intended to present the view. They should never be used for control or processing.

Always use the right tool for the job.
Singh Anisha
Ranch Hand

Joined: May 09, 2012
Posts: 100

So the right approach is,
for processing we should forward from jsp to servlet.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Be sure you are using the right words here. "Forward" means something very specific.

Submitting a form from a JSP to servlet is not forwarding. So if that's what you meant, then yes, forms should always be submitted to servlets, not JSPs.
Singh Anisha
Ranch Hand

Joined: May 09, 2012
Posts: 100

Yes that what i actually meant.
And thank you, provided link is very helpful to make me understand a better way to control the program.
:)
ankit mahajan
Greenhorn

Joined: Jul 30, 2013
Posts: 7

Singh Anisha wrote:you can use:
RequestDispatcher, and forward object of request and response of jsp to servlet.


The data that will come from jsp will come in ResultSet.
RequestDispatcher cannot send ResultSet.
or can it?

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Result sets should never be sent to a JSP!

A result set is an expensive database resource. When you do database access you should copy the data from the result set into "normal" Java constructs (lists, beans, etc) and close the result set as quickly as possible.

Never, but never, should a result set be sent to a JSP.
ankit mahajan
Greenhorn

Joined: Jul 30, 2013
Posts: 7

Bear Bibeault wrote:Result sets should never be sent to a JSP!

A result set is an expensive database resource. When you do database access you should copy the data from the result set into "normal" Java constructs (lists, beans, etc) and close the result set as quickly as possible.

Never, but never, should a result set be sent to a JSP.

I dont want to send ResultSet to jsp .
I want to send data retrieved in ResultSet to jsp.
but in an efficient manner so that it can be used to populate the corresponding fields in jsp.
Like passenger name retrieved from database should populate the textbox having id pass_name.

Can you help me with that?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

There are a bunch of ways to do that. First, make sure you use consistent naming throughout. Using different names of the same thing, even abbreviations, is a source for confusion.

So if the column is passenger_name, using pass_name elsewhere would make things harder. Be as consistent as possible.

If you want things to be rather free form, you could store the info in a Map, where the key is the name (e.g. passenger_name) and the value is, well, the value (eg. "Bear"). Or, you could be more formal about it and use a bean, where the properties are fixed.

Let's assume the latter. So you would create a bean for each row (a List of beans if there are more than one) and send the bean to the JSP as a scoped variable (using setAttribute());

On the JSP, fill in the values, example: <input type="text" name="passengerName" value="${myBean.passengerName}>

That do to get you started?">
 
Don't get me started about those stupid light bulbs.
 
subject: Populate form in a jsp with the data got from a servlet
 
Similar Threads
Update and Delete database in jsp
jsp-servlet-jsp
Flow of data from Init Servlet to jsp
How to update Session variable with Form Data ?
select tag in logic:iterate