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.
1. javax.servlet.jsp.tagext
2. javax.servlet.jsp
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.
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, IOExceptionIn 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); %>