Сервлет: отображается только один элемент вместо всех элементов из arrayList, и данные не удаляются после каждого сеанса - PullRequest
0 голосов
/ 01 октября 2018

Мое веб-приложение должно взять информацию о форме (называемую событием) из jsp и добавить ее в массив, содержащий несколько форм (называемый eventList).Тогда это для отображения этих форм на другой JSP.Тем не менее, мой сервлет отображает только одну форму вместо всех введенных, и даже после завершения сеанса та же самая форма будет отображаться с последнего сеанса.

AddEventServlet:

public class AddEventServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {
        String url = "/index.jsp";

        HttpSession session = request.getSession();
        EventList eventList = new EventList();
        session.setAttribute("eventList", eventList);

        // get current action
        String action = request.getParameter("action");
        System.out.println(action);

        if (action == null) {
            action = "home";  // default action
        }

        if (action.equals("add")) {
            // get parameters from the request
            String eventTitle = request.getParameter("Event Title");
            String description = request.getParameter("Description");
            String fieldNumber = request.getParameter("Field Number");
            String date = request.getParameter("Date");
            String startTime = request.getParameter("Start Time");
            String stAMPM = request.getParameter("stAMPM");
            String endTime = request.getParameter("End Time");
            String etAMPM = request.getParameter("etAMPM");

            System.out.println("hello world");
            System.out.println(eventTitle);
            System.out.println(description);
            System.out.println(fieldNumber);
            System.out.println(date);
            System.out.println(startTime);
            System.out.println(stAMPM);
            System.out.println(endTime);
            System.out.println(etAMPM);

            // use regular Java class
            Event event = new Event(eventTitle, description, fieldNumber, date, startTime, stAMPM, endTime, etAMPM);
            eventList.addEvent(event);

            System.out.println(event.toString());

            // store the Event object in request and set URL
            request.setAttribute("event", event);
            url = "/enterEvent.jsp";

            request.setAttribute("successfulEntry", "Successfully entered event!");
            request.getRequestDispatcher(url)
                    .forward(request, response);
        }

        if (action.equals("home")) {
            url = "/index.jsp";     // the "index" page
            request.getRequestDispatcher(url)
                    .forward(request, response);

        } else if (action.equals("display")) {

            session.getAttribute("eventList");
            if (eventList == null) {
                eventList = new EventList();
            }

            session.setAttribute("eventList", eventList);
            url = "/displayEvent.jsp";

            System.out.print(eventList);

            request.getRequestDispatcher(url)
                    .forward(request, response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

}

enterEvent.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE html>
<html>
    <c:import url="/includes/header.html" />
    <c:import url="/includes/navigation.html" />
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <link rel="stylesheet" href="styles/main.css" type="text/css"/>
    </head>
    <body>
        <div class="float-box1">
            <h1>Nice Kicks!</h1>
            <img src="https://res.cloudinary.com/jerrick/image/upload/f_auto,fl_progressive,q_auto,c_fit,w_1100/ekqqtovhznnalxpwr525" alt="Cool Soccer Cleats" height="500" width="500">
        </div>
        <div class="float-box">
            <h2>Enter Information for Field Reservation</h2>
            <form name="addEvent" action="AddEventServlet" method="POST">
                <input type="hidden" name="action" value="add">
                <p2>${successfulEntry}</p2>
                <p2>Event Title:</p2> <input type="text" name="Event Title" value="" size="100"/><br><br><br><br><br><br><br><br><br>
                <p2>Description:</p2> <input type="text" name="Description" value="" size="100"/><br><br>
                <p2>Field Number:</p2> <select name="Field Number">
                    <option>Field 1</option>
                    <option>Field 2</option>
                    <option>Field 3</option>
                    <option>Field 4</option>
                    <option>Field 5</option>
                </select><br><br>
                <p2>Date:</p2> <input type="text" name="Date" value="MM/DD/YYYY" /><br><br>
                <p2>Start Time:</p2> <input type="text" name="Start Time" value="00:00" />
                <select name="stAMPM">
                    <option>AM</option>
                    <option>PM</option>
                </select><br><br>
                <p2>End Time:</p2> <input type="text" name="End Time" value="00:00" />
                <select name="etAMPM">
                    <option>AM</option>
                    <option>PM</option>
                </select><br><br>
                <input type="reset" value="Clear" name="Clear" />
                <input type="submit" value="Submit" name="Submit" />
            </form>
            <div style="clear: both;"></div>

        </div>
        <div style="clear: both;"></div>
    </body>
    <footer>
        <p1><c:import url="/includes/footer.jsp" /></p1>
    </footer>
</html>

displayEvent.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<!DOCTYPE html>
<html>
    <c:import url="/includes/header.html" />
    <c:import url="/includes/navigation.html" />
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
        <link rel="stylesheet" href="styles/main.css" type="text/css"/>
    </head>
    <body>
        <div class="float-box1">
            <h1>Nice Strike!</h1>
            <img src="https://usatftw.files.wordpress.com/2018/04/epa_italy_soccer_serie_a.jpg?w=1000&h=600&crop=1" alt="Bicycle Kick" height="500" width="500">
        </div>
        <div class="float-box">
            <h2>Reserved Field Times</h2>
            <input type="hidden" name="action" value="display">
            <table>
                <th>Event Title</th>
                <th>Event Description</th>
                <th>Field Number</th>
                <th>Event Date</th>
                <th>Start Time</th>
                <th>AM/PM</th>
                <th>End Time</th>
                <th>AM/PM</th>
                    <c:forEach var="event" items="${eventList.events}"> 
                    <tr>
                        <td>${event.eventTitle}</td>
                        <td>${event.description}</td>
                        <td>${event.fieldNumber}</td>
                        <td>${event.date}</td>
                        <td>${event.startTime}</td>
                        <td>${event.stAMPM}</td>
                        <td>${event.endTime}</td>
                        <td>${event.etAMPM}</td> 
                    </tr>
                </c:forEach>
            </table>

        </div>
        <div style="clear: both;"></div>
    </body>
    <footer>
        <p1><c:import url="/includes/footer.jsp" /></p1>
    </footer>
</html>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...