Introduction to JSP
- JSP is a server side technology which helps to create a webpage dynamically using java as the programming language.
- JSP is a specification from Sun Microsystems. It is an extension to Servlet API.
A JSP Example
<%-- JSP comment --%> <HTML> <HEAD> <TITLE>MESSAGE</TITLE> </HEAD> <BODY> <%out.print("Hello, Sample JSP code");%> </BODY> </HTML>
Let us start learning with a simple JSP program. All JSP programs are stored as a .jsp files. Above is a simple JSP code, MyJSP.jsp which prints Hello, Sample JSP code. As discussed JSP is used for creating dynamic webpages. Dynamic webpages can have two types of contents – static & dynamic content.
The static contents can have text-based formats such as HTML, XML etc and the dynamic contents are generated by JSP elements.
Analysis of the above code –
1) The line <%–JSP Comment–%> represents the JSP element called JSP Comment, While adding comments to a JSP page you can use this tag, we will discuss this in detail in coming posts.
Note: JSP Comments must starts with a tag <%– and ends with –%>
2) Head, Title and Body tags are HTML tags – They are HTML tags, frequently used for static web pages. Whatever content they have is delivered to client(Web browser) as such.
3) <%out.print(“ Hello, Sample JSP code ”);%> is a JSP element, which is known as Scriptlet. Scriptlets can contain Java codes. syntax of scriptlet is: <%Executable java code%>. As the code in Scriptlets is java statement, they must end with a semicolon(;). out.print(“ Hello, Sample JSP code ”) is a java statement, which prints“ Hello, Sample JSP code”.
Servlet Vs JSP
Like JSP, Servlets are also used for generating dynamic webpages. Below is a comparison between them –
- Servlets are Java programs which supports HTML tags too.
- Generally used for developing business layer of an enterprise application.
- Servlets are created and maintained by Java developers.
On the other hand, JSP –
- JSP program is a HTML code which supports java statements too.
- Used for developing presentation layer of an enterprise application
- Frequenly used for desiging websites and used for web designers.
Advantages of JSP
- JSP has all the advantages that a servlet has, like: Better performance than CGI Built in session features, it also inherits the the features of java technology like – multithreading, exception handling, Database connectivity,etc.
- JSP Enables the separation of content generation from content presentation. Which makes it more flexible.
- With the JSP, it is now easy for web designers to show case the information what is needed.
- Web Application Programmers can concentrate on how to process/build the information.
Architecture of a JSP Application
Based on the location where request processing happens (Servlet OR JSP(java server pages)) there are two architectures for JSP. They are – Model1 Architecture & Model2 Architecture.
1) Model1 Architecture: In this Model, JSP plays a key role and it is responsible for of processing the request made by client. Client (Web browser) makes a request, JSP then creates a bean object which then fulfills the request and pass the response to JSP. JSP then sends the response back to client. Unlike Model2 architecture in this Model, most of the processing is done by JSP itself.
2) Model2 Architecture: In this Model Servlet plays a major role and it is responsible for processing the client’s(web browser) request. Presentation part (GUI part) will be handled by JSP and it performs this with the help of bean as shown in below figure. The servlet acts as controller and in charge of request processing. It creates the bean objects if required by the jsp page and calls the respective jsp page. The jsp handles the presentation part by using the bean object. In this Model, JSP doesn’t do any processing, Servlet creates the bean Object and calls the JSP program as per the request made by client.