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.

7
  • 1
    Better... for what? – user40980 Jul 1 '13 at 18:28
  • @MichaelT for Scripting and including Java Expressions in it. Servlets can do the same work right. – JNL Jul 1 '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. – user40980 Jul 1 '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 1 '13 at 18:39
  • Would it only be because of the ease of the scripting language in JSP's than JAVA code? – JNL Jul 1 '13 at 18:41
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>");
      }
    }
6
  • 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 1 '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 1 '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 1 '13 at 19:08
  • @JNL take a look at jspc sometime... and run it against one of your jsps. – user40980 Jul 1 '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 1 '13 at 19:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.