Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working with a servlet project in which the user inputs data in a form and if  the terms and conditions are not met then the form is displayed.
However, the above functionality does not seem to work?
How do I fix it?

I was expecting that when I run the app on netbeans and don't click on terms and conditions agree the browser displays the form with details with the text "the terms and consitions have not been agreed"

However, the form is displayed without the details I entered.

I am using NetBeans.


index.html

<!DOCTYPE html>
<html>
    <head>
        <title>Form</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <style>
            .container{
                width:40%;
                border:1px solid black;
                margin:auto;
                padding:20px;
                font-size:20px;
            }
             
            #my-form tr td input{
                font-size:20px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>My Form</h1>
            <form id="my-form" action="RegisterServlet" method="post">
                <table>
                    <tr>
                        <td>
                            Name:
                        </td>
                        <td>
                            <input type="text" name="user_name" placeholder="Enter your name"/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Password:
                        </td>
                        <td>
                            <input type="password" name="user_password" placeholder="Enter your password"/>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Email:
                        </td>
                        <td>
                            <input type="email" name="user_email" placeholder="Enter your email"/>
                        </td>
                    </tr>
                     <tr>
                        <td>
                            Gender:
                        </td>
                        <td>
                            <input type="radio" name="user_gender"/>Male    <input type="radio" name="user_gender" />Female
                        </td>
                    </tr>
                     <tr>
                        <td>
                            Course:
                        </td>
                        <td>
                            <select name="user_course">
                                <option value="Java">Java</option>
                                <option value="C++">C++</option>
                                <option value="C">C</option>
                                <option value="Python">Python</option>
                            </select>
                        </td>
                    </tr>
                    <tr style="text-align:center">
                        <td>
                             <input type="checkbox" value="checked" name="conditiontocheck"/>
                        </td>
                        <td>
                            Agree terms and conditions
                        </td>
                    </tr>
                    <tr style="text-align:center">
                        <td>
                              
                        </td>
                        <td>
                            <button type="submit">Register</button>
                             <button type="reset">Reset</button>
                        </td>
                    </tr>
                </table>
            </form>
        </div>
    </body>
</html>


RegisterServlet.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.packages;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;
 
/**
 *
 * @author soura
 */
public class RegisterServlet extends HttpServlet{
    @Override
    public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        out.println("<h1>Welcome to Register Servlet</h1>");
        String name=request.getParameter("user_name");
        String password=request.getParameter("user_password");
        String email=request.getParameter("user_mail");
        String gender=request.getParameter("user_gender");
        String course=request.getParameter("user_course");
         
         String condition=request.getParameter("conditiontocheck");
          
         if(condition!=null){
            if(condition.equals("checked")){
                out.println("<h2>Name:</h2>"+name);
                out.println("<h2>Password:</h2>"+password);
                out.println("<h2>Email:</h2>"+email);
                out.println("<h2>Gensder:</h2>"+gender);
                out.println("<h2>Course:</h2>"+course);      
            }else{
             out.println("The terms and conditions have not been applied."); 
            }
         }
         else{
            out.println("The terms and conditions have not been applied."); 
            RequestDispatcher rd=request.getRequestDispatcher("/index.html");
            rd.include(request,response);
         }
    }   
}


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
   version="4.0">
     
    <servlet>
        <servlet-name>
            Form
        </servlet-name>
        <servlet-class>
            com.packages.RegisterServlet
        </servlet-class>
    </servlet>
     
     
    <servlet-mapping>
        <servlet-name>
            Form
        </servlet-name>
        <url-pattern>
            /RegisterServlet
        </url-pattern>
    </servlet-mapping>
     
     
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>


What I have tried:

I have asked this question on stackoverflow, reddit and on coderanch but nobody has replied.
Please help
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900