开发者

How to link different Servlet together?

开发者 https://www.devze.com 2022-12-26 23:26 出处:网络
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

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>开发者_JAVA百科
            <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


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).


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号