我正在尝试编写一个将使用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 ){
}
}
的AddItem
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);
}
}
答案 0 :(得分:0)
修改:将演员从getAttribute()
添加到List
您可以使用在servlet上下文属性中存储ArrayList
来在所有servlet之间共享它。但是如果它比单个String更复杂,你应该考虑使用类来存储数据。
您可以尝试在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>());
}
}
然后在get或post方法中,您只需访问共享列表:
List<String> list = (List<String>) getServletContext().getAttribute("data_list");
根据Vighanesh Gursale的建议,如果您的数据具有自然键,您也可以使用HashMap。
Edit2:
为了确定发生了什么,您应该使用调试器来查看什么是null或添加如下所示的日志:创建属性后每个init
方法中有一个
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");
}
然后在doGet中:
ServletContext context = getServletContext();
log ((context == null) ? "Context null!?" : "Context ok");
Object attribute = context.getAttribute("data_list");
log((attribute == null) ? "Attribute null", attribute.toString());