Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do you use a JSP and when a Servlet? [duplicate]

Tags:

java

jsp

servlets

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.

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.

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?
  • like image 802
    Antony Avatar asked Sep 19 '08 12:09

    Antony


    People also ask

    Why do we use JSP instead of servlet?

    Servlets can accept and process all type of protocol requests. JSP on the other hand is compatible with HTTP request only. In Servlet by default session management is not enabled, the user has to enable it explicitly. On the other hand in JSP session management is automatically enabled.

    Should I use servlet or JSP?

    Servlet should be used when there is more data processing involved whereas, JSP is generally used when there is less involvement of data processing. Servlets run faster than JSP, on the other hand JSP runs slower than servlet as it takes time to compile the program and convert into servlets.

    When should one use JSP?

    It is used to create dynamic web content. In this JSP tags are used to insert JAVA code into HTML pages. It is an advanced version of Servlet Technology. It is a Web based technology helps us to create dynamic and platform independent web pages.

    What is use of JSP and servlet?

    Java™ servlets and Java server pages (JSPs) are Java programs that run on a Java application server and extend the capabilities of the Web server. Java servlets are Java classes that are designed to respond to HTTP requests in the context of a Web application.


    1 Answers

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

    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.

    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.

    like image 164
    s3v1 Avatar answered Sep 21 '22 03:09

    s3v1