007_Gamerx
007_Gamerx

Reputation: 43

Getting error working with html and servlets

I am new to java servlet and decided to make online coaching centre fees estimator... In this student have to select their board, class , subjects and submit the form index.html. Form data is requested at java servlet Fees_Calculator .java. The fees is than printed on through servlet-out.println() command. But fees is displayed only when all checkboxes are selected not when 2 or 3 out of 4 are selected ....Why?

Please help me... Can ask me anything in reply ... Will answer within 24 hrs... Thanks in advance

Below is my html code.(Till now I have developed code for only ICSE board )

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <title>Tuition Hub</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link type="text/png" rel="icon" href=""/>
        <style>
#page{
background-image: linear-gradient(to right,#2E74FA 10%,#59C2F9 90%);
}
#main {
border:1px;
border-radius:100px;
margin:50px 100px;
background-color:white;
}

.image{
border-radius:50px 0px 0px 50px;
height:500px;
width:50%;
float:left;
background-color:white;
}
.form{
border-radius:0px 50px 50px 0px;
height:500px;
width:50%;
float:right;
background-color:white;
}
#input{
width:89%;
background-color:#E6E6E6;
padding:15px;
margin:10px;
border: 0px;
border-radius:100px;
}
#select{
width:62%;
background-color:#E6E6E6;
padding:15px;
margin:10px;
border: 0px;
border-radius:100px;
}
#check{
font-size:44px;
color: blue;
}
input:focus {
background-color:aqua;
}


.submit{
color:white;
background-color:limegreen;
padding:10px;
border:0px;
border-radius:100px;
}
.img{
    height:500px;
    width:400px;
}
</style>
    </head>
    <body id="page">
        <div id="main">
<div class="image">
<img src="\images\tuition_hub.jpg" class="img">
</div>
<div class="form">
<form action="Fee_Calculator" method ="get">
<H3><B><center>Get estimated fees</center></B></H3>
<input type="text" name="fname" placeholder="Name" id="input" style="margin-top:0px"><br>
<input type="email" name="email" placeholder="Email" id="input"><br>
Select Your Board : <select name="board" id="select"><br>
<option value="ICSE" />ICSE
<option value="CBSE"/>CBSE
</select><br>
Select Your Class  : <select name="class" id="select"><br>
<option value="2" id="select"/>2
<option value="3" id="select"/>3
<option value="4" id="select"/>4
<option value="5" id="select"/>5
<option value="6" id="select"/>6
<option value="7" id="select"/>7
<option value="8" id="select"/>8
<option value="9" id="select"/>9
</select><br>
Select your Subjects :<br>
<input type="checkbox" name="Maths" id="check" value="on">Maths
<input type="checkbox" name="Science" id="check" value="on">Science<br>
<input type="checkbox" name="English" id="check" value="on">English
<input type="checkbox" name="Social Studies" id="check" value="on">Social Studies<br>
<center><input type="submit" value="CHECK" class="submit"></center>

</form>
</div>
</div>
    </body>
</html>

java servlet (Fees_Calculator):

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

/**
 *
 * @author NEGI PC
 */
