gpt4 book ai didi

java - 在 servlet 中恢复控制后如何恢复该值?

转载 作者:行者123 更新时间:2023-12-01 11:21:18 24 4
gpt4 key购买 nike

我试图在主 servlet 中包含几个 servlet 以完成一些进程并检索值。在此示例中,我从 jsp 文件接收对主 servlet 的控制。此 servlet 发送调用到下一个 servlet 以执行与 Java 列表相关的操作,然后将控制权返回到主 servlet。但是,我无法恢复该列表的值(value)。如何从主 servlet 调用的 servlet 中恢复值?源代码部分如下:

(主 Servlet)删除策略.java:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter printWriter = response.getWriter();

Client client= Client.create();
WebResource webResource= client.resource("http://localhost:8080/clientLibrary/webapi/policy");

//create an object of RequestDispatcher
RequestDispatcher rd = request.getRequestDispatcher("GetPolicy");

// send the client data available with req of delete to req of getPolicy with include()
rd.include(request, response);

// To receive the parameter from the second servlet
List<Policy> policies = (List<Policy>) request.getAttribute("policies");

printWriter.print("List of books in Delete: ");

for(Policy policy : policies) {
printWriter.println("<li>"+"ID: "+policy.getId()+"<br>"+"Max Number of Books: "+policy.getMax_books()+"<br>"+"Year of Book: "+policy.getYear_book()+"<br>"+"Activated: "+policy.getActivate()+"<br></li><br>");
}

printWriter.print("I am comming back in Delete to send a request to Delete method");

/*ClientResponse rs=webResource.accept(
MediaType.APPLICATION_JSON_TYPE,
MediaType.APPLICATION_XML_TYPE).
delete(ClientResponse.class,input);

printWriter.print("Delete a policy");*/
}

/* 包含 Jozef Chocholacek 提供的解决方案: request.setAttribute("policies",policies);GetPolicy.java(第二个Servlet):

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter printWriter = response.getWriter();

Client client= Client.create();
WebResource webResource= client.resource("http://localhost:8080/clientLibrary/webapi/policy");

printWriter.println("<u>Searching for current policies...</u><br>");

ClientResponse rs=webResource.accept(
MediaType.APPLICATION_JSON_TYPE,
MediaType.APPLICATION_XML_TYPE).
get(ClientResponse.class);

//ClientResponse rs = webResource.type(MediaType.APPLICATION_JSON).delete(ClientResponse.class,input);

/*Transform json to java object*/
String jsonPolicy=rs.getEntity(String.class);
Gson gson = new Gson();
Policy[] PolicyA = gson.fromJson(jsonPolicy, Policy[].class);
List<Policy> policies = Arrays.asList(PolicyA);

for(Policy policy : policies) {
System.out.println(policy.getId()+" "+policy.getMax_books()+", "+policy.getYear_book()+", "+policy.getActivate()+", ");
}

//Send List to the servlet that is calling
request.setAttribute("policies", policies);

/*Display book list in the servlet*/
printWriter.println("<h1>List of Policies</h1>");

if (policies.isEmpty()){
printWriter.println("<html><body>Sorry, we did not have any policy"+"<br>");
}else{
printWriter.println("<html><body>The complete list of policies: <br>");
printWriter.println("<ul>");
for(Policy policy : policies) {
printWriter.println("<li>"+"ID: "+policy.getId()+"<br>"+"Max Number of Books: "+policy.getMax_books()+"<br>"+"Year of Book: "+policy.getYear_book()+"<br>"+"Activated: "+policy.getActivate()+"<br></li><br>");
}
}
printWriter.println("</body></html>");
}

提前谢谢

干杯

最佳答案

那么,在您的第一个 servlet (DeletePolicy.java) 中您使用

List<Policy> policies = (List<Policy>) request.getAttribute("policies");

但是第二个 servlet (GetPolicies.java) 不会将此列表存储到请求中。您必须添加

request.setAttribute("policies", policies);

进入您的第二个 servlet。

关于java - 在 servlet 中恢复控制后如何恢复该值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31197155/

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