Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form processing on resubmit

Status
Not open for further replies.

preethib

Programmer
Jul 20, 2000
39
IN
Hi,

Below is the HTML form, with few form fields. on 'submit' of HTML form, I call the servlet. Basically what I want the servlet to do is if all the fields are filled return 'Thank You' information. Else the servlet should show an incomplete form as
&quot;**** FieldName = <text box>&quot;
And, on resubmit, the form should be reprocessed by the servlet and acknowledged(Thank You).

I am sure it's a trivial question. But the thing is I am beginner with JSP.
++++++++++++++++++++++++
ProcessInfo.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ProcessInfo extends javax.servlet.http.HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

res.setContentType(&quot;text/html&quot;);

//String firstName, lastName;
//String address, phone, fax, email;


//String fn, ln, addr, fax, email, phno;
//String address, phone, fax, email;
String fn = req.getParameter(&quot;FirstName&quot;);
String ln = req.getParameter(&quot;LastName&quot;);
String addr = req.getParameter(&quot;Address&quot;);
String phno = req.getParameter(&quot;PhoneNumber&quot;);
String fax = req.getParameter(&quot;FaxNumber&quot;);
String email = req.getParameter(&quot;EmailAddress&quot;);
boolean all_info;

PrintWriter pw = res.getWriter();

Enumeration paramNames = req.getParameterNames();

pw.println(&quot;<html>&quot;);
pw.println(&quot;<head><title>Processing Information Form</title><head>&quot;);
pw.println(&quot;<body>&quot;);

all_info = true;

pw.println(&quot;<form action=\&quot;servlet/ProcessInfo\&quot; method=\&quot;Get\&quot;>\n&quot;);

if ( ((fn.equals(&quot;&quot;) || fn == null))|| ((ln.equals(&quot;&quot;) || ln == null)) || ((addr.equals(&quot;&quot;) || addr == null)) || ((phno.equals(&quot;&quot;) || phno == null)) || ((fax.equals(&quot;&quot;) || fax == null)) || ((email.equals(&quot;&quot;) || email == null)) ) {
all_info = false;
}
else {
pw.println(&quot;<BR> FirstName&quot; + &quot;\t&quot; + fn +&quot;<BR>\n&quot;);
pw.println(&quot;<BR> LastName&quot; + &quot;\t&quot; + ln +&quot;<BR>\n&quot;);
pw.println(&quot;<BR> Address&quot; + &quot;\t&quot; + addr +&quot;<BR>\n&quot;);
pw.println(&quot;<BR> PhoneNumber&quot; + &quot;\t&quot; + phno +&quot;<BR>\n&quot;);
pw.println(&quot;<BR> Fax&quot; + &quot;\t&quot; + fax +&quot;<BR>\n&quot;);
pw.println(&quot;<BR> EmailAddress&quot; + &quot;\t&quot; + email +&quot;<BR>\n&quot;);
}
pw.println(&quot;</form>&quot;);

//while(paramNames.hasMoreElements()) {
// String paramName = (String)paramNames.nextElement();
// String[] paramValues = req.getParameterValues(paramName);
// String paramValue = paramValues[0];
//if (paramName != null || paramValue.length() != 0) {
// pw.println(&quot;<BR>&quot; + paramName + &quot;\t&quot; + paramValue +&quot;<BR>\n&quot;);
// }
//else if (paramName == null || paramValue.length() == 0){
// all_info = false;
// pw.println(&quot;<BR>*&quot; + paramName + &quot;\t<input type=\&quot;text\&quot;&quot;+ &quot;Name=&quot; + paramName + &quot;value=\&quot;\&quot; size=\&quot;20\&quot;>&quot; +&quot;<BR>\n&quot;);
// }
//else {
// pw.println(&quot;<BR>&quot; + paramName + &quot;\t&quot; + paramValue +&quot;<BR>\n&quot;);
// }
// }

pw.println(&quot;<BR>&quot;);
//pw.println(&quot;<BR>&quot;);

if (all_info == false){
pw.println(&quot;<form action=\&quot; method=\&quot;Post\&quot;>\n&quot;);
pw.println(&quot;Please enter all information&quot; + &quot;<BR>&quot;);
pw.println(&quot;<BR>&quot;);
pw.println(&quot;<input type=\&quot;submit\&quot; value=\&quot;Submit\&quot;><BR>&quot;);
pw.println(&quot;</form>&quot;);
}
else if (all_info == true){
pw.println(&quot;Thank You&quot; + &quot;<BR>&quot;);
}

pw.println(&quot;</body>&quot;);
pw.println(&quot;</html>&quot;);
pw.close();

}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}

}
++++++++++++++++++++++++
Info.html

<html>
<head>
<title>Lab01-JSP Programming:Info.html</title>
</head>

<!---- Info.html --->
<body>

<H4> Write an HTML page with a form that takes first name,last name,address,phone number, email address with a submit button</H4>

