JAVA SERVER PAGES!

- javax.servlet.jsp &
- javax.servlet.jsp.tagext

In many ways, Java Server Pages look like standard HTML and XML documents. In fact, they normally include HTML or XML markup. Such markup is known as fixed-template data or fixed-template text. JSP programmers can reuse Java Beans and create custom tag libraries that encapsulate complex, dynamic functionality.
Programmers tend to use JSP when most of the content sent to the client is fixed template data and only a small portion of the content is generated dynamically with Java code. On the other hand, Servlets are preferred only when a small portion of the content sent to the client is fixed-template data. In most cases, servlet and JSP are interchangeable.
As with servlets, JSPs normally execute as part of a Web Server, which acts as a container to JSP. When a JSP-enabled server receives the first request for a JSP, the JSP container translates that JSP into a Java servlet that handles the current request and the future requests to the JSP.
The request and response mechanism and life cycle of a JSP are the same as that of a servlet. JSPs can define methods jspInit and jspDestroy (similar to servlet methods init and destroy), which the JSP container invokes when initializing a JSP, and terminating a JSP respectively.
A simple JSP program that displays the current time of the server is as follows:
<?xml version = “1.0”>
<head>
<meta http-equiv = “refresh” content = “60” />
<title> A simple JSP Clock </title>
<style>
.big {font-family : Helvetica, arial, sans-serif;
Font-weight: bold; font-size : 2em;}
</style>
</head>
<body>
<p class = “big”> Simple JSP Example </p>
<table style= “border : 6px outset;”>
<tr>
<td style= “background-color : black;”>
<p class = “big” style = “color : cyan;”>
<%= new java.util.Date() %>
</p>
</td>
</tr>
</table>
</body>
</html>
In this example, JSP is composed of HTML tags and Java code. The HTML tags define the web page, which would be sent normally as a response to a client’s request. Apart from HTML tags, there is only one line of java code that retrieves the current data and time of the server and includes it as part of the web page. The code is as follows:
<%= new java.util.Date() %>
This line of code is a JSP expression and is delimited by <%= and %>. This line of code creates a new instance of class Date from the package java.util to get the current time and date from the server. To return the result as part of the web content we use = (equal) sign which calls the response object’s write method. Therefore, the above line of code is equivalent to the following statement:
<% response.write( new java.util.Date() ) %>
The JSP container converts the result of every JSP expression into a string that is output as part of the response to the client. In the above example, there is an HTML tag with the meta element. This element is used to set a refresh interval of 60 seconds for the document. This causes the browser to request clock.jsp every 60 seconds, so that the date and time are updated every minute with the current date and time of the server.
Implicit Objects:
Implicit objects are objects provided in JSP for having access to many Servlet capabilities in the context of a Java Server Page. Implicit objects have four scopes: Application, Page, Request and Session. The objects having the application scope can be used by both JSP and Servlet used in a web application. Objects with page scope exist only in the page that defines them.
Objects with request scope exist for the duration of the request. For e.g., a JSP can partially process a request, then forward the request to another Servlet or JSP for further processing. Request-scope objects go out of scope when request processing completes with a response to the client. Objects with session scope exist for the client’s entire browsing session.
Many of the implicit objects extend the classes that implement interfaces provided in the Servlet API. Thus, JSP can use the same methods that a Servlet use to interact with such objects. The implicit JSP objects and their description are as follows:
- Implicit ObjectsDescriptionApplication scopeapplicationThis is an instance of javax.servlet.ServletContext class. It acts as a container in which JSP executes.Page scope ObjectsconfigThis is an object of javax.servlet.ServletConfig class. It represents the JSP configuration options.exceptionThis is an instance of java.lang.Throwable class. It represents the exception that is passed to the JSP error page.outIt is an instance of javax.servlet.jsp.JspWriter class. It writes text as part of the response to a request. It is used implicitly with JSP expressions and actions that insert String content in a response.pageThis javax.lang.Object represents a reference for the current JSP instance.pageContextThis javax.servlet.jsp.PageContext object hides the implementation details of the underlying Servlet and JSP container and provides JSP programmers with access to the implicit objects.responseThis object is an instance of a class that implements HTTPServletResponse of javax.servlet.http package. It represents the response to the client.Request scoperequestThis object is an instance of a class that implements HTTPServletRequest of javax.servlet.http package. It represents the client request.Session scopesessionThis is an instance of the class javax.servlet.http.HTTPSession and represents the client session information if such a session has been created.
Table: Implicit JSP Objects with their description