我有一个采用HttpServletRequest的Servlet类,我需要将请求传递给另一个方法。我如何以另一种方法获取请求。

Servlet:

     public void doPost(final HttpServletRequest request,
                               final HttpServletResponse response)
      XmlParser xmlParser = new XmlParser();
      xmlParser.getRequest(request);


XmlParser类:

public class XmlParser{
    public void getRequest () //How can i get the request parameter from the servlet.
    {
    }
}

最佳答案

将其传递给构造函数:

public class XmlParser{
    final private HttpServletRequest request;
    public XmlParser(HttpServletRequest request)
    {
        this.request = request;
    }
    // use it in othe methods...
}

10-08 12:25