wood burning stoves*
The moose likes Servlets and the fly likes Dynamic forms and pages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Dynamic forms and pages" Watch "Dynamic forms and pages" New topic
Author

Dynamic forms and pages

Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
Okay, so I know that hard coding your html into your servlet is bad, but how do you set up a dynamic form and/or page without coding the html into the servlet?

I have a form where I need to populate the data with information from a database, let the users modify it, and then save the changes back to the database.

Connecting to the servlet is a GET call, so I was going to return an html form that calls the same servlets POST method, so that I can save the data back to the db. But I'm sure there has to be a better way to do this.

TIA.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Please define "dynamic form" in more technical terms. You know, we are developers, not endusers.

Also, what's the first coding step you're stucking at? Displaying data from DB as input values or so?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

So you want an HTML form which posts to a servlet? I don't see why you think you need that servlet to produce the form. If it's just a static page, just have a static page.

Or if it isn't a static page, but it needs to be generated, I don't see why you think you need that same servlet to generate it. I wouldn't do that. Have one servlet to generate the form and a second servlet to process the posted data.

But perhaps I haven't understood your question either.
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
Okay, lemme try again.

I have a form containing user details. name, phone, email, etc. I need to display to the user their current information and allow them to update it. Once they've done any updates, I need to take those updates and save them to the database.

Right now I have a solution that works, but it's a bit of a hack in my opinion and there has to be a better way to do it.

I'm using a servlet to generate the form so that I can populate the form with the users current information. However, right now that means I have the pages html coded in the servlet. Which AFAIK is a bad practice.

The servlet that the form submits to and database stuff I have working fine. It's just the initial form setup that I'm trying to figure out a better method for.

Sorry for the confusion.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Indeed, code the HTML in the JSP page and let the servlet forward the request to the JSP the usual way.
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
Bauke Scholtz wrote:Indeed, code the HTML in the JSP page and let the servlet forward the request to the JSP the usual way.


Okay, I haven't done JSP stuff before, so I'm not sure what the usual way is. I've been doing my pages as html pages with the form inside. How do I set up a JSP form and get my data to it?
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Just the same way as you did in servlet, but then in JSP.
Instead of scriptlets or string concatenations, you just use taglibs and EL.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

If you are new to JSP, please read the following:

The Secret Life of JSPs

The Front Man

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bai Shen
Ranch Hand

Joined: Sep 24, 2008
Posts: 323
Bear Bibeault wrote:If you are new to JSP, please read the following:

The Secret Life of JSPs

The Front Man


Ah, okay. I get it now. Off to make a JSP template.

Now I have another question, though.

Once I save my information to the db, I want to return a status page to the user. Is there a particular convention to do that? Again, right now I'm just creating html code inside of my servlet.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Same deal -- after the servlet is done with its job, it forwards to a JSP to produce the HTML page to send to the browser.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic forms and pages
 
Similar Threads
Saving and Restoring Form Data
Add Multiple rows in Servlet
Is using servlets means using J2EE?
proxy timeout causing problems in servlet [serious]
Form Problem