简体   繁体   English

如何从HttpServlet的doPost()方法返回一些数据?

[英]How to return some data from HttpServlet's doPost() method?

I am writing a java class that extends HttpServlet thus overwrites doPost() method. 我正在编写一个扩展HttpServlet的Java类,从而覆盖了doPost()方法。 Inside the doPost() method, some data object is assembled, but I am not sure what is a better way to return the data. 在doPost()方法内部,已组装了一些数据对象,但是我不确定什么是返回数据的更好方法。 For instance, 例如,

public class FormSubmission extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // http request is parsed to assemble an object, e.g., a List
        List L = parseRequest(request);

        // Question: how to return List L for use by Server?         
    }
}    

One way I can think of is to declare a static variable as 我能想到的一种方法是将静态变量声明为

Public static List L;

and call FormSubmission.L , which basically makes List L a global variable. 并调用FormSubmission.L ,这基本上使List L成为全局变量。

Another way is to write data bytes to a file so that it can be read back into a List later. 另一种方法是将数据字节写入文件,以便以后可以将其读回到列表中。

But is there better way to handle such situation? 但是有更好的方法来处理这种情况吗?

Use cases based on Comment: 基于评论的用例:

Two forms with HTTP/POST are created in main page "index.html", Form A and B. It is required that a user submits Form A first, and then submits Form B. Submitting Form A triggers the above doPost() method, such that List L is constructed. 在主页“ index.html”中创建了两个带有HTTP / POST的表单,即表单A和B。要求用户先提交表单A,然后再提交表单B。提交表单A会触发上述doPost()方法,这样就构造了列表L。 The scenario is to how to make this List available to process Form B submission. 方案是如何使此列表可用于处理表单B提交。 Specifically, how to have Form B's doPost() method be able to access List L created from A during the previous stage. 具体来说,如何使窗体B的doPost()方法能够访问在上一阶段从A创建的列表L。

I would consider using Session scope and put there. 我会考虑使用Session作用域并将其放置在那里。 You can reach object from that session. 您可以从该会话中获取对象。

final HttpSession session = req.getSession();

In your FormASubmission servlet, save the List to the user's session object. 在您的FormASubmission Servlet中,将List保存到用户的session对象。

List L = parseRequest(request);

HttpSession session = request.getSession();
session.setAttribute("FormADataList", L);

Then in your FormBSumission servlet, you can retrieve this List as 然后,在您的FormBSumission Servlet中,可以将该List检索为

HttpSession session = request.getSession();
List L = (List) session.getAttribute("FormADataList");

HttpSession is an interface whose underlying implementation is provided by the container. HttpSession是一个接口,其基础实现由容器提供。 This scoped object is created only once per user/browser interacting with the application. 每个用户/浏览器与应用程序进行交互时,只会创建一次该作用域对象。 Any user data stored here remains available for the length of the session across multiple HTTP requests from the same user. 在同一位用户发出的多个HTTP请求中,此处存储的所有用户数据在会话期间仍然可用。

The HttpSession 's javadoc says it HttpSession的javadoc说出来

Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user. 提供一种在多个页面请求或访问网站中识别用户的方法,并存储有关该用户的信息。

you can do a RequestDispatcher 你可以做一个RequestDispatcher

RequestDispatcher rd = request.getRequestDispatcher("name.jsp");
    rd.forward(request, response);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM