Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8373323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:39:00+00:00 2026-06-09T14:39:00+00:00

I want to put the interface with add,update, Delete ,Search Functions in JSP. Can

  • 0

I want to put the interface with add,update, Delete ,Search Functions in JSP. Can we put one servlet for all the functions. I have allredy add the add function and I have done validation part for buttons separetly according to my requirement. I have added all the coding below. if we can’t have one servlet for all the actions how can i put separate actions in jquery.

<form id="form-sign-up" class="styled" action="DepartmentServlet" method="post">
            <fieldset>
                <h3>Department </h3>
                <ol>
                    <li class="form-row"><label>Department Id:</label>
                        <input type="text" name="departmentId" id="departmentId" class="text-input required"/><span id="errmsg"></span>
                    </li>
                    <li class="form-row"><label>Department Name:</label>
                        <input type="text" name="departmentName" id="departmentName" class="text-input required"/>
                    </li>
                    <li class="form-row"><label>Department Head:</label>
                        <select name="departmentHead" class="text-input required" id="myDropdown"style="width: 158px"> <option value="">Select Department Head</option>
                            <%

                                String val = "";
                                Department dp = new Department();
                                ResultSet rs = dp.getHeadOfDepartment();
                                while (rs.next()) {
                                    val = rs.getString(2);
                            %>

                            <option value="<%= val%>"><%= val%></option>
                            <%
                                }

                            %>


                        </select>
                    </li>
                    <li class="form-row"><label>Appointed Date:</label>
                        <input id="demo1" name="appointedDate"type="text" maxlength="25" size="25" class="text-input required"><a href="javascript:NewCal('demo1','ddmmmyyyy',true,24)"><img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>
                            <span class="descriptions">pick a date..</span>
                    </li>

                    <li class="button-row">
                        <input type="submit" value="ADD" class="btn-submit img-swap"/>
                        <input type="submit" value="Delete" class="btn-delete img-swap"/>
                        <input type="submit" value="Update" class="btn-update img-swap"/>
                        <input type="submit" value="Search" class="btn-search img-swap"/>
                    </li>
                </ol>
            </fieldset>

