3

(Rewritten for reopening:)

What are relative advantages of Servlets over JSP, or vice versa?

Might JSP be preferred because it is easy to put code inside JSP, or Servlets because with them code can be separated from view? Are there other considerations?

Any information regarding this will be immensely helpful.

9000
  • 24,162
  • 4
  • 51
  • 79
JNL
  • 904
  • 2
  • 8
  • 18
  • 1
    Better... for what? –  Jul 01 '13 at 18:28
  • @MichaelT for Scripting and including Java Expressions in it. Servlets can do the same work right. – JNL Jul 01 '13 at 18:33
  • There are some things that are easier in JSPs. There are others easier in raw java servlets. Writing an application using only one of the tools is likely going to lead to poor code. –  Jul 01 '13 at 18:37
  • @MichaelT Thanks for your response Michael, Can you help me understand it better with some References if you know of or some examples. I understand that it would be easy for UI Developers to use the scripting language and use it in the expressions, but if JSP's gets converted as Servlets, what would be the advantage over it? – JNL Jul 01 '13 at 18:39
  • Would it only be because of the ease of the scripting language in JSP's than JAVA code? – JNL Jul 01 '13 at 18:41
  • anything asking whether something is "better" in general is inherently flawed. In this specific case, there is no "best" overall, use a combination of both as needed to get the results desired. – jwenting Jul 02 '13 at 12:06
  • JSP is just a fancy Servlet. – Koray Tugay Jul 07 '15 at 04:35

1 Answers1

4

The main advantage of JSP is that it's are easier to code and to read when you are creating a dynamic HTML front-end.

That's because you write mainly HTML and in some places embed Java code.

In a servlet you would have to invert the logic, ie, write java code and print HTML.

That's because in the presentation layer most code is HTML/JS.

On the other hand, business logic is in most part Java code, so in that case it's better to use Servlets or POJOs (plain, old Java objects).

EDIT: there's no performance difference since JSP code, as such, is never run, it's converted to a servlet. Only the first time you run the JSP after it has been changed, it takes a little longer to run because it has to be converted to servlet.

Compare a simple "Hello World" program in JSP vs servlet:

JSP:

<html>
        <head><title>Hello World JSP Page.</title></head>
        <body>
                <font size="10"><%="Hello World!" %></font>
        </body>
</html>

Servlet

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

    public class HelloWorld extends HttpServlet{ 
      public void doGet(HttpServletRequest request, 
      HttpServletResponse response)
      throws ServletException,IOException{
      response.setContentType("text/html");
      PrintWriter pw = response.getWriter();
      pw.println("<html>");
      pw.println("<head><title>Hello World</title></title>");
      pw.println("<body>");
      pw.println("<h1>Hello World</h1>");
      pw.println("</body></html>");
      }
    }
Tulains Córdova
  • 39,201
  • 12
  • 97
  • 154
  • Agree with your point, that it is easier to inlcude HTML in JSP than the other way around. Just curious is there any advantage with respect to performance in JSP than Servlets? – JNL Jul 01 '13 at 18:51
  • @JNL The only difference in performance is when you run a modified JSP for the first time, since it has to be converted. After that, it's a servlet as any other one. – Tulains Córdova Jul 01 '13 at 18:57
  • So is it not same with the Servlets, it runs through the entire lifecycle only if we change the Servlet code? – JNL Jul 01 '13 at 19:08
  • @JNL take a look at [jspc](http://docs.oracle.com/cd/E19146-01/821-0790/6nlqo2pgh/index.html) sometime... and run it against one of your jsps. –  Jul 01 '13 at 19:19
  • @MichaelT Thanks Michael, Appreciate it. I will try to get back to this question, need to phrase it appropriately, Thanks – JNL Jul 01 '13 at 19:25
  • for simple stuff JSP is much easier to read, but larger files, the verbosity is about the same. What thing that servlets offer is that it is easy to debug – Oliver Watkins Apr 24 '18 at 10:57