如何将不同的Servlet链接在一起?

发布于 2024-08-29 08:32:45 字数 1239 浏览 3 评论 0原文

首先,我没有使用Spring MVC。 :) :) 只是想先把它弄出来。 现在我拥有的是调用不同 Servlet 的不同 JSP 页面。所有的部分单独工作都很好,但我需要将它们链接在一起。如果所有 jsp 页面都发出 GET 请求,那么这很容易,因为我只需通过网址传递 type ,并且在我的 servlet 端,我只需枚举通过所有参数,确定它是哪种类型,并委托给正确的servlet。但并不是所有的jsp页面都会发出GET请求,有些页面会通过表单发出POST请求。让我们看一下示例

 A.jsp
 $.getJSON('GenericServlet?type=A', ...

 GenericServlet.java
 String type = request.getParameter("type");    
 if(type.equals("A")){
     //Somehow delegate to Servlet A (Not sure how to do that yet :))
 }

,但在 B.jsp 中我会得到类似的东西,

 B.jsp
 <form action="GenericServlet" method="post">
    <table border=0 cellspacing=10 cellpadding=0>
        <tr>
            <td>User Name:</td>
            <td><input type="text" name="username" size=22/></td>
        </tr>
        <tr>
            <td>Password:</td>
            <td><input type="password" name="password" size=22/></td>
        </tr>
    </table>
    <input type="submit" value="Create User" />
</form>

我很难在 GenericServlet.java 中确定这需要转到 servletB

First of all, I did not use Spring MVC. :) :) Just want to get it out first.
Now what I have is different JSP pages that making calls to different Servlets. All the pieces work great individually but I kind of need to link them together. If all of jsp pages make GET request then it would be easy, since I would just pass a type via the web address, and on my servlet side, I would just enumerated through all the parameter, determine which type is it, and delegate to the right servlet. But not all jsp pages make GET request, some make POST request via form. Let see example

 A.jsp
 $.getJSON('GenericServlet?type=A', ...

 GenericServlet.java
 String type = request.getParameter("type");    
 if(type.equals("A")){
     //Somehow delegate to Servlet A (Not sure how to do that yet :))
 }

but in B.jsp I would have something like this

 B.jsp
 <form action="GenericServlet" method="post">
    <table border=0 cellspacing=10 cellpadding=0>
        <tr>
            <td>User Name:</td>
            <td><input type="text" name="username" size=22/></td>
        </tr>
        <tr>
            <td>Password:</td>
            <td><input type="password" name="password" size=22/></td>
        </tr>
    </table>
    <input type="submit" value="Create User" />
</form>

It kind of hard for me to determine in GenericServlet.java that this need to go to servletB

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

飘过的浮云 2024-09-05 08:32:45

通常的 MVC 方法是覆盖 HttpServlet#service() 方法并让业务逻辑也依赖于请求方法,如下通过 HttpServletRequest#getMethod()。另请参阅此答案

另一种方法确实是让 doGet() 和 doPost() 执行相同的逻辑,但我不会将一个委托给另一个,我宁愿委托它们两者都采用相同的独立方法。例如(半伪):

protected void doGet(request, response) {
    process(request, response);
}

protected void doPost(request, response) {
    process(request, response);
}

private void process(request, response) {
    // Do your thing here.
}

HttpServlet#service() 方法相反,这采用 HTTP HEADTRACEPUTOPTIONSDELETE 请求方法纳入考虑。您可能想忽略它们并让 servletcontainer 以“默认”方式处理它们(即返回 不允许 HTTP 405 方法)。

The usual MVC approach is to override the HttpServlet#service() method and let the business logic depend on the request method as well, as obtained by HttpServletRequest#getMethod(). Also see this answer.

An alternative approach is indeed to let doGet() and doPost() do both the same logic, but I would not delegate the one to the other, I would rather delegate them both to the same independent method. E.g. (semi-pseudo):

protected void doGet(request, response) {
    process(request, response);
}

protected void doPost(request, response) {
    process(request, response);
}

private void process(request, response) {
    // Do your thing here.
}

As opposed to HttpServlet#service() method, this does not take the HTTP HEAD, TRACE, PUT, OPTIONS and DELETE request methods into account. You may namely want to ignore them and let the servletcontainer handle them the "default" way (i.e. returning HTTP 405 Method Not Allowed).

二手情话 2024-09-05 08:32:45

在您的 GenericServlet 中只需执行以下操作:

  public void doPost(HttpServletRequest request,HttpServletResponse response) 
         throws ServletException, IOException {
    doGet(request, response);
  }

因此 doPost() 将委托给 doGet()。

doGet() 的代码与以前相同。

In your GenericServlet just do:

  public void doPost(HttpServletRequest request,HttpServletResponse response) 
         throws ServletException, IOException {
    doGet(request, response);
  }

So the doPost() will delegate to doGet().

And have the same code for the doGet() as before.

苄①跕圉湢 2024-09-05 08:32:45

您还可以尝试执行 jsp 中的 servlet“委托”逻辑。使用 JSP 表达式语言 (EL) 和 JSTL 标记可以更轻松地完成此操作。

示例:

<c:if test="${param.type == 'A'}>
   call servlet 1
</c:if>
<c:if test="${param.type == 'B'}>
   call servlet 2
</c:if>

Servlet 1 或 2 可以根据需要实现 doGet() 或 doPost()。或者您可以按照 Heavy Bytes 的建议,将 doPost() 委托给 doGet()。

这样也许您可以取消 GenericServlet。

You can also try to perform the servlet 'delegation' logic you have inside your jsp. You can do this much easier using the JSP Expression Language (EL) and JSTL tags.

Example:

<c:if test="${param.type == 'A'}>
   call servlet 1
</c:if>
<c:if test="${param.type == 'B'}>
   call servlet 2
</c:if>

Servlet 1 or 2 could implement either doGet() or doPost() as needed. Or you can go by Heavy Bytes' sugestion of having the doPost() delegate to doGet().

This way maybe you can do away with your GenericServlet.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文