<form action=&quot;servlet/ProcessInfo&quot; method=&quot;Post&quot;>
Enter:<BR>
<BR>
Fist Name:<BR><input type=&quot;text&quot; Name=&quot;FirstName&quot; value=&quot;&quot; size=&quot;20&quot;><BR>
Last Name:<BR><input type=&quot;text&quot; Name=&quot;LastName&quot; value=&quot;&quot; size=&quot;20&quot;><BR>
Address:<BR><input type=&quot;text&quot; Name=&quot;Address&quot; value=&quot;&quot; size=&quot;20&quot; maxlength=120><BR>
Phone:<BR><input type=&quot;text&quot; Name=&quot;PhoneNumber&quot; value=&quot;&quot;><BR>
Fax:<BR><input type=&quot;text&quot; Name=&quot;FaxNumber&quot; value=&quot;&quot;><BR>
Email:<BR><input type=&quot;text&quot; Name=&quot;EmailAddress&quot; value=&quot;&quot;><BR>
<BR>
<input type=&quot;submit&quot; value=&quot;Submit&quot;><BR>
</form>

</body>
</html>
 
The best way to do this would be not to use JSP or Servlets but Javascript (or some other form of Client Side Programming). This way the clients maching will do the checking before the form is submitted. Same result but the server has less work too do and client doesn't have to wait for page to be reloaded

for example

Info.html

<script language=&quot;Javascript&quot;>
function isEmpty(field){
if(field.length() == 0)
return true
else
return false
}

function validate(form){
if(isEmpty(form.FirstName) || ...){
alert(&quot;Not all fields are completed&quot;)
return false
}//end if
else
return true
}//end function validate

</script>

<html>
<head>
<title>Lab01-JSP Programming:Info.html</title>
</head>

<!---- Info.html --->
<body>

<H4> Write an HTML page with a form that takes first name,last name,address,phone number, email address with a submit button</H4>

<form action=&quot;servlet/ProcessInfo&quot; method=&quot;Post&quot; onSubmit=&quot;return validate(this)&quot;>
Enter:<BR>
<BR>
Fist Name:<BR><input type=&quot;text&quot; Name=&quot;FirstName&quot; value=&quot;&quot; size=&quot;20&quot;><BR>
Last Name:<BR><input type=&quot;text&quot; Name=&quot;LastName&quot; value=&quot;&quot; size=&quot;20&quot;><BR>
Address:<BR><input type=&quot;text&quot; Name=&quot;Address&quot; value=&quot;&quot; size=&quot;20&quot; maxlength=120><BR>
Phone:<BR><input type=&quot;text&quot; Name=&quot;PhoneNumber&quot; value=&quot;&quot;><BR>
Fax:<BR><input type=&quot;text&quot; Name=&quot;FaxNumber&quot; value=&quot;&quot;><BR>
Email:<BR><input type=&quot;text&quot; Name=&quot;EmailAddress&quot; value=&quot;&quot;><BR>
<BR>
<input type=&quot;submit&quot; value=&quot;Submit&quot;><BR>
</form>

</body>
</html>
 
Even when using Javascript you should be doing Server-side validation as well. If someone has Javascript disabled in their browser then you have the possibility of very unexpected results.
 
Hi wushutwist,

Is JS the only solution, I have been assigned a task where the servlet needs to do 3 things:

1. Check to see all the form values are entered
2. If values are missing send a form back to the user that looks like this:
---ProcessInfo servlet output------------
First Name: Input eneterd
Last Name: Input eneterd
Address: Input eneterd
***Phone Number: <INPUT TEXTFIELD>
***Fax Number: <INPUT TEXTFIELD>
Email Address: Input eneterd

<SUBMIT> button
-------------------------------------
3.On submit, the servlet should reprocess, the form if user enters the missing fields.

I want to know if this is possible.

Preethi

----------------------------------------
//ProcessInfo.java program

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ProcessInfo extends javax.servlet.http.HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

res.setContentType(&quot;text/html&quot;);

String firstName, lastName;
String address, phone, fax, email;
boolean all_info;

PrintWriter pw = res.getWriter();

Enumeration paramNames = req.getParameterNames();

pw.println(&quot;<html>&quot;);
pw.println(&quot;<head><title>Processing Information Form</title><head>&quot;);
pw.println(&quot;<body>&quot;);

all_info = true;

while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
String[] paramValues = req.getParameterValues(paramName);
String paramValue = paramValues[0];

if (paramName == null || paramValue.length() == 0){
pw.println(&quot;<BR>****&quot; + paramName + &quot;;<BR>\n&quot;);

//&quot;\t<input type=\&quot;text\&quot;&quot;+ &quot;Name=&quot; + paramName + &quot;value=\&quot;\&quot; size=\&quot;20\&quot;>&quot; +&quot;<BR>\n&quot;);

all_info = false;
}
else {
pw.println(&quot;<BR>&quot; + paramName + &quot;\t&quot; + paramValue +&quot;<BR>\n&quot;);
}

}

pw.println(&quot;<BR>&quot;);
pw.println(&quot;<BR>&quot;);



if (all_info == false){
pw.println(&quot;Please enter all information&quot; + &quot;<BR>&quot;);
pw.println(&quot;<form action=\&quot; method=\&quot;Post\&quot;>\n&quot;);
pw.println(&quot;<input type=\&quot;submit\&quot; value=\&quot;Return to Form\&quot;><BR>&quot;);
pw.println(&quot;</form>&quot;);
}
else if (all_info == true){
pw.println(&quot;Thank You&quot; + &quot;<BR>&quot;);
}
pw.println(&quot;</body>&quot;);
pw.println(&quot;</html>&quot;);
pw.close();

}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}

}
 
Hi wushutwist,

Just one bit I wanted to make clear from my previous posting;

3. On 'submit' form (of step 2), the form should be sent back to servlet 'ProcessInfo.java'.

My program posted earlier kind of takes the user to the Info.html page for re-entering.

Preethi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top