0

I try to do a jsp form which would appeal to the the servlet after the Angular.js validation .

I have 3 main files -

default.jsp

<!DOCTYPE html>
<html>
<head>
<!-- CSS -->
<link rel="stylesheet" href="css/bootstrap.min.css" />

<!-- JS -->
<script src="js/angular.js"></script>
<script src="js/app.js"></script>
</head>
<body ng-app="validationApp" ng-controller="mainController">
    <div class="container">

        <!-- =================================================================== -->
        <!-- FORM ============================================================== -->
        <!-- =================================================================== -->

        <!-- pass in the variable if our form is valid or invalid -->
        <form action="AfterFormServlet" method="POST" name="userForm"
            ng-submit="submitForm(userForm.$valid)" novalidate>

            <!-- FIRST NAME -->
            <div class="form-group"
                ng-class="{ 'has-error' : userForm.name.$invalid && !userForm.name.$pristine }">
                <label>First name</label> <input type="text" name="name"
                    class="form-control" ng-model="user.name"
                    ng-pattern="/^[a-z ,.'-]+$/i" required>
                <p ng-show="userForm.name.$invalid && !userForm.name.$pristine"
                    class="help-block">Enter a valid last first name.</p>
            </div>    
            <button type="submit" class="btn btn-primary">Submit</button>    
        </form>    
    </div>
</body>
</html>

AfterFormServlet.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AfterFormServlet
 */
@WebServlet("/AfterFormServlet")
public class AfterFormServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public AfterFormServlet() {
        super();
        // TODO Auto-generated constructor stub
    }


    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Hi");
    }

}

app.js

// create angular app
var validationApp = angular.module('validationApp', []);

// create angular controller
validationApp.controller('mainController', function($scope) {

    // function to submit the form after all validation has occurred            
    $scope.submitForm = function(isValid) {

        // check to make sure the form is completely valid
        if (isValid) {
            alert('Thanks, your order was Sent');
        }
        else {
            alert('Invalid form');
        }

    };

});

After I run it I get into the form , fill the required fields , click on the submit and it get into the servlet even though the validation failed .

How to make it work ?

Update:

following @ug_ suggestion -

<form action="" active="AfterFormServlet" method="POST" name="userForm"
             ng-submit="submitForm(userForm.$valid);" novalidate>

make the validation but doesn't get into the servlet .

And -

<form action="AfterFormServlet" method="POST" name="userForm"
             ng-submit="submitForm(userForm.$valid);" novalidate> 

cause the validation take affect but it get into the servlet even though the validation failed .

2
  • My suggestions: change form action to be action="" and return true with ng-submit. Also quick question, does the alert show? Commented May 8, 2014 at 8:32
  • @ug_ : can you explain me how to write that ? and yeap the alert is shown . Commented May 8, 2014 at 8:36

1 Answer 1

2

You don't have an action attribute for your form, this means that when using the ng-submit directive it will prevent the default behavior causing the form to not submit. Info about it here https://docs.angularjs.org/api/ng/directive/ngSubmit .

Fix:

Add the action="" attribute to your form tag.

<form active="AfterFormServlet" action="" method="POST" name="userForm"
        ng-submit="submitForm(userForm.$valid)" novalidate>

Also I noticed you have some attribute called active, I'm not sure if this was just a mistype and is suppose to be action or if it is related to something else you got going on, that isn't part of the post.

Edit: After looking a bit more I think you should just rework your code a little bit. It seems like a better flow to not use a submit button type but instead just send the submit behavior to a function where you validate the form. So it would look something like this:

$scope.submitForm = function(isValid) {

    // check to make sure the form is completely valid
    if (isValid) {
        alert('Thanks, your order was Sent');
        $scope.userForm.submit();
    }
    else {
        alert('Invalid form');
    }
};

and your html:

<!-- CHANGE HERE -->
<form action="AfterFormServlet" method="POST" name="userForm" novalidate>
    <!-- FIRST NAME -->
    <div class="form-group" ng-class="{ 'has-error' : userForm.name.$invalid &&  userForm.name.$pristine }">
        <label>First name</label>
        <input type="text" name="name" class="form-control" ng-model="user.name" ng-pattern="/^[a-z ,.'-]+$/i" required>
        <p ng-show="userForm.name.$invalid && !userForm.name.$pristine" class="help-block">Enter a valid last first name.</p>
    </div>    
    <!-- CHANGE HERE -->
    <button ng-click="submitForm(userForm.$valid)" class="btn btn-primary">Submit</button>    
</form> 

Alternatively you could use the $http provider in angular to submit your form via AJAX and it wouldn't refresh the page.

Sign up to request clarification or add additional context in comments.

1 Comment

After your edit- still , it doesn't enforce the validation .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.