gpt4 book ai didi

java - 如何使两个 Servlet 共享相同的项目列表

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:32:00 25 4
gpt4 key购买 nike

这是一个简单的 CRUD 网络应用程序。

我有两个 Servlet。

一个用于列出(MyProjectListServlet.java) 所有 页面上的项目。

另一个处理项目信息的更新(MyPprojectEditorServlet.java)或添加一个项目到列表。

目前,两个 Servlet 都创建并维护它们自己的相同项目列表。因此,列表 Servlet 始终显示其自己的未更改项目列表,更新 Servlet 显示更新的项目信息(如果您编辑项目,否则它将显示与列表 servlet 相同的信息)。

如何让两个 Servlet(MyProjectListServletMyPprojectEditorServlet)共享同一个 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/");
}

}

最佳答案

你有两个选择:

  1. 将列表保存为 HttpSession 对象( session 范围)的属性。
  2. 将列表保存为 ServletContext 对象的一个​​属性(应用范围)。

关于java - 如何使两个 Servlet 共享相同的项目列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12750555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com