$(document).ready(function() { 
    $("#departmentId").keypress(function (e)  
    { 
        //if the letter is not digit then display error and don't type anything
        if( e.which!=8 && e.which!=0 && (e.which<48 || e.which>57))
        {
            //display error message
            $("#errmsg").html("Digits Only").show().fadeOut("slow"); 
            return false;
        }   
    });


    $('.btn-delete').click(function(e){

        // Declare the function variables:
        // Parent form, form URL, email regex and the error HTML
        var $formId = $(this).parents('form');
        var formAction = $formId.attr('action');
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        var $error = $('<span class="error"></span>');

        // Prepare the form for validation - remove previous errors
        $('li',$formId).removeClass('error');

        $('span.error').remove();

        // Validate all inputs with the class "required"
        $('.required',$formId).each(function(){
            var inputVal = $("#departmentId").val();
            var $parentTag = $("#departmentId").parent();
            if(inputVal == ''){
                $parentTag.addClass('error').append($error.clone().text('Required Field'));
            }






            // Run the email validation using the regex for those input items also having class "email"


            // Check passwords match for inputs with class "password"


        });

        // All validation complete - Check if any errors exist
        // If has errors



        if ($('span.error').length > 0) {

            $('span.error').each(function(){

                // Set the distance for the error animation
                var distance = 5;

                // Get the error dimensions
                var width = $(this).outerWidth();

                // Calculate starting position
                var start = width + distance;

                // Set the initial CSS
                $(this).show().css({
                    display: 'block',
                    opacity: 0,
                    right: -start+'px'
                })
                // Animate the error message
                .animate({
                    right: -width+'px',
                    opacity: 1
                }, 'slow');

            });
        } else {
            $formId.submit();
        }
        // Prevent form submission
        e.preventDefault();


    });

    // Fade out error message when input field gains focus
    $('.required').focus(function(){
        var $parent = $(this).parent();
        $parent.removeClass('error');
        $('span.error',$parent).fadeOut();
    });

    $('.btn-submit').click(function(e){

        // Declare the function variables:
        // Parent form, form URL, email regex and the error HTML
        var $formId = $(this).parents('form');
        var formAction = $formId.attr('action');
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
        var $error = $('<span class="error"></span>');

        // Prepare the form for validation - remove previous errors
        $('li',$formId).removeClass('error');

        $('span.error').remove();

        // Validate all inputs with the class "required"
        $('.required',$formId).each(function(){
            var inputVal = $(this).val();
            var $parentTag = $(this).parent();
            if(inputVal == ''){
                $parentTag.addClass('error').append($error.clone().text('Required Field'));
            }






            // Run the email validation using the regex for those input items also having class "email"
            if($(this).hasClass('email') == true){
                if(!emailReg.test(inputVal)){
                    $parentTag.addClass('error').append($error.clone().text('Enter valid email'));
                }
            }

            // Check passwords match for inputs with class "password"
            if($(this).hasClass('password') == true){
                var password1 = $('#password-1').val();
                var password2 = $('#password-2').val();
                if(password1 != password2){
                    $parentTag.addClass('error').append($error.clone().text('Passwords must match'));
                }
            }

        });

        // All validation complete - Check if any errors exist
        // If has errors



        if ($('span.error').length > 0) {

            $('span.error').each(function(){

                // Set the distance for the error animation
                var distance = 5;

                // Get the error dimensions
                var width = $(this).outerWidth();

                // Calculate starting position
                var start = width + distance;

                // Set the initial CSS
                $(this).show().css({
                    display: 'block',
                    opacity: 0,
                    right: -start+'px'
                })
                // Animate the error message
                .animate({
                    right: -width+'px',
                    opacity: 1
                }, 'slow');

            });
        } else {
            $formId.submit();
        }
        // Prevent form submission
        e.preventDefault();


    });

    // Fade out error message when input field gains focus
    $('.required').focus(function(){
        var $parent = $(this).parent();
        $parent.removeClass('error');
        $('span.error',$parent).fadeOut();
    });

});

public class DepartmentServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Getting the Value from the viewer
        String sdepartmentId=request.getParameter("departmentId");
        String departmentName=request.getParameter("departmentName");
        String departmentHead=request.getParameter("departmentHead");
        String dateTime=request.getParameter("appointedDate");




        //Converting to the String department id to integer
        Integer departmentId=Integer.parseInt(sdepartmentId);

        Department dp=new Department();
        dp.setAppDate(dateTime);
        dp.setDepartHead(departmentHead);
        dp.setDepartId(departmentId);
        dp.setDepartName(departmentName);
        try {
            new Department().insertDepartment(dp);
        } catch (SQLException ex) {
            Logger.getLogger(DepartmentServlet.class.getName()).log(Level.SEVERE, null, ex);
        }

    }



}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:39:02+00:00Added an answer on June 9, 2026 at 2:39 pm

    Well its a good practice to have one single servlet which behaves as a controller.
    To make the “DepartmentServlet” as a single point servlet sufficing all the actions, below are few changes to be made in the above code

    1. Add a hidden variable named action in the jsp / html page, inside the form tag, which contains the action to be performed when the corresponding button is pressed.

      <input type="hidden" name="action" id="action" value=""/>

      write a jquery function which sets the value of action to the button clicked, for example when the add button is clicked call below snippet

      $("#action").val('add')

    2. In the DepartmentServlet, retreive the action request parameter value

       String action = request.getParameter("action");    
    
       if(action.equals("add")){
    
       }else if(action.equals("update")){
    
       }
       ....
       ....
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hey, hi i want put limit on object creation means a class can have
I want to put an interface down and up every 1 second for 80
I want to put two side by side on another (I have three )
I want to put all my resources into a ClientBundle for easy management. So
I have a UIViewController in storyboard that includes a scrollView. I want to put
I have a tab bar app that I want to add a banner ad
I want put a button on android edittextpreference. I create a custom editextpreference: public
I've projected an Intranet Ajax application and I want put it in a full
I want to put some images in an area at the application start. As
I want to put the name of the loggedin user as the title of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.