aspose file tools*
The moose likes Servlets and the fly likes Servlet problem concerning form data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet problem concerning form data" Watch "Servlet problem concerning form data" New topic
Author

Servlet problem concerning form data

arpit singla
Ranch Hand

Joined: Jun 18, 2001
Posts: 66
I have a form data to be submitted to a servlet and then to database, but if any field is left blank in the form data, i want to go back to the form data page(html page), giving information about the blank fields.just like registration page on any website.. I dont know how to call that html page in my servlet ? Is it servlet chaining ?
if anybody could help me..
thanks a lot
Steve Chernyak
Ranch Hand

Joined: Oct 19, 2000
Posts: 113
if it is possible reneame the page to a .jsp
ant do the following
content = "yourpage.jsp"
RequestDispatcher dispatcher = getServletContext.getRequestDispatcher(content);

dispatcher.forward(request, response);
arpit singla
Ranch Hand

Joined: Jun 18, 2001
Posts: 66
Steve
where do i put this code ? jsp or servlet? will it take care of the fact that i need to display the fields which have already been filled by the user so that he need to key in the fields left blank.
thanks for the reply.
Steve Chernyak
Ranch Hand

Joined: Oct 19, 2000
Posts: 113
you should put this code into the servlet that you are posting to initially.
When you do this forward you are passing the jsp the request object which you can then use to repopulate the fields.
ex.
<input type=text name=yourField value="<%=request.getParameter("yourField")%>">
Hope this helps
arpit singla
Ranch Hand

Joined: Jun 18, 2001
Posts: 66
Steve
thanks for your valuable advice. i will try to implement it the way u said.
arpit
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
You can also do the validation upfront using javascript. Just don't allow the form to be submitted if any fields are blank. That will at least save you a trip to the server.

Bosun


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
arpit singla
Ranch Hand

Joined: Jun 18, 2001
Posts: 66
Steve
When I use the following code for my JSP:
<b> First Name: </b>  
<input type="text" name="firstName" value="<%request.getParameter("firstName")%>" size="40" maxlength="40">
i see the "<%request.getParameter("firstName")%>" code filled in in the text field corresponding to First Name. JSP is giving me error when dispayed on browser.
error is :
org.apache.jasper.JasperException: Unable to compile class for JSPC:\tomcat\work\localhost_8080%2Fexamples\_0002fjsp_0002fregister_0002ejspregister_jsp_0.java:61: Invalid type expression.
request.getParameter("firstName")
if Steve or sb. else could help me.
thanks
Vadivel S
Greenhorn

Joined: May 26, 2001
Posts: 4
Hi,
Always try to use javascript to validate form elements. B'cos javascript will be executed in the client end (browser).
Just a function
<SCRIPT language="javascript">
function validate()
{
if(document.formname.elementname.value =='')
{
alert('<your own message')
return;
}
}
</script>
will help u in solving the problem.
Regards
Vels

Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
Originally posted by Vadivel S:
Hi,
Always try to use javascript to validate form elements. B'cos javascript will be executed in the client end (browser).
Just a function
<SCRIPT language="javascript">
function validate()
{
if(document.formname.elementname.value =='')
{
alert('<your own message')
return;
}
}
</script>
will help u in solving the problem.
Regards
Vels


Client-side validation using JavaScript is great to provide immediate feedback to the user about any problems encountered with the form, since he receives notification without having to wait for a submit and response to/from the server. However, as a programmer, you CANNOT rely on client-side validation to ensure valid data is being submitted to your server-side application.
You should ALWAYS perform server-side data validation. What if the client has turned off JavaScript? What if the client is trying to submit malicious data to your servlet, possibly using a form of his own creation? You should never count on the client providing valid data. As I said, clien-side validation is a convenience provided to the user, but server-side validation should be required in all cases.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet problem concerning form data
 
Similar Threads
can servlets send email???????????
How can I pass html form element values to applet?
autoentry of fields on an html page
dynamically send user back
Dynamic forms and pages