使用html和servlet时出错

问题描述 投票:2回答:2

我是java servlet的新手,并决定在线教练中心费用估算器...在这个学生必须选择他们的董事会,班级,科目和提交表格index.html。表单数据在java servlet Fees_Calculator .java中请求。通过servlet-out.println()命令打印费用。但是,只有在选择了所有复选框时才显示费用,而不是选择4个中的2个或3个....为什么?

请帮帮我...可以回答我的任何问题...... 24小时内回复...提前致谢

下面是我的HTML代码。(到目前为止我已经开发了仅用于ICSE板的代码)

<!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>

}
java html servlets
2个回答
1
投票

如果没有选中值为English的复选框,则此行request.getParameter("English")返回null,其中一行if(sub3.equals("on"))抛出NullPointerException,因此未到达out.println行。如果未选中任何复选框,则会发生这种情况,NullPointerException将由if语句抛出,该语句使用与第一个未选中复选框对应的变量。

我会通过切换像"on".equals(subX)这样的边来改变所有的if语句(其中X是1,2,3,4)。

因为try没有catch,所以没有记录异常,如果try有一个记录异常的catch子句,如果执行被异常停止会更容易发现


0
投票
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>

}
© www.soinside.com 2019 - 2024. All rights reserved.