使用 servlet 的正确方法是什么?

发布于 2024-10-18 21:12:03 字数 468 浏览 4 评论 0原文

我正在学习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 技术交流群。

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

发布评论

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

评论(3

枕梦 2024-10-25 21:12:03

我不太确定最佳实践,但我有一些可能有效的建议:

  1. 如果您的表单是使用提交按钮提交的,您可以根据<按钮名称>范围。因此,如果您的按钮具有值 UpdateCreate 且名为 account-submit,请检查使用 request 获得的值。 getParameter('account-submit'),您可以知道单击了哪个按钮来生成此请求。如果您以不同的方式命名它们,您也可以只检查两个参数中哪一个不为空,然后您就知道您正在处理哪个表单提交。

    请注意,如果表单中只有一个文本字段,并且用户按 Enter 而不是单击按钮,您将在 servlet 中得到 null !请参阅我关于此行为的博客文章


  2. 检查 Referer 标头 - 我真的不推荐这样做,因为您并不总是知道已部署应用程序的上下文,该值可能并不总是存在,并且可以很容易地欺骗。

  3. 为您的 servlet 添加另一个映射,以便可以在 http://myapp.example.com 上访问它/context/createhttp://myapp.example.com/context/update。然后,您可以检查 ServletPath (request.getServletPath()) 以查看请求来自哪个“servlet”。我可能会选择这个,因为它对我来说似乎是最强大的,但您可能还想添加其他两项检查来确保。在您的 web.xml 中,您需要类似


    <servlet>
        <servlet-name>CreateUpdateServlet</servlet-name>
        <servlet-class>my.package.CustomerServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CreateUpdateServlet</servlet-name>
        <url-pattern>/create</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>CreateUpdateServlet</servlet-name>
        <url-pattern>/update</url-pattern>
    </servlet-mapping>

I'm not really certain about the best practice for this but I have a couple of suggestions that might work:

  1. 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 and Create and were named account-submit, by checking the value you get with request.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 a null in your servlet! See my blog post about this behaviour.

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

  3. 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 your web.xml, you'd want something like

    <servlet>
        <servlet-name>CreateUpdateServlet</servlet-name>
        <servlet-class>my.package.CustomerServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CreateUpdateServlet</servlet-name>
        <url-pattern>/create</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>CreateUpdateServlet</servlet-name>
        <url-pattern>/update</url-pattern>
    </servlet-mapping>
星星的轨迹 2024-10-25 21:12:03

JSP 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 just POST back to the same JSP.

与他有关 2024-10-25 21:12:03

您不需要 servlet。 JSP(或 Facelets)可以通过 EL 直接与 Bean 对话。

You don't need the servlet. JSPs (or Facelets) can talk directly to the beans via EL.

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