This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes JSP and the fly likes Filling a Form based on previous JSP data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Filling a Form based on previous JSP data" Watch "Filling a Form based on previous JSP data" New topic
Author

Filling a Form based on previous JSP data

Sam Gardner
Ranch Hand

Joined: Oct 02, 2007
Posts: 31
I have an HTML form on my site that is processed by a JSP page. If the process page detects any basic errors, it forwards it onto the FormRetry page. I'd like the data originally entered to be the initial values of the FormRetry page, so the user can see what they originally entered and hopefully figure out what the problem was based on the errors displayed. I've been trying to do it with sessions, but <TYPE = ... VALUE = <%session value%>> doesn't seem to work too well.

Anyone have any suggestions? If this is more of an HTML issue, feel free to send me over to the other forum. I figured it went here because I needed to get something from a previous JSP page.

Thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

but <TYPE = ... VALUE = <%session value%>> doesn't seem to work too well.

"doesn't seem to work too well" isn't a great description of the issue.

Your HTML is well-formed, is it not (unlike your example)?
[ November 08, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sam Gardner
Ranch Hand

Joined: Oct 02, 2007
Posts: 31
The pages display correctly, if that's what you're getting at. The FormRetry displays the correct errors, it just doesn't display the right data in the Form when you first bring up the page.

My instinct was to try <INPUT TYPE=TEXTVALUE = <%session.getAttribute(foo)%> MAXLENGTH = 4 NAME=3 SIZE=4> but that does not work. It displays "MAXLENGTH" (without the quotes) in each field that I dynamically specify.

Correct me if I'm wrong, but just saying <%session.getAttribute(foo)%> doesn't actually generate any HTML for me.

What I'm trying to get at is
<INPUT TYPE=TEXT VALUE = "VALUE of session.getAttribute(foo)" MAXLENGTH = 4 NAME=3 SIZE=4>
[ November 08, 2007: Message edited by: Sam Gardner ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66


Is that really what your code looks like? It's got problems.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66



First, ditch the uppercase; it really marks you as a n00b.

Second, always quote all attribute values. It doesn't matter if HTML let's you get away with it in some cases. Always quote.

Thirdly, learn the difference between <% %> and <%= %>. Or better yet, move into the modern era and use the EL.

That should get you going in the right direction.
[ November 08, 2007: Message edited by: Bear Bibeault ]
Sam Gardner
Ranch Hand

Joined: Oct 02, 2007
Posts: 31
Originally posted by Bear Bibeault:

Is that really what your code looks like? It's got problems.


Yes, I realize that, hence the topic :P

All I want is for each line of text input to display the previously entered text input.

For example, if the user entered in 192-04-17 for a date field, I want the page displaying errors to display 192-04-17 in the date field, with errors next to it before the user changes the value, if that makes any sense.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

So what happens when you apply my suggested improvements?
Sam Gardner
Ranch Hand

Joined: Oct 02, 2007
Posts: 31
Well, using the <%= %> tags seems to work better, as the field will now display the results of the session.getAttribute(foo) correctly.

However, it's currently getting 'null' from the session, and I can't figure out why, as the page that sets the relevant session attributes is the process page (and the process page forwards to the FormRetry page if errors are detected). Other pages that are accessed after the process page get the same session attributes fine, so I'm a bit confused.
[ November 08, 2007: Message edited by: Sam Gardner ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

Originally posted by Sam Gardner:
Well, using the <%= %> tags seems to work better, as the field will now display the results of the session.getAttribute(foo) correctly.
Do you understand the difference?

And, I'm assuming that it's session.getAttribute("foo"), with the quotes. Such tings are important when posting code. Please be complete and accurate.

as the page that sets the relevant session attributes is the process page
Process page, not servlet?

(and the process page forwards to the FormRetry page if errors are detected).

If you are forwarding to the JSP from the processing resource, there's no need to use the session. Placing the scoped variables on the request is much better.
Sonny Gill
Ranch Hand

Joined: Feb 02, 2002
Posts: 1211

As Bear said, if you are forwarding (and not redirecting), there is no need for setting those values as session attributes.
Moreover, when forwarding, the original request parameters are still available, so you can just access them using request.getParameter().

If you can reduce your two JSPs to the minimum code needed to generate this problem, and post it here, somebody may be able to spot why the session attribute is null.

Mmm..I hope you picked up what Bear was saying about using quotes around the value passed to the value attribute of the input element.


The future is here. It's just not evenly distributed yet. - William Gibson
Consultant @ Xebia. Sonny Gill Tweets
Sam Gardner
Ranch Hand

Joined: Oct 02, 2007
Posts: 31
Alright, I'll try to explain what I'm doing with the relevant pages and why I'm doing things this way.

When a user logs into the site I'm creating, they have various options related to data entry into a mySQL server. The option I'm currently trying to implement lets the user enter the personal data for a participant in a survey into the database. The steps I've implemented to do that are as follows (all of the following pages are displayed in an iframe running inside of a main Display.jsp page for the site, to keep everything looking consistent):

1) User gets to a plain old HTML page called Survey.html. Nothing out of the ordinary here; the page just displays a basic form in an iframe. The form's target is the second page SurveyProcess.jsp.

2)SurveyProcess.jsp page doesn't do much other than define a few variables based on what the user entered for the participant's name, DOB, etc. It checks each field against what is expected (ie Dates are in proper form, the participant's ID is in the correct form, etc) and adds an error to the error vector that is created in the bean. If, once it's done processing the data, it doesn't find any errors, the page creates a temporary table in mySQL (named based on ('temp' + "participant's ID") and forwards to VerifySurvey.jsp. If it DOES find errors, it forwards to SurveyRetry.jsp. Both of these forwards are done with pageForward.context("target").

3) VerifySurvey.jsp creates a "display form" (it just displays data, and makes the user click "submit" to get the data into the actual database. Eventually there will be an option to further edit the data, but I need to figure out how to pre-fill forms correctly for that to be useful). The form targets VerifySurveySubmit.jsp, which calls the appropriate methods from SurveyBean to move the temp file into the actual database, and it also deletes the temp file. If the methods succeeded, everything forwards back to
Survey.html. If the ID is already in the database, it says so, and forwards back to Survey.html. If other errors resulted, it outputs them. The error output is mainly "just in case" right now, as SurveyProcess did enough that everything should be in the right form for mySQL to read.

4) SurveyRetry.jsp is basically a clone of Survey.html, but with an added area for errors (Survey.html's form is created in a table; I just added an extra column which holds any errors for each field in the form). Right now, the errors display perfectly (e.g. if there was an error with the ID the error message is displayed in the correct place for the ID) but the original data entered into the field does not. I want the form to display everything the way it was originally entered into Survey.html, so that the user doesn't have to re-enter all of the fields every time they input something wrong. The target of SurveyRetry.jsp is the same as Survey.html, so it keeps getting checked over until everything on it is in the right form.

I'm not very experienced at all with web development on this scale, and I've been going based on the Sun Java documentation and random JSP tutorials I've found online that are kind of doing the same thing I am. Needless to say, what I have so far results in a fairly fragmented understanding of JSP, so while I think my structure is fairly well put-together (at least for my uses; this basic structure is also going to be used for entering in survey data at some point), the implementation of the server requests and session data is not very well defined, and I'm sort of afraid to change what I have going right now without a clear idea of how to make it work in a manner more befitting the modern era (I'm pretty sure some of the tutorials I've been using were written in the late 90's). So, if any of you JSP experts could enlighten me as to how I should be keeping the data first entered into Survey.html persistent (whether to use <jsp:forward> tags or session.setAttribute() statements at some point, or some other technique), I would be very appreciative.

Appendix A) The bean that all of these pages use is called SurveyBean, and has a few methods for mySQL manipulation (so that my site actually generates useful data to stick in the database) as well as stores the current errors and various other miscellaneous "housekeeping" methods.
[ November 09, 2007: Message edited by: Sam Gardner ]
 
GeeCON Prague 2014
 
subject: Filling a Form based on previous JSP data