JSP stands for Java Server Pages. JSP is a server side web technology like Servlet. JSP is extension of Servlet not enhancement of Servlet. It does not replace the servlet technology and can be used with Servlets. JSP is used to develop server side web components to generate and render dynamic contents

  • JSP is Java code embedded in HTML

Such design (Java with in HTML) allows smooth development of presentation layer. A UI designer/developer can develop HTML pages that are rich in UI feature. A Java developer can later easily embed JSP within these HTML pages. In case of servlets all the HTML contents should be embedded in Java. To achieve rich UI feature, servlets become more of HTML than java. Any change in UI, enforces change in Java code. Change in Java code needs compilation of Java file. This becomes more expensive

  • JSP file extension must be .jsp

Basic example of JSP

The typical request-response flow is depicted for JSP

  • User sends request to the server from index.jsp
  • Server receives the request and directs it to LoginServlet.java as per the URL pattern configuration
  • LoginServlet.java fetches data from request parameters, performs business logic, sets the data in request attributes and forwards the request to welcome.jsp. Not a single line of code to prepare the presentation as a part of response.
  • welcome.jsp is responsible for rendering all UI stuff, fetches the attributes from request set by LoginServlet.java, displaying the fetched data.

If welcome.jsp was not there then LoginServlet.java has to perform other tasks also like fetching the data, preparing HTML response, setting this data in HTML. All UI stuff servlet has to do means by Java Developer but most likely UI developer are more efficient to do UI stuff. So in case of servlet either Java developer should learn UI technologies or always work closely with any UI developer.

On the other hand in case of JSP this task of Java and HTML can be separated. Let Java developer write LoginServlet.java. Let UI developer write welcome.jsp in HTML only. Later let Java Developer embed the JSP code in welcome.jsp

Include directive v/s Include action

Footer with Map