Java Servlet和JSP最初无法从会话中查看arraylist

时间:2018-09-20 11:38:57

标签: html jsp servlets

我是JSP和servlet的完整入门者,如果您能在这个小问题上帮助我,我将不胜感激。我有一个index.html文件,该文件具有两个JSP文件的链接:addItem.jsp(用户可以将项目输入到他们的todDo列表中,然后将按钮链接提交到servlet),以及toDoList.jsp(显示toDO列表并通过按钮重定向)添加addItem.jsp)

我遇到的问题是,在将项目添加到列表之前查看toDoList.jsp时,列表没有出现。但是,我在addItem.jsp上添加了一个项目,该列表显示在servlet中,现在我可以查看完整列表,包括添加到toDoList.jsp上的项目。

如何更改代码,这样我就不必在列表中添加任何内容即可查看它?

toDoList.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="javax.servlet.http.HttpSession" %>
<%@page import="java.util.ArrayList" %>
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h1>My ToDo List:</h1>

    <% ArrayList<String> toDoList = new ArrayList<String>();%>

    <% if(session.getAttribute("toDoList") != null) {  // If the list exists in the session %>  
    <%   toDoList = (ArrayList<String>) session.getAttribute("toDoList");%> 
    <%   for(String toDoItem:toDoList) { %>
    <%      out.println(toDoItem); %><br>
    <%   } %>
    <% } else {%>

    <h3>Your list is empty!</h3>
    <% } %>
    <form action="addItem.jsp" method="post">
        <fieldset>
            <input type="submit" value="Add Item">
        </fieldset>
    </form>
</body>

addItem.jsp:

<!DOCTYPE html>
<html>
<head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <form action="Servlet1" method="post">
        <fieldset>
            <label>New ToDo Item:</label>
            <input type="text" name="item"><br>
            <input type="submit" value="Submit">
        </fieldset>
    </form>         
</body>

Servlet1的主要代码:

@WebServlet(urlPatterns = {"/Servlet1"})
public class Servlet1 extends HttpServlet {

ArrayList<String> toDoList = new ArrayList<String>();

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    String item = request.getParameter("item");
    toDoList.add(item);

    HttpSession session = request.getSession();

    boolean is_toDoList_Initilized = false;

    if(session.getAttribute("toDoList") == null) {  
        is_toDoList_Initilized = false;  
        session.setAttribute("toDoList", toDoList);
    } else is_toDoList_Initilized = true;


    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet Servlet1</title>");            
        out.println("</head>");
        out.println("<body>");
        for(String toDoItem:toDoList) { 
            out.println(toDoItem + "<br>");  
        }  
        out.println(is_toDoList_Initilized);
        out.println("</body>");
        out.println("</html>");
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您只需在从索引页面重定向toDoList.jsp之前调用该servlet。

流量会像

流程1:index.jsp> servlet1> toDoList

流程2:index.jsp> addItem.jsp> servlet1> toDoList