wood burning stoves 2.0*
The moose likes Servlets and the fly likes servlet or jsp page with prefilled forms Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet or jsp page with prefilled forms" Watch "servlet or jsp page with prefilled forms" New topic
Author

servlet or jsp page with prefilled forms

Viv Singh
Ranch Hand

Joined: Nov 08, 2008
Posts: 73
Hi,

I have a jsp page where an admin can manage users.
this page is used to both create new users or edit details of existing users.

at the top of the page there is a drop down box.

what i want is, if the admin selects an username from the dropdown menu, the html form should be prefilled by fetching the correspondig data from the database.

if he/she does not select anything from the drop down box, he/she can fill in the form and create a new user.

how can i prefill the forms when a user is selected from the drop down menu?

thanks in advance.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

Set the value attributes of the controls.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Viv Singh
Ranch Hand

Joined: Nov 08, 2008
Posts: 73
sorry i am new to jsp and servlet programming. how does it work, setting the attributes for controls?
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

HTML input form elements all have a value attribute.



So when the select box changes and you get the data from the database use the value attribute to populate form elements. This can be done with a standard/typical POST/GET refreshing the page with the new values. So you might have something like:



or this could be done using AJAX. As you are new to all this I'd get the basics down first though. Once you understand it then you can look at helpful technologies such as AJAX to make things better on your applications.


GenRocket - Experts at Building Test Data
Viv Singh
Ranch Hand

Joined: Nov 08, 2008
Posts: 73
thanks for the reply.

just a question for the syntax:



is that assuming the bean-id is user and there is an attribute in the servlet called "firstName"?

are there any good tutorials out there to read on??

thanks
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by V. Singh:
thanks for the reply.

just a question for the syntax:



is that assuming the bean-id is user and there is an attribute in the servlet called "firstName"?



That's correct.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
On the change event of the dropdown, fire of a call to the servlet/backend that will get the associated values and store it in the appropriate scope so that the view/jsp can get it from there.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Viv Singh
Ranch Hand

Joined: Nov 08, 2008
Posts: 73
On the change event of the dropdown, fire of a call to the servlet/backend that will get the associated values and store it in the appropriate scope so that the view/jsp can get it from there.


This will have to be done using Ajax, isnt it?
Viv Singh
Ranch Hand

Joined: Nov 08, 2008
Posts: 73
If I used this syntax: value="${user.firstName}" it simply puts that in the text box and not the value of the attribute firstName. What am I doing wrong?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61451
    
  67

See the JSP FAQ to make sure that your web app is set up correctly for JSP 2.0.
Viv Singh
Ranch Hand

Joined: Nov 08, 2008
Posts: 73
Originally posted by Bosun Bello:
....., fire of a call to the servlet/backend that will get the associated values and store it in the appropriate scope so that the view/jsp can get it from there.


I had to change the idea to the following there is a single page.
At the top of the page there is a textbox and a search button.
followed by a few fields (text boxes) to enter or update data.

now if the user enters a username in the first text box, which is the "search text box" and clicks the search button i get the data from the database in a servlet. after that it should redirect the admin to the same page but now with the fields below the search textbox prefilled with the data fetched from the database.

I have the following code:



my "suser" servlet fetches the data from the database and i have written another class/bean "User" that stores the data fetched, which is instanciated in the servlet. Now i would like get the data from that bean to prefill the forms.

what am i doing wrong here?

thanks for the help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: servlet or jsp page with prefilled forms