使用servlet和jsp

时间:2018-04-20 16:35:07

标签: java jsp servlets

我被困在我的项目中。我有一个学生名单,我可以在那里看到每个学生的分数,但是当我试图删除学生的分数时,它不起作用。

DAO实施:

public class ResultDaoImplementation implements ResultDAO {
    Connection connection = null;
    public ResultDaoImplementation() {
        connection= connectionWeb.getConnection();
    }

    @Override
             public List<Result> findbyId(long student_id){
            List<Result> results = new ArrayList <> ();
        Result result = null;
        ResultSet resultSet;
        PreparedStatement preparedStatement;
       try { preparedStatement= connection.prepareStatement ("SELECT * FROM  results where student_id=?");
        preparedStatement.setLong(1,student_id);
        resultSet = preparedStatement.executeQuery();
         while (resultSet.next()){
                result = new Result ();
       result.setStudent_id(resultSet.getLong("student_id"));
       result.setCourse_id(resultSet.getString("course_id"));
       result.setSemester(resultSet.getInt("semester"));
       result.setMarks(resultSet.getInt("marks"));
       result.setMark_id(resultSet.getString("mark_id"));
       results.add(result);}
       resultSet.close();
       preparedStatement.close();}
       catch (SQLException ex){
           ex.printStackTrace();
       }
       return results;   
}

     // Delete method
    public void delete(Result result){
        PreparedStatement preparedStatement;
        String sql="DELETE FROM results WHERE MARK_ID=? AND STUDENT_ID=?";
        try{
            preparedStatement= connection.prepareStatement(sql);
            preparedStatement.setString(1, result.getMark_id());
            preparedStatement.setLong(2, result.getStudent_id());
            preparedStatement.executeUpdate();
            preparedStatement.close();
        }
        catch (SQLException ex){
            ex.printStackTrace();

        }}

控制器:

 protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String forward = "";

        String action = request.getParameter("action");
        try {

            if (action.equalsIgnoreCase("delete")) {
                String Rtid = request.getParameter("sid");
            long Rstdid = Long.parseLong(Rtid);
                String rdel = request.getParameter("rmarkid");
                Result results = new Result();
                results.setMark_id(rdel);
                results.setStudent_id(Rstdid);
                dao2.delete(results);
                forward = LIST;
                // forward new info in the list 
                request.setAttribute("listResult1", dao2.findbyId(Rstdid));

                request.getRequestDispatcher("/listResult.jsp").forward(request, response);
            }

JSP:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
     <title> List of Result </title>

        <table border="1">
            <thead>
                <tr>
                    <th>Student ID</th>
                    <th>Course ID</th>
                   <!-- <th>Course Name</th>-->
                    <th>Semester</th>
                    <th> Marks </th>
                    <th> Mark ID </th>
                   <th colspan=2> Action </th>
                </tr>
            </thead>
            <tbody>
            <c:forEach items="${listResult1}" var="results">
                <tr>
                <td><c:out value="${results.student_id}"/></td>
                <td><c:out value="${results.course_id}"/></td>
                <td><c:out value="${results.semester}"/></td>
                <td><c:out value="${results.marks}"/></td>
                 <td><c:out value="${results.mark_id}"/></td>
                <td> <a href="resultList?action=edit&sid&rmarkid=<c:out value="${results.student_id}"/>">Edit</a>
                <td> <a href="resultList?action=delete&rmarkid&sid=<c:out value="${results.mark_id}"/>">Delete</a>
                </tr>

            </c:forEach>
        </tbody>
        </table>
     <p> <a href="result.html"> Add new record </a> </p>
    </body>
</html>

我认为问题在于我使用2个参数删除并且只使用1个参数在列表中转发,或者我如何编辑动作&#34;删除&#34;在我的JSP中

1 个答案:

答案 0 :(得分:0)

您正在传递rmarkid为空并为sid传递错误的值!与您密切关注。

这就是你要传递的内容:

<td> <a href="resultList?action=delete&rmarkid&sid=<c:out value="${results.mark_id}"/>">Delete</a>

这是你应该通过的:

<td> <a href="resultList?action=delete&rmarkid=<c:out value="${results.mark_id}"&sid=<c:out value="${results.student_id}"/>">Delete</a>