23 Introduction to JSP

Pravin Jain

epgp books

 

Introduction

 

Welcome to this module on Introduction to JSP. Earlier we have used servlets in our web application. Servlets have their own life cycle in which they get initialized and after initialization a regular invocation of their service method whenever there is a request coming from a client. In the service method the Servlet receives instances of ServletRequest and ServletResponse as parameters. Typically the Servlet would get the instance of PrintWriter from the ServletResponse and then on this writer, it would write the Output(noramlly HTML) for presentation on the browser. Designing the HTML output is normally the job a Web Designer and not of a Programmer. The service method is then full of out.println(“<some html tags>”); .

 

What we have here is that instead of a programmer having to write all the html tags in double quotes for output in a java file for a Servlet, in tis service method. Other than the service method the other part of the class may be similar in all cases. Someimes, the things to be presented may sometimes have somekinds of expressions,, and some other things.

 

Instead of writing a Servlet inthis manner, suppose, we give the task of creating the web page to a web page designer and he gives us a good HTML page. We might think of designing some utilty which can read the HTML, convert it into a Java code, which is a Servlet and then compiles it and use it like a Servlet. This is exactly what is available to us, in the Web Container in the form of JSP engine.

 

What is JSP

 

JSP is an acronym for Java Server Pages. In the Web Container, we have a JSP engine. What the JSP engine does is, it translates into java. A JSP file is not a Java file.A JSP file is made up of lots of tags (HTML tags) according to the output we want to see in the browser.

 

The task of the JSP engine is to translate the JSP file into a Java file which is an equivalent Servlet. JSPs are Servlets. They are special kind of a Servlet. Like Servlet have a life-cycle, even JSPs have their own life-cycle.

 

What can a JSP contain?

 

A JSP can contain anything which can normally go in a HTML file. You can pickup an HTML file rename it as .jsp and if you have it as part of your web application and access it just like an HTML, just like accessing any other static resource, the JSP engine would come into picture. It would then do the task of translation of our JSP , into a Java file and that Java file is also getting compiled and then it is used like a Servlet.

 

JSP API

 

The translated Java file is a kind of extension of a Servlet. We do have a separate API for JSP, just like we have an API for the Servlet. We have this API in the package javax.servlet.jsp. This package contains a few useful interfaces. We have a JspPage interfaces available in this package. JspPage is the general interface, and there is one specific interface called HttpJspPage. So the translation is into a

class which is an implementation of these interfaces.

 

A JSP is nothing bot, it is a special kind of a Servlet. As far as coding is concerned, we don’t have to write a Java code here, we simply have to write the content, fromt the point of view of the output. We can simply look at the output which we want to have, and from that point of view, we just create a web-page, and you can give it an extension of jsp.

 

Let us see what kind of elements can go in a JSP. A JSP can contain everything which can go in a HTML. It can contain all HTML tags like, if someone asks, “can we include Javascript?” The answer is same as the answer for question “Is Javascript allowed in html?”, yes. ie. Anything, which is allowed in html is allowed in JSP.

 

Over and above whatever is available for HTML, we have certain additional things, which can also be included in a JSP. Ultimately, whatever we put in JSP, ultimately it gets translated into a service method, where within the service method, we will have out.println(“<any valid html>”).

 

Scripting elements in a JSP

 

Now, over and above this valid html, our JSP includes more kinds of elements. It is not just the elements available for HTML. JSP can have its own additional elements, eg. It has something called expression.

 

expression <%= java-expression %>

 

An expression would be written as <%=, and hen you can put any kind of java expression and end it with a %>. So, we have a Java expression which can be included within a JSP..

 

Now what is the effect of expression, or a JSP scripting element called expression. The JSP scripting element, expression, can be used to include result of evaluating any kind of Java expression in the output. Now, what kind of objects can I use in this java expression. Someone may like to have a date, maybe wants to create an object of java.util.Date, and print it out. Whatever we write in a JSP is normally meant for output, and it is mostly HTML. An expression is also meant for output. eg. When we use <%= new java.util.Date() %> anywhere in a JSP, then what is going to be the output. The output would be all the HTML content as it is before the expression and then the current Date and time.

 

