JSP - SPLessons
boy2 Chapter 6

JSP Basics

JSP - SPLessons

JSP Basics

shape Introduction

JSP Basics, JSP stands for Java Servlet Pages. It is utilized for server-side technology and it is an augmentation of Servlet technology that was created by Sun Microsystem.Servlet will get executed on client browser where as JSP will get executed on the server.

JSP is utilized to create dynamic web applications like as a Servlet technology, but gives extra functionalities like custom tags, JSTL, expression dialect. JSP concentrates for the most part of presentation logic of the application. Servlet run quicker than JSP, the fundamental favorable position of JSP is less coding contrasted with servlets.

JSP contains both Java and HTML in the same file. JSP is same like HTML, but the difference in JSP developer writes Java code with script tags. JSP can be easily managed because business logic is separated from presentation logic. But in Servlet technology, both will be mixed up. When JSP page is updated, there is no need to recompile and redeploy the project.

JSP uses mainly two packages.

Advantages of JSP

shape Advantages

More Features compared to Servlet: JSP is an extension to Servlet technology, JSP have predefined tags, Implicit object, Custom tags, Expression language that makes JSP have reduced the code and development code easier.

Maintainance is easier: JSP is used for both development and also for designing part. JSP separates both development part and designing part while comparing to a Servlet. In Servlets, both designing part and development part is written in a single Servlet class.

Redeploy and recompile: When the JSP file is modified, there is no need to redeploy and recompile the code while in Servlet, the file must be recompiled and redeploy the Servlet program.

Less code: JSP have lots of tags like action tags, custom tags, predefined tags, an implicit object.So, there is  no need to write heavy code.

Platform Independent : As JSP is developed using Java technology it is paltform independent.

shape Conceptual
figure

Web container is responsible to convert JSP pages into servlet.

Request Object Creation

shape Description

Request Object is requested to retrieve the object.

In Servlet

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//HttpSErvlet class is extended
public class DemoRequestDispatcher extends HttpServlet
{
  // service method is called
    public void doGet(HttpServletRequest reques, HttpServletResponse response)
           throws ServletException, IOException{

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    String user_name = request.getParameter("user_name");
    out.println(user_name);
}
}

In servlet to request the object developer has to create class that should extend from HttpServlet.

public class DemoRequestDispatcher extends HttpServlet

Service method should be called.Where doGet used to get the object.

 public void doGet(HttpServletRequest reques, HttpServletResponse response)
           throws ServletException, IOException

In JSP

In JSP no need to write long code so this is the advantage with JSP compared to servlet.To retrieve the object above code can be written as follows.

<% String user_name = request.getParameter(“user_uname”); out.print(user_name); %>


Summary

shape Key Points

  • JSP Basics – JSP is similar to servlet, but JSP has some additional features than a servlet.
  • JSP Basics – JSP pages are easier to maintain.
  • JSP Basics – JSP takes less code compared to servlets.
  • JSP Basics – After updating a JSP file, no need to recompile the project.
  • In get() limited data will be sent through the header and in the Post() maximum data will be sent through the body.