reset password
Author Message
venny
Posts: 61
Posted 17:14 Oct 22, 2015 |

Hi, I'm working on the Homework and I'm having trouble with the data validation,   if we find an error, we have to go back to the registration page with the form filled out with the previous info.  Do we have to use cookies? sessions?  Can I just pass parameters around?

GeorgeAria
Posts: 14
Posted 17:57 Oct 22, 2015 |

So I assume what you're asking is how to make your form elements sticky(at least, the ones that were validated).  There are different ways to make a form element sticky. I believe you can use cookies to do this, since they can store data in the client's computer and you could access that data when you needed it(not 100% though since I'm not using them in my code).  Just make sure to only send back data that is DEFINETELTY validated, like if they I inputted a real email address. There shouldn't be any incorrect data in the registration form  if you send the user back there. Hope this helps, and good luck on the midterm!! :)

 

 

acervantes
Posts: 28
Posted 20:57 Oct 22, 2015 |

 

This assignment does not require the use of cookies or sessions.

Think about the problem this way.  When the form is submitted via a POST method, we process the form data in the doPost method.  If there are any errors, we are to simply re-display the form.  If your doGet method was created to display the form, then you can simply forward the request and response objects to the doGet from within the doPost method.

If any errors occur during your validation, you can simply attach an error flag to your request object.  Keep in mind that we have discussed 3 scopes this quarter: Application, Session, and Request.  The request scope is the most ephemeral of the scopes.  Data stored in the request scope lasts only for the duration of the request.  

Since we only care to display error messages based on the data of a single request, it follows that we can store "flags", or other messages, in the request object itself.  Then, if we forward the request to the doGet, any values that we've added to the request object from within the doPost would be available in the doGet.

Example:

From within the doPost method you can validate the name field:

// Pseudocode:  doPost method
...
if ( nameHasError )
  request.setAttribute("nameError", true);
...
doGet(request, response);

// Now, from within doGet you could write
...
if (request.getAttribute("name") == true)
  out.println("<p class=\"text-error\">...</p>");
...

So, in short, you do not need to use cookies or sessions.