@WebServlet(urlPatterns = {"/Fee_Calculator"})
public class Fee_Calculator extends HttpServlet {
String name,email,board,sub1,sub2,sub3,sub4;
            int sclass,fee;
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {

            name = request.getParameter("fname");
            email = request.getParameter("email");
            board = request.getParameter("board");
            sub1 = request.getParameter("Maths");
            sub2 = request.getParameter("Science");
            sub3 = request.getParameter("English");
            sub4= request.getParameter("Social Studies");
            sclass= Integer.parseInt(request.getParameter("class"));
            if(board.equals("ICSE")){
        switch(sclass){
            case 2 :
                if(sub1.equals("on")){
                fee+=200;
                } 
                if(sub2.equals("on")){
                fee+=200;
                }
                if(sub3.equals("on")){
                fee+=200;
                }
                if(sub4.equals("on")){
                fee+=200;
                }
                break;
            case 3 : 
                if(sub1.equals("on")){
                fee+=250;
                }
                if(sub2.equals("on")){
                fee+=250;
                }
                if(sub3.equals("on")){
                fee+=250;
                }
                if(sub4.equals("on")){
                fee+=250;
                }
                break;
             case 4 : 
                if(sub1.equals("on")){
                fee+=250;
                }
                if(sub2.equals("on")){
                fee+=250;
                }
                if(sub3.equals("on")){
                fee+=250;
                }
                if(sub4.equals("on")){
                fee+=250;
                }
                break;
             case 5 : 
                if(sub1.equals("on")){
                fee+=300;
                }
                if(sub2.equals("on")){
                fee+=300;
                }
                if(sub3.equals("on")){
                fee+=300;
                }
                if(sub4.equals("on")){
                fee+=300;
                }
                break;
             case 6 : 
                if(sub1.equals("on")){
                fee+=350;
                }
                if(sub2.equals("on")){
                fee+=350;
                }
                if(sub3.equals("on")){
                fee+=350;
                }
                if(sub4.equals("on")){
                fee+=350;
                }
                break;
             case 7 : 
                if(sub1.equals("on")){
                fee+=400;
                }
                if(sub2.equals("on")){
                fee+=400;
                }
                if(sub3.equals("on")){
                fee+=400;
                }
                if(sub4.equals("on")){
                fee+=400;
                }
                break;
             case 8 : 
                if(sub1.equals("on")){
                fee+=500;
                }
                if(sub2.equals("on")){
                fee+=450;
                }
                if(sub3.equals("on")){
                fee+=450;
                }
                if(sub4.equals("on")){
                fee+=450;
                }
                break;
             case 9 : 
                if(sub1.equals("on")){
                fee+=500;
                }
                if(sub2.equals("on")){
                fee+=450;
                }
                if(sub3.equals("on")){
                fee+=450;
                }
                if(sub4.equals("on")){
                fee+=450;
                }
                break;
             default:
                 out.println("WRONG INPUT !!!");
        }
            }   
        else{
        //For option CBSE , Not created yet!!

        }
        out.println("<h2>Your Estimated fees => Rs."+fee);
        out.println("<br> For more Details ,Consult the Tutor!!!</h2>"); 
        fee=0;   
        } 

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

Upvotes: 2

Views: 168

Answers (2)

corroborator
corroborator

Reputation: 321

Hope it helps:

 index.jsp 
-----------------------------
<!DOCTYPE html>

<html>
    <head>
        <title>Tuition Hub</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link type="text/png" rel="icon" href=""/>
        <style>
#page{
background-image: linear-gradient(to right,#2E74FA 10%,#59C2F9 90%);
}
#main {
border:1px;
border-radius:100px;
margin:50px 100px;
background-color:white;
}

.image{
border-radius:50px 0px 0px 50px;
height:500px;
width:50%;
float:left;
background-color:white;
}
.form{
border-radius:0px 50px 50px 0px;
height:500px;
width:50%;
float:right;
background-color:white;
}
#input{
width:89%;
background-color:#E6E6E6;
padding:15px;
margin:10px;
border: 0px;
border-radius:100px;
}
#select{
width:62%;
background-color:#E6E6E6;
padding:15px;
margin:10px;
border: 0px;
border-radius:100px;
}
#check{
font-size:44px;
color: blue;
}
input:focus {
background-color:aqua;
}


.submit{
color:white;
background-color:limegreen;
padding:10px;
border:0px;
border-radius:100px;
}
.img{
    height:500px;
    width:400px;
}
</style>
    </head>
    <body id="page">
        <div id="main">
<div class="image">
<img src="\images\tuition_hub.jpg" class="img">
</div>
<div class="form">
<form action="Fee_Calculator" method ="POST">
<%
String errorMesg=(String)request.getAttribute("errorMsg");
if(errorMesg!=null){
    out.println("<div class='error'>*Please select all subjects !<div>"); 
 }
