This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes JSP and the fly likes how to populate textboxes with values from database based on id Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "how to populate textboxes with values from database based on id" Watch "how to populate textboxes with values from database based on id" New topic
Author

how to populate textboxes with values from database based on id

Karen Wilson
Ranch Hand

Joined: Nov 19, 2012
Posts: 68
I have a jsp file (show.jsp) that displays all values from a database in a table. I added a column in this table so an edit link appears next to each of these values, and when I click that link an edit.jsp page opens with textboxes that I want to populate with the values of the item I selected in the first jsp. I then want to update these values and save to the database. So the textboxes in edit.jsp should populate with the values I selected from the table in show.jsp
My problem is I don't know how to populate the textboxes in edit.jsp from the item I selected in show.jsp

here's the edit.jsp that's just the form for displaying the information:


and here's the servlet the handles the code:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Karen Wilson wrote:My problem is I don't know how to populate the textboxes in edit.jsp from the item I selected in show.jsp


The way that you populate input elements is via their value attribute. So what you want to end up with is HTML like: <input type="text" name="city" value="Boston">

So in the page controller you need to put the values into request scope so that they can be accessed in the JSP via the EL. So your markup could look something like:


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

Joined: Jan 10, 2002
Posts: 60050
    
  65

P.S. If it's possible that the city name can contain the double-quote character, you'd need to escape the value with <c:out>
Karen Wilson
Ranch Hand

Joined: Nov 19, 2012
Posts: 68
but how does it know to populate the value attribute from the item I selected in show.jsp

this image is what show.jsp looks like: https://www.google.ie/search?q=table+to+edit+and+delete+jsp&source=lnms&tbm=isch&sa=X&ei=vBmeUtaTCaes7Qb90IHoBg&ved=0CAcQ_AUoAQ&biw=1680&bih=956#facrc=_&imgdii=_&imgrc=jHoc-2VtcxaH-M%3A%3BxzkqOUa-gbMrDM%3Bhttp%253A%252F%252Fwww.roseindia.net%252Ftutorialfiles%252F47190.record.gif%3Bhttp%253A%252F%252Fwww.roseindia.net%252Fjsp%252Fcrud-application.shtml%3B359%3B247

so when I click edit/update link the textboxes need to know which id was selected
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

I have no way of knowing. Obviously your server code that processing the submission from show.jsp has the values. It's not up the JSP to make any decisions, but for the controller code to send the appropriate data to the JSP.
Karen Wilson
Ranch Hand

Joined: Nov 19, 2012
Posts: 68
so how do i get the jsp to send the data to the servlet?
I need to get the id of the property i selected and pass that id to the edit.jsp page right?
then the edit.jsp will display all the information
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60050
    
  65

Communication from the client to the server take place via a form submission or a hyperlink. In either case, data is carried to the server via request parameters; either from elements int he form, or the query string of the link.

Technically, a JSP does not communicate with a servlet. The JSP is transformed into an HTML page which is loaded into the browser. It is the HTML (not the JSP) that in turn submits a form or processes a link.
Is that the communication you are asking about?
Raymond Holguin
Ranch Hand

Joined: Aug 11, 2009
Posts: 72
The servlet code you show is what happens after the user clicks the "SAVE" button from the edit screen. What you are missing is the servlet code that is run after the user clicks the hyperlink from the "show" page. This is the code where you should be loading the object based on ID, and then setting the request object with all the various parameters you want to populate your form with. Then as BearB mentioned, you will do something like value="${MyVar}" for each element where your retrieving the data from the request object you set in your servlet.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to populate textboxes with values from database based on id
 
Similar Threads
How do I move to last record of database with a button in JSP, using JavaScript function
image uploaded to database, but other column returns null
How to transfer object from servlet tp JSP and back to Servlet
Table name as textbox?
servlet & JDBC connection