使用 servlet 的正确方法是什么?
我正在学习EJB3。
我有一个会话 bean,它提供创建/更新客户帐户的服务。
该会话 bean 提供以下服务:
public void addCustomer(Customer c);
public void updateCustomer(Customer c);
理想情况下,我希望有一个 servlet:CustomerServlet,它将调用我上面列出的会话 bean。
问题是我有两个 JSP:UpdateAccount.jsp 和 CreateAccount.jsp。这两个 JSP 都有一个带有方法 POST 和操作“CustomerServlet”的表单。
如何在客户 servlet 中区分我应该执行哪个操作:createAccount 或 updateAccount?
我想另一种选择是为每个操作都有一个单独的 servlet...
谢谢
I'm studying EJB3.
I have a session bean which provides services to create/update customer accounts.
This session bean offers services on the lines of:
public void addCustomer(Customer c);
public void updateCustomer(Customer c);
Ideally I'd like to have a single servlet: CustomerServlet and it would invoke the session beans that I have listed above.
Problem is that I have two JSPs: UpdateAccount.jsp and CreateAccount.jsp. Both of these JSPs have a form with a method POST and action "CustomerServlet".
How can I distinguish in a customer servlet which operation I should carry out: createAccount or updateAccount?
I guess the alternative is to have a separate servlet for each operation...
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不太确定最佳实践,但我有一些可能有效的建议:
如果您的表单是使用提交按钮提交的,您可以根据<按钮名称>范围。因此,如果您的按钮具有值
Update
和Create
且名为account-submit
,请检查使用request 获得的值。 getParameter('account-submit')
,您可以知道单击了哪个按钮来生成此请求。如果您以不同的方式命名它们,您也可以只检查两个参数中哪一个不为空,然后您就知道您正在处理哪个表单提交。请注意,如果表单中只有一个文本字段,并且用户按
Enter
而不是单击按钮,您将在 servlet 中得到null
!请参阅我关于此行为的博客文章检查
Referer
标头 - 我真的不推荐这样做,因为您并不总是知道已部署应用程序的上下文,该值可能并不总是存在,并且可以很容易地欺骗。为您的 servlet 添加另一个映射,以便可以在 http://myapp.example.com 上访问它/context/create 和 http://myapp.example.com/context/update。然后,您可以检查
ServletPath
(request.getServletPath()
) 以查看请求来自哪个“servlet”。我可能会选择这个,因为它对我来说似乎是最强大的,但您可能还想添加其他两项检查来确保。在您的web.xml
中,您需要类似I'm not really certain about the best practice for this but I have a couple of suggestions that might work:
If your form is being submitted using a submit button, you could distinguish the request on the basis of the value of the <button-name> parameter. So if your buttons had the values
Update
andCreate
and were namedaccount-submit
, by checking the value you get withrequest.getParameter('account-submit')
, you'd be able to tell which button was clicked to generate this request. If you named them differently, you could also just check which of the two parameters was not null and you'd know which form submit you were handling.Note that if you have only a single text field in your form and the user hits
Enter
instead of clicking the button, you'll get anull
in your servlet! See my blog post about this behaviour.Check the
Referer
header - I wouldn't really recommend this since you wouldn't always know the context of the deployed app, this value may not always be present and it can be easily spoofed.Add another mapping for your servlet so that it's accessible at both http://myapp.example.com/context/create and http://myapp.example.com/context/update. You can then check the
ServletPath
(request.getServletPath()
) to see what 'servlet' the request came in for. I'd probably go with this one since it seems the most robust to me but you might also want to add the other two checks just to make sure. In yourweb.xml
, you'd want something likeJSP 是 Servlet,只是源代码形式不同,没有理由
POST
到不同的 Servlet,您只需POST
返回到相同的 JSP。JSPs are Servlets, just in a different source code form, there is no reason to
POST
to a different Servlet, you can justPOST
back to the same JSP.您不需要 servlet。 JSP(或 Facelets)可以通过 EL 直接与 Bean 对话。
You don't need the servlet. JSPs (or Facelets) can talk directly to the beans via EL.