my servlets not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Naha
    New Member
    • Feb 2007
    • 41

    #1

    my servlets not working

    hi, my servlet is not linking with my jsp, when i click on submit it does not load up the page. and i have mapped to the DD.

    Code:
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    
    
    public class Servlet1 extends HttpServlet {
    
    
    
    public void doPost(HttpServletRequest req,
    HttpServletResponse res) throws ServletException, IOException
     
    { 
    
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
    
    String firstname = req.getParameter("firstname");
    
    String lastname = req.getParameter("lastname");
    
    
    
    out.println("<html><body>");
    
    
    
    out.println(firstname);
    
    out.println(lastname);
    
    
    
    out.println("</body></html>");
    
    out.close();
    
    }
    }



    Code:
    <html>
    <head><title>Registration Details</title>
    <SCRIPT LANGUAGE="JavaScript">
    
    
    		
    		
    
    
    function processForm1() {
    var form1 = document.formOne;
     if (form1.firstname.value==""){
     alert("you must fill in your first name")
     formOne.firstname.focus();
     }
     else if (form1.lastname.value==""){
     alert("you must fill in your lastname")
     formOne.lastname.focus();
     }
     else if (form1.postcode.value==""){
     alert("you must fill in your postcode")
     formOne.postcode.focus();
     }
     else if (form1.dateofbirth.value==""){
     alert("you must fill in your DOB")
     formOne.dateofbirth.focus();
     
    
     }
     }
     
    function checkPassword (){
    var form1= document.forms["formOne"]
    if (form1.mypassword.value==""){
    alert("Please fill in your password")
    
    }
    password=form1.mypassword.value
      if (password.length != 6){
    alert ("password must be 6 characters long")
    form1.mypassword.focus()
    }
    
    else if (form1.mypassword2.value==""){
    alert("Please confirm your password")
    }
    
    else if (form1.mypassword.value != form1.mypassword2.value){
    alert ("entered password did not match")
    form1.mypassword.focus()
    
    }
    
     else { 
    	controlform = parent.frame2.document.controlform
    	controlform.firstname.value=form1.firstname.value
    	controlform.lastname.value=form1.lastname.value
    	controlform.address.value=form1.address.value
    	controlform.city.value=form1.city.value
    	controlform.county.value=form1.county.value
    	controlform.postcode.value=form1.postcode.value
    	controlform.country.value=form1.country.value
    	controlform.contactphonenumber.value=form1.contactphonenumber.value
    	controlform.dateofbirth.value=form1.dateofbirth.value
    	controlform.emailaddress.value=form1.emailaddress.value
    	controlform.mypassword.value=form1.mypassword.value
    	controlform.mypassword2.value=form1.mypassword2.value
    	 
    }
    }
    
    function checkEmail(){
    var formOne= document.forms["formOne"]
    var re=/^\w+([\.\-]?\w+)@\w+$/;
     if (formOne.emailaddress.value==""){
     alert("you must fill in your email address")
     formOne.emailaddress.focus();
     }
     else if(re.test(document.formOne.emailaddress.value))
     
    return true
    else{
     alert ("invalid email address")
      formOne.emailaddress.focus();
    	
    }
    }
    
    function checkPhone(){
    var form1= document.forms["formOne"]
    var num =/^\d$/;
    if (form1.contactphonenumber.value==""){
    alert("Please fill in your contactPhoneNumber")
    
    }
    
    phoneNum=form1.contactphonenumber.value
     if (num.test(document.formOne.contactphonenumber.value))
    return true
    
    else{
     alert ("phone numbers must be digits only ")
      formOne.emailaddress.focus();
    	
    	phoneNum=form1.contactphonenumber.value
    if (phoneNum.length != 11)
    alert ("Maximum characters for phone number is 11")
    form1.contactphonenumber.focus()
    
    }
    }
    
     
    </script>
    </head>
    
    <body>
    <form method="Post" action="http://localhost:8081/Servlet1" NAME="formOne">
    
    <center>
    <h1>Registration Details</h1>
    <br><br>
    First Name <input type="text" name="firstname" value=""><br><br>
    Last Name <input type="text" name="lastname" value=""><br><br>
    Address <input type="text" name="address" value=""><br><br>
    City <input type="text" name="city" value=""><br><br>
    County (optional) <input type="text" name="county" value=""><br><br>
    Post Code <input type="text" name="postcode" value=""><br><br>
    Country <input type="text" name="country" value=""><br><br>
    Contact Phone Number <input type="text" name="contactphonenumber" value=""><br><br>
    Date of Birth <input type="text" name="dateofbirth" value=""><br><br>
    Email Address <input type="text" name="emailaddress" value=""><br><br>
    Password (minimum 6 characters) <INPUT TYPE=PASSWORD NAME="mypassword"><br><br>
    Confirm Password  <INPUT TYPE=PASSWORD NAME="mypassword2"><br><br>
    <input type="BUTTON" NAME="Submit"  value="--SUBMIT--" onClick="processForm1();checkPassword ();checkEmail();checkPhone();">
    </center>
    </form>
    </body>
    </html>
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Naha
    hi, my servlet is not linking with my jsp, when i click on submit it does not load up the page. and i have mapped to the DD.

    Code:
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
     
     
    public class Servlet1 extends HttpServlet {
     
     
     
    public void doPost(HttpServletRequest req,
    HttpServletResponse res) throws ServletException, IOException
     
    { 
     
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();
     
    String firstname = req.getParameter("firstname");
     
    String lastname = req.getParameter("lastname");
     
     
     
    out.println("<html><body>");
     
     
     
    out.println(firstname);
     
    out.println(lastname);
     
     
     
    out.println("</body></html>");
     
    out.close();
     
    }
    }



    Code:
    <html>
    <head><title>Registration Details</title>
    <SCRIPT LANGUAGE="JavaScript">
     
     
     
     
     
     
    function processForm1() {
    var form1 = document.formOne;
    if (form1.firstname.value==""){
    alert("you must fill in your first name")
    formOne.firstname.focus();
    }
    else if (form1.lastname.value==""){
    alert("you must fill in your lastname")
    formOne.lastname.focus();
    }
    else if (form1.postcode.value==""){
    alert("you must fill in your postcode")
    formOne.postcode.focus();
    }
    else if (form1.dateofbirth.value==""){
    alert("you must fill in your DOB")
    formOne.dateofbirth.focus();
     
     
    }
    }
     
    function checkPassword (){
    var form1= document.forms["formOne"]
    if (form1.mypassword.value==""){
    alert("Please fill in your password")
     
    }
    password=form1.mypassword.value
    if (password.length != 6){
    alert ("password must be 6 characters long")
    form1.mypassword.focus()
    }
     
    else if (form1.mypassword2.value==""){
    alert("Please confirm your password")
    }
     
    else if (form1.mypassword.value != form1.mypassword2.value){
    alert ("entered password did not match")
    form1.mypassword.focus()
     
    }
     
    else { 
    	controlform = parent.frame2.document.controlform
    	controlform.firstname.value=form1.firstname.value
    	controlform.lastname.value=form1.lastname.value
    	controlform.address.value=form1.address.value
    	controlform.city.value=form1.city.value
    	controlform.county.value=form1.county.value
    	controlform.postcode.value=form1.postcode.value
    	controlform.country.value=form1.country.value
    	controlform.contactphonenumber.value=form1.contactphonenumber.value
    	controlform.dateofbirth.value=form1.dateofbirth.value
    	controlform.emailaddress.value=form1.emailaddress.value
    	controlform.mypassword.value=form1.mypassword.value
    	controlform.mypassword2.value=form1.mypassword2.value
     
    }
    }
     
    function checkEmail(){
    var formOne= document.forms["formOne"]
    var re=/^\w+([\.\-]?\w+)@\w+$/;
    if (formOne.emailaddress.value==""){
    alert("you must fill in your email address")
    formOne.emailaddress.focus();
    }
    else if(re.test(document.formOne.emailaddress.value))
     
    return true
    else{
    alert ("invalid email address")
    formOne.emailaddress.focus();
     
    }
    }
     
    function checkPhone(){
    var form1= document.forms["formOne"]
    var num =/^\d$/;
    if (form1.contactphonenumber.value==""){
    alert("Please fill in your contactPhoneNumber")
     
    }
     
    phoneNum=form1.contactphonenumber.value
    if (num.test(document.formOne.contactphonenumber.value))
    return true
     
    else{
    alert ("phone numbers must be digits only ")
    formOne.emailaddress.focus();
     
    	phoneNum=form1.contactphonenumber.value
    if (phoneNum.length != 11)
    alert ("Maximum characters for phone number is 11")
    form1.contactphonenumber.focus()
     
    }
    }
     
     
    </script>
    </head>
     
    <body>
    <form method="Post" action="http://localhost:8081/Servlet1" NAME="formOne">
     
    <center>
    <h1>Registration Details</h1>
    <br><br>
    First Name <input type="text" name="firstname" value=""><br><br>
    Last Name <input type="text" name="lastname" value=""><br><br>
    Address <input type="text" name="address" value=""><br><br>
    City <input type="text" name="city" value=""><br><br>
    County (optional) <input type="text" name="county" value=""><br><br>
    Post Code <input type="text" name="postcode" value=""><br><br>
    Country <input type="text" name="country" value=""><br><br>
    Contact Phone Number <input type="text" name="contactphonenumber" value=""><br><br>
    Date of Birth <input type="text" name="dateofbirth" value=""><br><br>
    Email Address <input type="text" name="emailaddress" value=""><br><br>
    Password (minimum 6 characters) <INPUT TYPE=PASSWORD NAME="mypassword"><br><br>
    Confirm Password <INPUT TYPE=PASSWORD NAME="mypassword2"><br><br>
    <input type="BUTTON" NAME="Submit" value="--SUBMIT--" onClick="processForm1();checkPassword ();checkEmail();checkPhone();">
    </center>
    </form>
    </body>
    </html>
    Do not use out.println in the servlet. Instead use
    Code:
    request.getRequestDispatcher("jspname.jsp").include(request, response);

    Comment

    Working...