我是Java servlet的新手,因此决定创建在线教练中心费用估算器...在此学生中,必须选择他们的董事会,班级,学科并提交$start-color
表格。在Java Servlet $end-color
中请求表单数据。费用然后通过index.html
命令打印出来。但是,只有在所有复选框都选中的情况下才会显示费用,而不是在4个中选择2个或3个时....为什么?
请帮助我... 可以问我任何答复... 会在24小时内回答... 预先感谢
下面是我的html代码。(到目前为止,我仅为ICSE板开发了代码)
Fees_Calculator .java
java servlet(Fees_Calculator):
servlet-out.println()
答案 0 :(得分:1)
如果未选中值为English
的复选框,则此行request.getParameter("English")
返回null
,其中这些行之一if(sub3.equals("on"))
抛出NullPointerException
,因此{{1 }}行未到达。如果未选中任何一个复选框,则会发生这种情况,if语句将抛出out.println
,该语句使用与第一个未选中的复选框相对应的变量。
我将通过切换像NullPointerException
这样的边(其中X是1、2、3、4)来更改所有if语句。
不会记录异常,因为"on".equals(subX)
没有try
,如果catch
具有{{1 }}子句记录异常
答案 1 :(得分: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>
}