简体   繁体   English

如何从HttpServlet调用RemoteServiceServlet中的方法?

[英]How do I call method in RemoteServiceServlet from HttpServlet?

I am using GWT and want to be able to invoke the method incrementProduct(String productName) in my main RemoteServiceServlet by typing this URL in my browser: 我正在使用GWT,并且希望能够通过在浏览器中键入以下URL来在主RemoteServiceServlet中调用方法incrementProduct(String productName)

http://mywebshop.appspot.com/increment?name=pillow

To handle this request I've created the following HttpServlet: 为了处理此请求,我创建了以下HttpServlet:

public class IncrementServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        System.out.println("IncrementServlet called!");
        String name = request.getParameter("name");
        // Call MyRemoteServiceServlet.incrementProduct(name) somehow
    }
}

and added this to web.xml: 并将其添加到web.xml:

<servlet>
    <servlet-name>IncrementServlet</servlet-name>
    <servlet-class>x.y.z.IncrementServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>IncrementServlet</servlet-name>
    <url-pattern>/increment</url-pattern>
</servlet-mapping>
  • Question 1 : Is this a sensible approach? 问题1 :这是明智的做法吗?
  • Question 2 : How do I go about invoking incrementProduct() from the HttpServlet? 问题2 :如何从HttpServlet调用增量产品()?

I happened to overlook one important fact that solved my problem: 我碰巧忽略了一个解决我问题的重要事实:

  • RemoteServiceServlet inherits HttpServlet ! RemoteServiceServlet 继承 HttpServlet

So I simply implemented doGet() in my RemoteServiceServlet like this: 因此,我像这样在RemoteServiceServlet中简单地实现了doGet():

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    switch (req.getServletPath()) {
    case "/increment":
        incrementProduct(req.getParameter("name"));
        break;
    }
}

And replaced IncrementServlet in web.xml with this: 并将web.xml中的IncrementServlet替换为:

<servlet-mapping>
    <servlet-name>MyRemoteServiceServlet</servlet-name>
    <url-pattern>/increment</url-pattern>
</servlet-mapping>

Which allows me to invoke incrementProduct(...) by entering this URL in a browser: 这使我可以通过在浏览器中输入以下URL来调用增量产品(...):

http://mywebshop.appspot.com/increment?name=pillow

which is exactly what I wanted! 这正是我想要的! There simply was no need for another HttpServlet :) 根本不需要另一个 HttpServlet :)

Question 1 In general as an classic servlet code this is fine. 问题1一般来说,这是经典的servlet代码。 As GWT application it does not use anything from GWT. 作为GWT应用程序,它不使用GWT中的任何内容。

Question 2 GWT will generate all the servlet code, you do not have to wrap it inside another servlet(IncrementServlet). 问题2 GWT将生成所有servlet代码,您不必将其包装在另一个servlet(IncrementServlet)中。 To turn this into a GWT application you have to implement AsyncCallback on client side entry point(onModuleLoad()) and on server side you must implement RemoteServiceServlet as you did already, what's missing now is the binding part - an interface definition extending RemoteService with a method having similar signature as your service method. 要将其转换为GWT应用程序,您必须在客户端入口点(onModuleLoad())上实现AsyncCallback,在服务器端必须像以前一样实现RemoteServiceServlet,现在所缺少的是绑定部分-接口定义扩展了RemoteService具有与您的服务方法相似的签名的方法。 Normally GWT plugin in any IDE would generate the binding part, if not then you can do it manually. 通常,任何IDE中的GWT插件都会生成绑定部分,如果没有,则可以手动进行。 See GWT documentation with step by step guide. 请参阅GWT文档以及逐步指南。 https://developers.google.com/web-toolkit/doc/1.6/DevGuideServerCommunication https://developers.google.com/web-toolkit/doc/1.6/DevGuideServerCommunication

Note that async rpc is a bit old and has some drawbacks. 请注意,异步rpc有点旧,并且有一些缺点。 GWT now has RequestFactory concept, but for your use case I would say it is a big overhead. GWT现在具有RequestFactory概念,但是对于您的用例,我会说这是很大的开销。

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

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