I am new to this world.
I wrote an application that has a dropdown list and I need data from database to be populated in that dropdown before submitting and adding the data to another table.
Please help me in getting this done through beans or JSP.
Thank you all..
Anish
sandeep kethe
Greenhorn
Joined: Jan 11, 2009
Posts: 14
posted
0
Hi
Store the data in hashmap or in an array list and iterate it for example use as in this way
for(int i=0;i<arraylist.size;i++)
{
Bean instance = (Instance of bean)arraylist.get(i);
//Store all the values in the bean and now close the scriptlet
//Put the code for select box like
<option value= <%=bean instance.get id %><%=bean intance.get required value&>
}
Hope you have understood
Regards
K. Sandeep
heyanish Raj
Greenhorn
Joined: Aug 19, 2009
Posts: 2
posted
0
hi Sandeep,
i am very new to this, so to be honest I dont understand,.
Let me put what i have done so far..
In my Class file
First off, since you are new to JSP, it's important to establish good habits right up front. Rule #1 is to never, never, never, put Java code ina JSP. Never. That's a really old-fashioned and bad practice.
Rather, you should fetch the data from the DB in a servlet controller, or a bean, to use on the JSP.
Also, please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.
You can go back and change your post to add code tags by clicking the button on your post.
Gudim, Just above your post Bear mentioned that don't use Java code(Scriptlets) in JSP. JSTL and EL better alternatives.
Max Rahder
Ranch Hand
Joined: Nov 06, 2000
Posts: 177
posted
0
Vishal Pandya wrote:Gudim, Just above your post Bear mentioned that don't use Java code(Scriptlets) in JSP. JSTL and EL better alternatives.
I agree. Your JSP would be structured something like this:
This code assumes that some previous code, such as your MVC controller, placed a java.util.List in scope -- e.g., via request.setAttribute("mylist",aList); -- and also assumes that each list item has a "getProperty()" method.
To learn more, Google "JSP expression language" and "jstl".
Sunder Ganapathy
Ranch Hand
Joined: Apr 01, 2003
Posts: 120
posted
1
The Java Bean ( DTO ) is as follows:
The Java Bean ( DAO ) is as follows.
JSP accessing the DAO class is as follows.
The database ( test ) is updated with state names in a table ( states ) .
Preet Dhillon
Greenhorn
Joined: May 20, 2008
Posts: 14
posted
0
In the code sample provided by Sunder Ganapathy for the dropdown, is there a posibility to pass locale from jsp to the DAO?
My database table is setup for different locales and I am currently working with something like this: ("select * FROM tablename WHERE tablename.locale='en_CA'")
I need the locale to change when user switches language. I am thinking of keeping locale in a session variable but not sure if it would be a good practice to specify session variable in the WHERE clause. Are there other better options?
It's fine to pass the value of any scoped variable to the lower layers of the application, as long as you aren't passing the session itself (ar anything else from the servlet packages).
Another way of looking at it is if you need to import javax.servlet in model classes, you're doing it wrong!