From the translation point of view, all the html gets translated into a java statement out.println(“the html content of jsp”); and and expression is translated into a Java statement out.println(new java.util.Date()); inside the service method. This is how expressions are used.

 

So, we have seen the first scripting element of a JSP, which is an expression.

 

scriptlet <% java code segment %>

 

Another scripting element which can go in a JSP page is a scriptlet. In a JSP, most of the things get translated into out.println… , but many times we might like include some different type of code. It may not always be about output, eg. We might like to have a for loop, because we want to repeat output of certain part certain number of times. So, how do we dot this. We don’t have any such provision in HTML. We will have to put that HTML a number of times. So insteading of repeating the HTML a number of times in the JSP, We can think about a translation, in which we can have a for loop. eg. If we wnat it repeated five times, we might like to have something like for (int i = 0; i < 5; i++) { and then the out.println(….) for the output to be repeated and then ending the for loop also. So, how can be have a for loop included in this manner.. Here, you like the JSP engine to do the translation, to include some java code segments, as part of the service method. In the translated service method, For including any such java code segment in a JSP file, we have a scripting element called scriptlet.. A scriptlet is written as, <% any-valid-java-code-segment %>. If we include this in a JSP, in the translation, it will simply be included as it is. There is no out.println or any such thing happening with the scriptlet.. So scriptlet can be used for embedding any java code segment within your service method. So, for eg., what we could be doing here

 

<some valid html code>

<% for (int i = 0; i < 5; i++) {  %>

<some more valid html code>

<% } %>

<rest of the html code>

So in the translation we will have, out.println(“<some valid html code>”);

for (int i = 0; i < 5; i++) {

out.println(“<some more valid html code>

}

out.println(“<rest of the html code>”) within our service method.

 

So, the final output which goes to the client will have that portion repeated five times., which is part of the for loop.

 

The idea here is, if we can visualize the translation, we should easily be able to include the scriptlets within our JSP. So, scriptlets are nothing but Java code segments. Any valid Java code segment. But remember that this can’t be anything like a method definition. We can’t include a method definition inside a scriptlet. Remember that this is something which is anything that goes inside the service method. It is going to be included within the service method.

 

declaration <%! java declaration %>

 

So far we have seen expressions, and scriptlets, both of these are affecting the translation of the service method. Other than the service method, within the translated class, we might like to define some methods, which we may like call from an expression. So, if we are interested in having something ourside the service method. As, we visualize the translation, we have the class being defined, for the JSP, When the class is defined, it has some regular methods like something equivalent of the init method, something equivalent of a destroy, those will be included.and it would that this something equivalent of a service.

 

In case of a JSP, as mentioned earlier, we something called a JspPage. In JspPage, we have a method called jspInit, we have a jspDestroy, those can be included, but then we have the translation, creating a method called _jspService. This is what the normal translation be, but we may to have some variables being defined, anything which can go inside a class definition, some variable declaration, static members, instance variables, though it is not advised to have instance variables. We might like to include some constants, methods, static methods, anything which cna go in a class. If we want to include any such thing, we can use in a JSP, we can include a scripting element called declaration.

 

In a JSP we can have declaration. A declaration is written as <%! any kind of valid member which can go in a class definition %>. This could a static variable declaration, an instance variable, a method, a nested class. We can include anything which can be a member of a class. The entire member has to be part of a single declaration. So, if it is a nested class, then put the entire class definition in a single declaration.

 

So, we have three kinds of primary scripting elements which can go in any JSP. One is expression, another is scriptlet and the third thing is the declaration. The expression and the scriptlet affect the service method not anything else, but, the declaration is outside of the service method.

 

Implicit Objects

 

Coming back to what kind of objects can be used inside the expression and the scriptlet. Do, we only use things like creating a new Date. What kind of objects are available in expressions and scriptlets.

 

Expressions, they are part of service method. Look at the service method, the service method has got two parameters, First parameter is going to be a ServletRequest, This is Http specific, so we will have an object of JttpServletRequest and the other parameter will be HttpServletResponse, When someone writes a Servlet, he gets a lots of objects in the service method, he gets the HttpServletRequest, HttpServletResponse, he can use the getServletContext, getServletConfig, and can get so many objects. What about a JSP. For the one who is writing a JSP, he would also need those objects. We may be interested in knowing what is available in a HttpSession object. Knowing if there are cookies, and so many other things in a JSP.

 

We understand that when the translation is done the method would have some parameters for the HttpServletRequsst and HttpServletResponse, what would be the parameter names. We have standardization on the variable name for these two, they are always going to be request and response respectively. This would mean that we can use these names directly in our expressions and scriptlets. Similarly there are other standard objects which are going to be available for use in the expressions and scriptlets. These objects are known as the implicit objects for the JSP.

 

These are available for use in expressions and scriptlets, because in the initial part of the translation of the _jspService method, before it starts the out.println and including code for the scriptlets, we have some fixed kind code, which declares these local variables. All these variables we call them as implicit objects. These have reference to various objects. Let us look at the list of these implicit objects.

 

The first is the request, another is the response,.

 

We commonly use a Writer, for output. In JSP also we have a Writer object being created for the JSP page. This Writer is of type JspWriter. This JspWriter extends from the Writer class. It does have the methods like print and println, similar to the PrintWriter. The variable name for this JspWriter is out.

 

We may also be interested in the configuration object (ServletConfig). It is possible for someone configure a JSP as a Servlet, giving it a servlet-name and giving it some init parameters. The ServletConfig object which is created for the servlet entry is available to us as an implicit object by using the name as config.

 

Similarly, if we are interested in the ServletContext object, that is also available as a variable called application.

 

If we are interested in the current page itself, then we have a variable called page. This page is nothing but current object. The data-type for page is Object, but it is nothing but reference to the current JspPage.

 

There a few other objects, but these objects are optional, they may not be available in all JSPs. They are conditional objects. A couple of such objects which are conditional, and their availablity is decided by page directives, which we will come across later.

 

If the JSP page is participating in a session, we have the current HttpSession object available to us as session.  So, we can use any attribute available in the current HttpSession object, by using this object.

 

We have another implicit object called exception. exception is something, whcih is not going to be available to all the JSP pages. It is available only in those pages which are designed as error pages.

 

JSPs are normally designed for output, We design JSPs for normal output. But sometimes when there is an error, we do have some pages which are JSP pages, which are designed to show errors. They are target for error. So, if we have any exception taking place in a service method of a JSP, we might like to forward to another JSP, where the targeted JSP, might like to have information about the exception which was thrown from the service method.. For this we need to build such JSPs as errorPage. How to create errorPage, would be seen, when we look at page directives.

 

So far, have the implicit objects called request, response, out, config, application, page, session and exception. All these objects are encapsulated in a simgle object called pageContext.. The life of a pageContext is within the JSP. The pageContext object is going to be more useful for developers of custom tags, which is covered in a later module. It is useful for passing all the other implicit objects within a simgle object.

 

Summary

 

So, we have seen the three basic kinds of scripting elements, namely the expression, scriptlets and the declarations.

 

In a JSP, we can include anything which can go in a HTML and we can embed expression for embedding any kind of Java expression, scriptlets for embedding Java code segments, these will be part of the service methods. Then we have declarations which are used for embedding other kinds of elements which can go in a java class definition.

 

We have more kinds of scripting elements, we have the directives, tags, there are standard tags and custom tags. And then we also something called expression-language. We will be covering all of these in our subsequent modules.

you can view video on Introduction to JSP

Suggested Reading:

  1. Core Servlets and Java Server Pages Volume 2 by Marty Hall & Larry Brown, Second Edition, Pearson Education.
  2. Core JSP by Damon Haugland and Aaron Tavistock, Pearson Education
  3. Java Server Programming for Professionals by Ivan Bayross, Sharanam Shah, Cynhthia Bayross and Vaishali Shah Shroff Publishers and Distributros.\
  4. http://docs.oracle.com/javaee/5/tutorial/doc/bnagx.html