Calling forms from within a servlet and returning that information to a database

Calling forms from within a servlet and returning that information to a database

I know what I want to do, but I'm not sure the best or right way to do it, and I'm not finding quite what I'm wanting on Google.

I have a java servlet that accesses a sql database and displays the information in the browser.
I want to add a button that would bring up another browser page/tab containing a form allowing me to fill in fields that would be added to my sql database when a Submit button is pressed.
The original servlet would then update, either automatically (preferably) or by hitting the browser's refresh button, and show the new entry.

Do I need a form in the original servlet for the button to call the other page/form with?
Or should the new page I want to generate be a separate servlet, for which my original web.xml would contain another entry or just another form in my original servlet?

Hopefully that makes sense.

That comment indicates to me that you have not yet fully absorbed the principles servlets use.

Once servlet A has gotten a request and created a response web page, it no longer knows a thing about this particular client. It is not sitting there waiting to send more data.

It will take a separate request to servlet A to get a new page with modified data.

Time to break out the whiteboard or pencil and paper and diagram the data flow.

Thanks for that bit of information; I wasn't sure. I just barely jumped into servlets 2 days ago at my boss's request.
You addressed one part of my question, but I don't feel like I fully understand just what needs to happen. I'm trying.

Am I talking about calling another servlet from the 1st servlet, both of which might contain a form?

Maybe point me in the right direction or something. I don't think I understand enough to diagram the data flow yet.

You are making a classic mistake of trying to make each component do too much.

Here's a typical flow:

A servlet (page controller) receives a request to display the page with the form.
The controller gathers any data that's need, if any (e.g. inital values for the form fields), and puts those values into request scope as scoped variables (via setAttribute()).
The controller forwards to a JSP that defines the HTML to be sent to browser. Once the response is sent, the controller and JSP are done. Finis. No more.
The user interacts with the page and eventually hits the submit button.
The form is submitted to a different servlet (task controller) with responsibilities for updating the database (preferably by interacting with UI-agnostic model classes).
The task controller redirects to the page controller for whatever page is to be shown next. If it's the form again, it redirects to the page controller servlet for the form.
Notice how this differs from your original scenario. Important points are:

[1] [2] [3] [4] Next

Copyright © 2007-2012 www.chuibin.com Chuibin Copyright