Java 通过servlet将数据存储到ArrayList中

Java 通过servlet将数据存储到ArrayList中,java,servlets,arraylist,Java,Servlets,Arraylist,我正在尝试编写一个应用程序,它将使用3个不同的servlet。 一个将显示信息,另一个将允许用户输入数据并显示它,最后一个servlet将允许用户编辑现有数据。 我很难找到将数据从一个servlet存储到ArrayList并显示在servlet上显示信息的表中的方法 以下是我到目前为止的情况: import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import javax.serv

我正在尝试编写一个应用程序,它将使用3个不同的servlet。 一个将显示信息,另一个将允许用户输入数据并显示它,最后一个servlet将允许用户编辑现有数据。 我很难找到将数据从一个servlet存储到ArrayList并显示在servlet上显示信息的表中的方法

以下是我到目前为止的情况:

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/DisplayItems")
public class DisplayItems extends HttpServlet {
private static final long serialVersionUID = 1L;
public static ArrayList list = new ArrayList();

public DisplayItems() {
    super();
    // TODO Auto-generated constructor stub
}

public void init(ServletConfig config) throws ServletException {
    ServletContext context = config.getServletContext();
    if (context.getAttribute("data_list") == null) {
        context.setAttribute("data_list", new ArrayList<String>());
    }

}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<String> list = (List<String>) getServletContext().getAttribute("data_list");
        list.add("1");
    PrintWriter out = response.getWriter();
    response.setContentType("text/html");
    out.println("<html>");
    out.println("<head><title>Department Library</title>");
    //out.println("<style>h1,h2,form,p{text-align:center;color:white}body{background-color:black;}</style></head>");
    out.println("<body>");
    out.println("<h1>Welcome to the Department Library</h1>");
    out.println("<table border='1' cellpadding='2' cellspacing='2'>");
    out.println("<thead><tr><th>ID</th><th>Type</th><th>Name</th><th>Addition Info</th><th>Available</th><th>Operation</th></tr><thead>");
    out.println("<tr><td>"+list.get(1)+"</td><td>"+list.get(0)+"</td><td>"+list.get(0)+"</td><td>"+list.get(0)+"</td><td>"+list.get(0)+"</td><td><a href='./EditItem'>Edit</a></td></tr>");
    out.println("<p><a href='./AddItem'>Add Item</a></p>");
    out.println("<p><a href='./EditItem'>Edit Item</a></p>");
    //out.println("<form method='post'><input id='inputtext' name='inputtext'></inputText><input type='submit' value='Translate' name='submit'></input></form>");
    out.println("</body></html>");
    out.close();

}

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

}

public  void row(String id, String type, String name, String info ){

}
}
附加项

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Array;

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;

@WebServlet("/AddItem")
public class AddItem extends HttpServlet {
private static final long serialVersionUID = 1L;

public AddItem() {
    super();
    // TODO Auto-generated constructor stub
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    response.setContentType("text/html");
    out.println("<html>");
    out.println("<head><title>Add Items</title>");
    out.println("<body>");
    out.println("<h1>Add Items</h1>");
    out.println("<form method='post'");
    //name=type
    //name=name
    //name=info
    //name=copies
    out.println("<table><tbody><tr><td>Type:</td><td><select name='type'><option>Book</option><option>Tablet</option> </select></td><tr>");
    out.println("<tr><td>Name:</td><td><input name='name' size='60'/></td></tr>");
    out.println("<tr><td>Additional Info:</td><td><input name='info' size='60' /></td></tr>");
    out.println("<tr><td># of Copies:</td><td><input name='copies' size='8' /></td></tr>");
    out.println("<tr><td colspan='2' rowspan='1'><input name='add' type='submit' value='Add' /></td></tr></tbody></table>");
    out.println("</form>");
    out.println("<p><a href='./DisplayItems'>Display Items</a></p>");
    //out.println("<p><a href='./EditItem'>Edit Item</a></p>");
    //out.println("<form method='post'><input id='inputtext' name='inputtext'></inputText><input type='submit' value='Translate' name='submit'></input></form>");
    out.println("</body></html>");
    out.close();    }

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

    PrintWriter out = response.getWriter();
    request.getParameter("type");
    String name = request.getParameter("name");
    String info = request.getParameter("info");
    String copies = request.getParameter("copies");
    //type.add(value);

    out.println(name);
    out.println(info);
    out.println(copies);
}

}
编辑:将“从getAttribute转换”添加到列表

您可以使用将ArrayList存储在servlet上下文属性中,以便在所有servlet之间共享它。但如果类比单个字符串更复杂,则应考虑使用类来存储数据

您可以尝试在servlet的init方法中添加类似的内容:

public void init(ServletConfig config) throws ServletException {

    ServletContext context = config.getServletContext();
    if (context.getAttribute("data_list") == null) {
        context.setAttribute("data_list", new ArrayList<String>());
    }

}
然后在多吉特:

ServletContext context = getServletContext();
log ((context == null) ? "Context null!?" : "Context ok");
Object attribute = context.getAttribute("data_list");
log((attribute == null) ? "Attribute null", attribute.toString());

为什么不使用hashmap呢?从中获取价值要容易得多。还要尝试创建一个包含所有所需参数的类,并创建该类的对象,并通过会话或请求参数传递它。当您从一个servlet获取数据时,您应该立即将其持久化,否则您将无法使用丢失数据的模式。谢谢。当我尝试访问共享列表时,它使我不得不向其中添加cast。然后我试着运行它,但它给了我错误。我正在尝试从表单添加到数组列表。顺便说一句,我可能会做散列映射,因为我没有想到这点。@user3162117我最初忘记了投稿,并相应地编辑了这篇文章。但您的新错误是什么?这是我的列表List=List getServletContext.getAttributedata_List;表1.add1;我得到了错误:javax.servlet.GenericServlet.getServletContextGenericServlet.java:123 hw1.DisplayItems.doGetDisplayItems.java:35I确实像您一样初始化了。以下是错误java.lang.NullPointerException javax.servlet.GenericServlet.getServletContextGenericServlet.java:123 hw1.DisplayItems.doGetDisplayItems.java:34 javax.servlet.http.HttpServlet.serviceHttpServlet.java:620 javax.servlet.http.HttpServlet.serviceHttpServlet.java:727org.apache.tomcat.websocket.server.WsFilter.doFilterWsFilter.java:52@user3162117:您应该调试或记录以查看实际发生的情况。见我编辑的帖子。
    if (context.getAttribute("data_list") == null) {
        context.setAttribute("data_list", new ArrayList<String>());
        log("Creating attribute data_list");
    }
    else {
        log("Attribute data_list already created");
    }
ServletContext context = getServletContext();
log ((context == null) ? "Context null!?" : "Context ok");
Object attribute = context.getAttribute("data_list");
log((attribute == null) ? "Attribute null", attribute.toString());