什么时候使用 JSP,什么时候使用 Servlet? [英] When do you use a JSP and when a Servlet?

查看:67
本文介绍了什么时候使用 JSP,什么时候使用 Servlet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序可以将客户发送到另一个站点来处理付款.客户之外的另一个站点调用我们服务器上的一个页面,让我们知道付款的状态.被调用的页面检查支付应用程序提供的参数,并检查我们是否知道交易.然后更新数据库以反映状态.这一切都是在与客户没有任何互动的情况下完成的.

I have an application that sends the customer to another site to handle the payments. The other site, outside of the customer, calls a page on our server to let us know what the status is of the payment. The called page checks the parameters that are given by the payment application and checks to see whether the transaction is known to us. It then updates the database to reflect the status. This is all done without any interaction with the customer.

我个人选择将这个功能作为 JSP 来实现,因为在文件系统中放置一个文件比编译和打包文件然后在配置文件中添加一个条目更容易.

I have personally chosen to implement this functionality as a JSP since it is easier to just drop a file in the file system than to compile and package the file and then to add an entry into a configuration file.

考虑到页面的功能,我认为 servlet 将是首选选项.问题是:

  • 我的推测是否正确?
  • 是否有真正的理由在 JSP 上使用 servlet?
  • 这些原因是什么?
  • Considering the functionality of the page I would presume that a servlet would be the preferred option. The question(s) are:

  • Is my presumption correct?
  • Is there a real reason to use a servlet over a JSP?
  • What are those reasons?
  • 推荐答案

    JSP 在第一次运行时被编译为 servlet.这意味着它们之间没有真正的运行时差异.

    A JSP is compiled to a servlet the first time it is run. That means that there's no real runtime difference between them.

    然而,大多数人都有将 servlet 用于控制器和 JSP 用于视图的传统.由于控制器只是 Java 类,因此您可以从所有 IDE 获得完整的工具支持(代码完成等).与 JSP 相比,这提供了更好的质量和更快的开发时间.一些更高级的 IDE(想到的是 IntelliJ IDEA)具有很好的 JSP 支持,因此该论点已过时.

    However, most have a tradition to use servlets for controllers and JSPs for views. Since controllers are just java classes you can get full tool support (code completion etc.) from all IDEs. That gives better quality and faster development times compared to JSPs. Some more advanced IDE's (IntelliJ IDEA springs to mind) have great JSP support, rendering that argument obsolete.

    如果您正在制作自己的框架或只是使用简单的 JSP 制作它,那么您应该可以继续使用 JSP.没有性能差异,如果您觉得 JSP 更容易编写,那么请继续.

    If you're making your own framework or just making it with simple JSPs, then you should feel free to continue to use JSPs. There's no performance difference and if you feel JSPs are easier to write, then by all means continue.

    这篇关于什么时候使用 JSP,什么时候使用 Servlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