是否可以实现以下方式以将数据存储在 DTO 中并从应用程序中的任何位置访问它而不是使用上下文?
请提供建议!!!
public class DummyDTO {
private String name = null;
private String age = null;
// getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
public class MyServletClass extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DummyDTO dummyDTO = new DummyDTO();
dummyDTO.setName(request.getParameter("name"));
dummyDTO.setAge(request.getParameter("age"));
AnotherClass.setValues(dummyDTO);
}
public class AnotherClass {
String name = "";
String age = "";
public static void setValues(DummyDTO dummyDTO) {
name = dummyDTO.getName();
age = dummyDTO.getAge();
}
}
}