1.1 JSP Overview
JSP (Java Server Pages) is Oracle specification and a server side technology used to implement presentation part of web application. JSP runs on the server machine and capable of rendering dynamic views as compared to HTML which can render static content only.
JSP enables putting java code with a pure HTML code and thus renders dynamic content.
When a first request to a JSP came, JSP is translated in to Java (Servlets) and compiled to get class files out of it.
JSP provides support other functionality like custom tag library, expression language etc. We will discuss all these features in detail in later chapters.
Configuring and accessing JSP pages is much more simpler as compared to Servlets. For Servelts , we need to configure it in web.xml or need to use annotation and implement a interface but nothing is needed in case of JSP. Just need to place a jsp file at a defined location and once deployed it can be accessed.
Along with the JSP there are three constructs ( directives, scriplets and actions ) which can be embed and helps in providing dynamic behaviour.
JSP acts as a View layer in MVC design pattern and a Good Design approach is
- to divide the web application in a view and business logic processing parts
- to use servlets for business logic
- to use JSP for view part.
Note: It is recommended to minimize the use of Java code in JSP and instead use Jstl , expressions tag to achieve dynamic behaviour.
JSP specification provides nine implicit or predefined objects like request, response etc to use directly within a scriplets.
1.2 Advantages of JSP
· JSPs are much convenient to write as compared to Servlets and specifically writing a presentation code in servlets is very difficult. Writing presentation code (HTML) requires much of the println statements and a small change in css class would require change in servlet., compilation and would require a complete testing as business logic is embedded in that servlet along with presentation code.
· In JSP we can simply use HTML code and easy to maintain.
· Any change in JSP code will not require any manual compilation (as container automatically does compilation of JSP) where as in case of servlet , manual compilation is required.
· JSP has all the advantages of Servlets like better performance as compared to traditional Common Gateway Interface scripts in terms of both memory and processing time.
· JSP has an access to all other java classes and functionality like RMI, sockets.
· JSP provides several features like use of expression language (EL), jstl tags using which a person having less knowledge of Java can create JSP.
1.3 Simple Example of JSP
Let’s have a look at simple JSP file just to get a feeling of what we have discussed above.
<HTML> <HEAD> <TITLE> Sample JSP File </TITLE> </HEAD> <BODY> <STRONG> <% out.println("Hello, This is sample message which will be displayed on browser "); %> </STORNG> </BODY> </HTML>
Save the above file as helloworld.jsp . All JSP files must have an extension “ .jsp ” .Now if you look at the above code, it is a mix of plain HTML code and a dynamic code using out.println() statement.
<% %> tags are known as scriplets in which we can write any java code . We will discuss this in detail in later chapters.
1.4 JSP Comments
JSP specification has defined <%-- comment --%> structure for comments .
Comments added between <%-- --%> tag is not included in response object and thus not available even in the view source
<%-- Comments written inside this tag will not be displayed even in view source --%>
There is another way of adding comments like <!—Comments --> .
This is a HTML comment and not a JSP comment and container will consider this as any other HTML tag. Though this will not be displayed on browser but can be seen in view source
<!-- HTML style Comments written inside this tag will be displayed in view source -->
Also as I mentioned earlier that in scriptelts ( <% %> ) tag we can write any java code which means including java style comments (// and /* */ ) like below
// Single line comments in scriplet using java comments style
Multi line comments in scriplet using java comments style
To see it practically , lets create one jsp (comments.jsp ) like below and place it in WebContent folder of web application–
<html> <head> <title> JSP Comments </title> </head> <body> <H4> JSP comments in practical.</H4> <%-- Comments written inside this tag will not be displayed even in view source --%> <!-- HTML style Comments written inside this tag will be displayed in view source --> <% // Single line comments in scriplet using java comments style /* Multi line comments in scriplet using java comments style */ % </body> </html>
On accessing comments.jsp , we will see only “JSP comments in practical.” Because rest all are the comments so will not be displayed on browser.
Lets see what all we can see on page source of browser.
We can see there is no comments added in JSP style but we can see the comments added in HTML style. Also we can see entire scriplet tag is not visible in page source.