JSP stands for Java Server Pages, which is a Java-based technology developed by SUN Microsystems to simplify the development of dynamic Web pages. JSP pages provide a means of separating the presentation logic from the business logic and help to directly embed Java code in static Hypertext Markup Language (HTML) pages. The presentation logic is provided by HTML tags and the business logic for dynamic content is handled by JSP tags. JSP is a server-side technology and Java Server Pages are processed on a Web server in the JSP Servlet engine. When a client requests a Java Server Page, the JSP Servlet engine first processes the requested page to generate the HTML code for dynamic content, then compiles the page into a servlet, and then executes the resulting servlet to generate an output to the client.
Introducion of JSP Technology
JSP is a Java-based Web application development technology and serves as an advancement of the Java Servlet technology. Before the introduction of Java Server Pages, servlets were used by Java programmers to provide dynamic content to clients. In the case of servlets, both the processing of requests and the generation of responses were handled by a single servlet class. Servlets usually contain HTML code embedded in Java code. Therefore, programmers who created servlets needed a thorough knowledge of Java programming and basic knowledge about HTML programming. Also, servlets do not separate the presentation logic from the business logic in an application.
JSP greatly simplifies the process of creating dynamic Web pages. In JSP, the Java code is embedded within the HTML code to provide dynamic content to a client. There is a clear separation between the presentation logic, which is provided by HTML tags, and the business logic, which is handled by the embedded Java code. This separation allows you to divide the tasks of providing code for presentation and business logic in an application between designers programmers having different skills. This implies that, in the case of JSP, designers can implement the presentation logic code and the user interfaces in HTML, and Java programmers can later add the code for dynamic content to implement the business logic. As far as their execution is concerned, Java Server Pages provide all the benefits of servlets. Apart from this, JSP pages are easier to write than servlets. Java Server Pages is translated to a servlet during compilation, and the response is sent to a client by generating dynamic content in the form of the servlet.
JSP vs Java Servlet
JSP offers various advantages over Java Servlet. It provides better server-side scripting support as compared to Java Servlet. Java programmers can create servlets easily, but handling Java code is always a tough job for Web page designers. As stated earlier, a JSP page has HTML code, and some Java code embedded with the HTML code. This makes creating a Web page as easy as the basic designing of the page is done by using the HTML code. You can use the Java code to add dynamic content to the page. The Java code is embedded by using different JSP constructs.
The following are some reasons to prefer JSP over Java Servlet:
- Allows you to place static code and components symmetrically on a Web page. These static Web pages are designed by Web designers by using HTML code. Designing static Web pages by using servlets requires a good knowledge of Java, but Web designers may not be comfortable with Java programming constructs. Also, while using servlets, you have to enclose HTML code in the out.println() method, which disables ail Integrated Development Environment (IDE) support for generating HTML content, Therefore, using JSP is always easier than using servlets for the same HTML output.
- Facilitates automatic recompilation of a Java Server Pages by a Web container for any update in the code; whereas, you need to recompile your servlet for every single change in the source code of the servlet.
- Allows you to access a Java Server Pages directly as a simple HTML page; whereas, servlets cannot be accessed directly and have to be first mapped in the web.xml file.
- Allows you to create a Java Server Pages by using a simple HTML template and the Java Server Pages is automatically handled by the JSP container. However, in servlets, you need to provide the Java code to generate dynamic HTML pages.
These advantages of JSP make it a popular technology to use in the presentation logic of any Web application.
Learn more about the Life Cycle of a JSP page.