这是一个简单的 CRUD 网络应用程序。
我有两个 Servlet。
一个用于列出(MyProjectListServlet.java
)页面上的所有项目。
另一个处理项目信息的更新(MyPprojectEditorServlet.java
)或将项目添加到列表中。
目前,两个 Servlet 都创建和维护自己的相同项目列表。因此,列表 Servlet 始终显示它自己的未更改项目列表,更新 Servlet 显示更新的项目信息(如果您编辑项目,否则它将显示与列表 servlet 相同的信息)。
如何使两个 Servlet(MyProjectListServlet
和MyPprojectEditorServlet
) 共享同一个itemRepo
列表。
MyProjectListServlet.java:
package com.myproject;
import java.io.IOException;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.myproject.MyProjectRepository;
@WebServlet("/project/")
public class MyProjectListServlet extends HttpServlet {
@Inject
private MyProjectRepositoryImpl itemsRepo = new MyProjectRepositoryImpl(); // creates brand new list of items
public MyProjectListServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setAttribute("items", itemsRepo.listItems());
getServletContext().getRequestDispatcher("/WEB-INF/pages/item-list.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
MyPprojectEditorServlet.java:
package com.myproject;
import java.io.IOException;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.myproject.myitem;
import com.myproject.myprojectRepository;
@SuppressWarnings("serial")
@WebServlet("/project")
public class MyPprojectEditorServlet extends HttpServlet {
@Inject
private MyProjectRepositoryImpl itemRepo = new MyProjectRepositoryImpl(); //creates its own list of items
public MyProjectEditorServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String id = request.getParameter("id");
if(id != null && !id.isEmpty()){
MyItem item = itemRepo.lookupitemById(id);
request.setAttribute("item", item);
}
request.setAttribute("items", itemRepo.listMyItems());
getServletContext().getRequestDispatcher("/WEB-INF/pages/item-form.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String str1 = request.getParameter("str1");
String str2 = request.getParameter("str2");
String str3 = request.getParameter("str3");
String str4 = request.getParameter("str4");
String str5 = request.getParameter("str5");
String id = request.getParameter("id");
if(id == null || id.isEmpty()){
itemRepo.addItem(str1, str2, str3, str4, str5);
}else{
itemRepo.updateItem(id, str1, str2, str3, str4, str5);
}
response.sendRedirect(request.getContextPath() + "/project/");
}
}