如何将不同的Servlet链接在一起?
首先,我没有使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常的 MVC 方法是覆盖
HttpServlet#service()
方法并让业务逻辑也依赖于请求方法,如下通过HttpServletRequest#getMethod()
。另请参阅此答案。另一种方法确实是让 doGet() 和 doPost() 执行相同的逻辑,但我不会将一个委托给另一个,我宁愿委托它们两者都采用相同的独立方法。例如(半伪):
与
HttpServlet#service()
方法相反,这不采用 HTTPHEAD
、TRACE
、PUT
、OPTIONS
和DELETE
请求方法纳入考虑。您可能想忽略它们并让 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 byHttpServletRequest#getMethod()
. Also see this answer.An alternative approach is indeed to let
doGet()
anddoPost()
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):As opposed to
HttpServlet#service()
method, this does not take the HTTPHEAD
,TRACE
,PUT
,OPTIONS
andDELETE
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).在您的 GenericServlet 中只需执行以下操作:
因此 doPost() 将委托给 doGet()。
doGet() 的代码与以前相同。
In your GenericServlet just do:
So the doPost() will delegate to doGet().
And have the same code for the doGet() as before.
您还可以尝试执行 jsp 中的 servlet“委托”逻辑。使用 JSP 表达式语言 (EL) 和 JSTL 标记可以更轻松地完成此操作。
示例:
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:
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.