%>
<H3><B><center>Get estimated fees</center></B></H3>
<input type="text" name="fname" placeholder="Name" id="input" style="margin-top:0px" ><br>
<input type="email" name="email" placeholder="Email" id="input" ><br>
Select Your Board : <select name="board" id="select"><br>
<option value="ICSE" />ICSE
<option value="CBSE"/>CBSE
</select><br>
Select Your Class  : <select name="class" id="select"><br>
<option value="2" id="select"/>2
<option value="3" id="select"/>3
<option value="4" id="select"/>4
<option value="5" id="select"/>5
<option value="6" id="select"/>6
<option value="7" id="select"/>7
<option value="8" id="select"/>8
<option value="9" id="select"/>9
</select><br>
Select your Subjects :<br>
<input type="checkbox" name="subject" id="sub1" value="Maths">Maths
<input type="checkbox" name="subject" id="sub2" value="Science">Science<br>
<input type="checkbox" name="subject" id="sub3" value="English">English
<input type="checkbox" name="subject" id="sub4" value="SocialStudies">Social Studies<br>
<center><input type="submit" value="CHECK" class="submit"></center>

</form>
</div>
</div>
    </body>
</html>



Servlet
---------------
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author NEGI PC
 */
@WebServlet(urlPatterns = {"/Fee_Calculator"})
public class Fee_Calculator extends HttpServlet {


/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {

        String name, email, board, sub1, sub2, sub3, sub4;
        int sclass, fee = 0;

        name = request.getParameter("fname");
        email = request.getParameter("email");
        board = request.getParameter("board");

        String[] subjects = request.getParameterValues("subject");
        sclass = Integer.parseInt(request.getParameter("class"));

        request.setAttribute("errorMsg", "");

        if (subjects != null && subjects.length > 0) {
            switch (board) {
                case "ICSE":
                    switch (sclass) {
                        case 2:
                            for (String subject : subjects) {
                                if (subject.equals("Maths") || subject.equals("Science")
                                        || subject.equals("English")
                                        || subject.equals("SocialStudies")) {
                                    fee += 200;
                                }

                            }
                            break;

                        case 3:
                        case 4:
                            for (String subject : subjects) {
                                if (subject.equals("Maths") || subject.equals("Science")
                                        || subject.equals("English")
                                        || subject.equals("SocialStudies")) {
                                    fee += 250;
                                }
                            }
                            break;
                        case 5:
                            for (String subject : subjects) {
                                if (subject.equals("Maths") || subject.equals("Science")
                                        || subject.equals("English")
                                        || subject.equals("SocialStudies")) {
                                    fee += 300;
                                }
                            }
                            break;
                        case 6:
                            for (String subject : subjects) {
                                if (subject.equals("Maths") || subject.equals("Science")
                                        || subject.equals("English")
                                        || subject.equals("SocialStudies")) {
                                    fee += 350;
                                }
                            }
                            break;
                        case 7:
                            for (String subject : subjects) {
                                if (subject.equals("Maths") || subject.equals("Science")
                                        || subject.equals("English")
                                        || subject.equals("SocialStudies")) {
                                    fee += 400;
                                }
                            }
                            break;

                        case 8:
                        case 9:
                            for (String subject : subjects) {

                                if (subject.equals("Maths")) {
                                    fee += 500;
                                }
                                if (subject.equals("Science") || subject.equals("English")
                                        || subject.equals("SocialStudies")) {
                                    fee += 450;
                                }
                            }
                            break;

                        default:
                            out.println("WRONG INPUT !!!");
                    }
                case "CBSE":
                    // For option CBSE , Not created yet!!
                    break;

            }

            out.println("<h2>Your Estimated fees => Rs." + fee);
            out.println("<br> For more Details ,Consult the Tutor!!!</h2>");

        } else {
            RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
            request.setAttribute("errorMsg", "Please select any subject");
            rd.forward(request, response);
        }


    }

}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the
// left to edit the code.">
/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

/**
 * Returns a short description of the servlet.
 *
 * @return a String containing servlet description
 */
@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}

Upvotes: 0

user10386912
user10386912

Reputation:

If the checkbox with value English is not selected this line request.getParameter("English") returns null and one of these lines if(sub3.equals("on")) throws NullPointerException hence the out.println lines are not reached. This happens if any of the checkboxes is not selected, the NullPointerException will be throw by the if statement which uses the variable corresponding to the first unchecked checkbox.

I would change all the if statements by switching the sides like this "on".equals(subX) (where X is 1, 2, 3, 4).

The exception is not logged as the try is without catch, it would be easier to spot if the execution is stopped by an exception if the try would have a catch clause which logs the exceptions

Upvotes: 1

Related Questions