有人可以告诉我这段代码有什么问题吗?

时间:2018-08-31 11:03:14

标签: jsp servlets

这不会产生任何错误,但是我的数据不会发送到数据库。 JSP和servlet页面都可以正常工作,但是我找不到连接问题。我已经使用相同的方法创建了以前的项目,并且它们运行良好。

catering.jsp

<div class="cat_book_wrapper" data-stellar-background-ratio="0.3">
    <div class="container">
        <div class="row">
           <div class="col-lg-6 col-md-8 col-lg-offset-3 col-md-offset-2 col-sm-offset-0 col-xs-offset-0 padding-right padding-left">
                <div class="cat_book_form wow zoom">    
                    <form method="POST" action="Catering">
                        <table>
                            <tr>
                                <td>Date</td>
                                <td><input type="date" class="form-control datepicker" placeholder="dd/mm/yyyy" id="cus_date" /></td>
                            </tr>
                            <tr>
                                <td>No.Of People</td>
                                <td>
                                    <select class="form-control selectpicker" id="cus_ppl">
                                        <option value="1">Select from list</option>
                                        <option value="2">30</option>
                                        <option value="3">40</option>
                                        <option value="4">50</option>
                                        <option value="5">100</option>
                                        <option value="6">150</option>
                                        <option value="7">200</option>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td>Venue</td>
                                <td><input type="text" class="form-control" placeholder="No.45, Kadawatha, Wattala" id="venue" /></td>
                            </tr>
                            <tr>
                                <td>Contact Person</td>
                                <td><input type="text" class="form-control" placeholder="A.M De Alwis" id="cus_name" /></td>
                            </tr>
                            <tr>
                                <td>Phone Number</td>
                                <td><input type="text" class="form-control" placeholder="0777456345" id="cus_num" /></td>
                            </tr>
                            <tr>
                                <td>Email Id</td>
                                <td><input type="email" class="form-control" placeholder="Info@yourdomain.com" id="cus_email" /></td>
                            </tr>
                            <tr>
                                <td><input type="submit" value="book now" class="btn search_toggle" /></td>
                            </tr>
                        </table>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

Catering.java(Servlet)

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

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 Catering
 */
@WebServlet("/Catering")
public class Catering extends HttpServlet {
    private static final long serialVersionUID = 1L;

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

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String cus_date = request.getParameter("cus_date");
            String cus_ppl = request.getParameter("cus_ppl");
            String venue = request.getParameter("venue");
            String cus_name = request.getParameter("cus_name");
            String cus_num = request.getParameter("cus_num");
            String cus_email = request.getParameter("cus_email");

            String sql = "insert into cater(cus_date,cus_ppl,venue,cus_name,cus_num,cus_email) values(?,?,?,?,?,?)";
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sujanee", "root", "");
            java.sql.PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, cus_date);
            ps.setString(2, cus_ppl);
            ps.setString(3, venue);
            ps.setString(4, cus_name);
            ps.setString(5, cus_num);
            ps.setString(6, cus_email);

            ps.executeUpdate();
            response.sendRedirect("cateringDisplay.jsp");
            return;

        } catch (ClassNotFoundException e) {

            e.printStackTrace();

        } catch (SQLException e) {

            e.printStackTrace();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

首先,在doPost方法上设置一个断点,并检查是否可以获取请求参数。

似乎您在表单中缺少属性name。这代表HTTP请求参数名称。

尝试一下:

 <div class="cat_book_wrapper" data-stellar-background-ratio="0.3">
        <div class="container">
            <div class="row">
                <div
                    class="col-lg-6 col-md-8 col-lg-offset-3 col-md-offset-2 col-sm-offset-0 col-xs-offset-0 padding-right padding-left">
                    <div class="cat_book_form wow zoom">

                        <form method="POST" action="Catering">
                            <table>

                                <tr>
                                    <td>Date</td>
                                    <td><input type="date" class="form-control datepicker"
                                        placeholder="dd/mm/yyyy" id="cus_date" name="cus_date" /></td>
                                </tr>
                                <tr>
                                    <td>No.Of People</td>
                                    <td><select class="form-control selectpicker" id="cus_ppl" name="cus_ppl">
                                            <option value="1">Select from list</option>
                                            <option value="2">30</option>
                                            <option value="3">40</option>
                                            <option value="4">50</option>
                                            <option value="5">100</option>
                                            <option value="6">150</option>
                                            <option value="7">200</option>
                                    </select></td>
                                </tr>
                                <tr>
                                    <td>Venue</td>
                                    <td><input type="text" class="form-control"
                                        placeholder="No.45, Kadawatha, Wattala" id="venue"  name="venue" /></td>
                                </tr>
                                <tr>
                                    <td>Contact Person</td>
                                    <td><input type="text" class="form-control"
                                        placeholder="A.M De Alwis" id="cus_name"  name="cus_name" /></td>
                                </tr>
                                <tr>
                                    <td>Phone Number</td>
                                    <td><input type="text" class="form-control"
                                        placeholder="0777456345" id="cus_num" name="cus_num" /></td>
                                </tr>
                                <tr>
                                    <td>Email Id</td>
                                    <td><input type="email" class="form-control"
                                        placeholder="Info@yourdomain.com" id="cus_email" name="cus_email" /></td>
                                </tr>
                                <tr>
                                    <td><input type="submit" value="book now"
                                        class="btn search_toggle" /></td>
                                </tr>
                            </table>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>