JSP - saving textfield values before loading 'intermediate' page

R

Rico

Here's the scenario:

We have a JSP page that takes user's input to produce a document.
The user would need to upload an attachment for the document.
The upload is performed by a servlet, which causes us to leave the current
page of input. But we want to preserve the data the user has already input
in the textfields.

So, we're looking at using the session scope. What is the best way to save
the values already input to a session Object before calling the upload
servlet?
I'm thinking most parameters on the JSP page have no business being in the
upload servlet and it'd be messy to get it to save said parameters.

I'm just wondering whether I'd be reinventing the wheel, or have the wrong
conception, because preserving state is a 'standard' aspect of web
programming. My idea is to have a dedicated helper servlet that would
perform this task of saving the parameters already input to the session
Object before redirecting us to the upload page.

Is there a simpler way?

Rico.
 
K

kaeli

So, we're looking at using the session scope. What is the best way to save
the values already input to a session Object before calling the upload
servlet?

A bean with session scope.

--
 
R

Rico

A bean with session scope.

Thanks.
More specifically, my question is how do I save the textfield values
present into the fields of the bean.
Is a request mandatory?
If so, should the task be delegated to the next page where some parameters
might not have any business being at all, if only for being 'saved'
or do I need a dedicated servlet for the purpose of saving parameters and
afterwards calling the required following page?

Is something like
sessionBean.setProductID(request.getParameter("productId"))
the only (best?) way to commit textfield values to the session?

Rico.
 
A

Andrea Desole

Rico said:
Thanks.
More specifically, my question is how do I save the textfield values
present into the fields of the bean.
Is a request mandatory?

well, a request is needed to get the session. Anyway, I don't see the
problem. You always get a request for free.

If so, should the task be delegated to the next page where some parameters
might not have any business being at all, if only for being 'saved'
or do I need a dedicated servlet for the purpose of saving parameters and
afterwards calling the required following page?

if you want to save them in the next page, you will have to use forward.
If you use redirect you will reach the next page without parameters.
Even using forward you will have to save the parameters in the form as
hidden values, or build some logic in the jsp code to save them when the
page is loaded. To be short: don't save them in the next page.
Besides, it makes sense that the first page, with the first form, has a
servlet that takes care of it. You can (and maybe should) use a session
bean to store the data for more servlets, but let each servlet save its
own part.
Is something like
sessionBean.setProductID(request.getParameter("productId"))
the only (best?) way to commit textfield values to the session?

Something like that, yes

I would also like to comment that using a session bean is not always the
best way. You might get into troubles if the user can edit two forms at
the same time. That also depends on how your requirements are.
 
K

kaeli

More specifically, my question is how do I save the textfield values
present into the fields of the bean.
Is a request mandatory?

Yes. The form is on the client. To get the values on the server, a request
must be issued.
How that is accomplished is highly dependent on what you're trying to do.
For example, if the user initiates a "save" type deal, they can just press a
form submit button that submits to a JSP page that has a bean set up with the
proper matching setters for each field.
If you're trying to do this independent of user action, you have a few
options, all of which are prone to failure. ;)
Or they require javascript on the client, which has its own pitfalls.

I assume the values you want saved are values the user changed AFTER the page
was rendered and sent to the client. If not, this is all a moot point. Just
save them before you render the page.
If so, should the task be delegated to the next page where some parameters
might not have any business being at all, if only for being 'saved'
or do I need a dedicated servlet for the purpose of saving parameters and
afterwards calling the required following page?

If this is typical of your application, you should be using an MVC
architecture and have a controlling servlet do all that work. That would be
similar to your latter choice above.
If this is a one shot deal, just save them in an intermediate page and
forward on the request.

Do keep in mind that a user more than likely can have two browsers, and thus
two sessions, open at the same time. All web apps should be written with this
in mind.

--
--
~kaeli~
Persons disagreeing with facts are always emotional and
employ faulty reasoning.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top