The main difference between servlets and JSPs is that servlets are Java classes and JSPs are not (they are embedded in HTML pages). This difference also specifies where you want to use servlets and where you want to use JSPs: If you generate all of your html pages dynamically and will have complex logic, you will probably use servlets. If dynamic content is very low compared to the static content in your web page, then you will probably use JSPs.
So far what we have learnt is a request which is to be send to the server and depending on the request,the server processes the request and sends back results again to the receiver. Various web servers or servlet containers are available in market namely Apache Tomcat by Apache,IIS by Microsoft etc.In the following section a brief information regarding installation of Tomcat and its working principle is presented.Our upcoming articles will be based on Tomcat.
Tomcat - Installation & working
Tomcat,often referred to as the Tomcat server,is an open-source servlet container and JSP engine.The servlet API and JSP specification designers aim to create,as much as possible,new versions that are backward compatible. When we first start up the Tomcat server,it is operating in standalone mode.Tomcat has a built-in Web server that is used to handle the incoming request from our browser.Along with the Web server,the JSP engine is also started.However the servicing of the initial welcome page does not involve the JSP engine.The JSP page that we see in our browser is stored under the <Tomcat Installation Directory>/webapps/ROOT directory.
On a Windows system,the downloaded file(by selecting Open or by double-clicking the downloaded Jakarta-tomcat-5.0.28.exe file) is executed.This will start the wizard-based windows installer.The following things should be kept in mind during installation:
· When the installer asks to choose an installation location,the default folder should be c:\tomcat5 or a similar directory of users choice.
· When the installer asks to enter configuration options,enter the administration login username of tomcat and choose a password of users choice.
On a Windows system,one should see the Apache Process Runner running on the system tray with a green arrow indicating that Tomcat5 is running.To shut down Tomcat5 on a Windows system,right click the Apache Process Runner on one’s system tray and select Shutdown->Tomcat5.The Apache Process Runner will dissppear from the system tray.
To verify that the version of Tomcat is installed properly,try to start a browser and access the following URL:
http://localhost:8080/index.jsp
Having verified that Tomcat5 is correctly installed,it is time to try out some JSP coding.
<%@ taglib prefix=”tags” tagdir=”/WEB-INF/tags” %>
My First JSP 2.0 Template
All I want to say is
Current time is <%=new java.util.date() %>
<html>
<head>
<title>Presenting JSP2.0</title>
</head>
<body>
<h1>My First JSP 2.0 Template</h1>
</hr>
<p>All I want to say is <tags:helloWorld/></p>
<br>
Current time is <%=new java.util.date() %>
</body>
</html>
The above JSP makes use of a special custom tag that simply outputs the text”HelloWorld!”.Within the JSP body,which is essentially an HTML page here,the tag is embedded as follows:
<tags:helloWorld/>
Wherever this helloWorld tag is placed within the JSP,the JSP container will substitute the dynamically generated text “helloWorld!” in its place.
The rest of the above code contains html statements and tag.A static HTMl file with no dynamic element can be a JSP file. Thus JSP is rightly called a presentation technology.In fact it is possible to have professional Web page designers design the template.The page designer can work on the HTML portion of the template,while the JSP developer can work on the tags used and their placement.If the above sample JSP program is slightly edited & the following modifications are made
<p>I am so execited,I want to scream “tags:helloWorld/><tags:helloWorld/>
<tags:helloWorld/>”</p>
In this case the dynamically generated text is repeated.The following o/p is generated:
I am so excited. I want to scream “Hello, world!Hello,world!Hello,world!”
Because each request is independent,the JSP container is given a chance to determine whether the JSP has been modified since it was last accessed.If the JSP container detects that the JSP has been modified,the page is recompiled before being used to handle an incoming request.This makes the JSP development cycle quite straight forward;just modify the JSP file and the changes take effect immediately.
The above JSP makes use of a special custom tag that simply outputs the text “Hello World!” Custom tags are collected in a atg library,or taglib for short.To notify the jsp container of the location of the custom tag,a special JSP tag(called the taglib directive) needs to be included:
<%@ taglib prefix=”tags” prefix=”tags” tagdir=”/WEB-INF/tags” %>
In addition the preceding taglib directive also tells Tomcat that all of the tags in this library are prefixed by the word tags.The prefix that is added to the tag to distinguish which library it orginates from is often called the namespace.A detailed understanding of the above code is not possible unless we get acquainted with the elements that make up a JSP page.
The various elements that make up a JSP page are as follows:
· Directive element
· Template element
· Action
· Scripting elements
Detailed discussion on various elements that make a JSP page will be covered in next article. Until then precious comments on the aforesaid article are welcome.