Section 1 MCSL 054 Unit 1
Section 1 MCSL 054 Unit 1
Technologies Lab
Introduction
Objectives
Tomcat Installations and Setting
Running JSP Programs
Running Servlet Programs
XML Programming
List of Lab Assignments
Further Readings
Page Nos.
5
5
6
14
17
21
25
28
1.0 INTRODUCTION
In this section of lab course, you will have hands on experience of Advanced Internet
Technologies, which includes programming practices in Java Servlet and Java Server
Pages (JSP), web page security, and XML. This section is based on course MCS051:
Advanced Internet Technologies. A list of programming exercises (Lab Assignments)
is given at the end of this section. You will have to write programs to solve these
exercises and execute it during lab sessions.
Java Servlet technology and Java Server Pages (JSP) are server-side technologies.
They are now becoming the standard way to develop commercial web applications.
Java Technologies basic feature Servlet JSP bring the Write Once, Run Anywhere
paradigm to web applications. If, you apply effectively best practices, servlets and JSP
to help separate presentation from content, this will help in the understanding
applications and reduces complexity.
Before you begin programming/attempt programming, it is essential to learn how
container/web servers are to be installed to handle Servlet/JSP applications. In this
section, first you will learn about Jakarta Tomcat installation, then, about the
execution of Servlet and JSP programs.
The objective of XML design is to describe data and at the same time to focus on what
data is. You may not realise, but all that the XML does to create structures, store and
to send information. Now, it has started becoming evident that XML will be as
important to the future of Web Applications as HTML has been to the foundation of
Web Development. In this section, you will learn to create and view XML documents.
1.1 OBJECTIVES
After completing this lab section, you should be able to:
Lab Manual
Advanced Internet
Technologies Lab
Lab Manual
Now, start your server by double-clicking on shortcut to startup. You will find
window shown in Figure 5.
Advanced Internet
Technologies Lab
Now, your server is started. The next step is to, enter the URL http://localhost/ in your
browser and see whether you reach/are able to access the Tomcat welcome page or
not. Welcome page is shown in Figure 6.
If, you get an error message saying that the page could not be displayed or that the
server could not be found then, there is some problem in your installation and setting.
If this does not work, there are a couple of things to check:
Go through the error messages; it should help you find out the problem (e.g.,
JAVA_HOME not set properly or IIS already reserving port 80).
Lab Manual
Check whether the server appears to be running but you cannot access the home
page? There is a chance that your browser is using a proxy and you have not set
it to bypass proxies for local addresses. If this is the case then:
Advanced Internet
Technologies Lab
Tomcat check the modification dates of the class files of requested servlets, and reload
ones that have changed since they were loaded into the server's memory. If you dont
turn reloadable on for your development server, you have to restart the server every
time you recompile a servlet, which is already loaded into the server's memory.
Enabling the Invoker Servlet
As a beginner, you need to run your initial servlet programs without making any
change in your Web application's deployment descriptor file (in WEB-INF/web.xml
file). For this, you will have to get invoker servlet enabled. After this, you just drop
your servlet into
WEB-INF/classes and use the URL http://host/servlet/ServletName (or
http://host/webAppName /servlet/ServletName) once you start using your own Web
applications.
You have to, uncomment the following servlet and servlet-mapping elements in
install_dir/conf/web.xml as shown in Figure 10 and Figure 11, for enabling the
invoker servlet.Remember this is not Apache Tomcat-specific web.xml file which
goes in the WEB-INF directory of each Web application.
1.
<servlet>
<servlet-name>invoker</servlet-name>
<servlet-class>
org.apache.catalina.servlets.InvokerServlet
</servlet-class>
...
</servlet>
11
Lab Manual
2.
<servlet-mapping>
<servlet-name>invoker</servlet-name>
<url-pattern>/servlet/*</url-pattern>
</servlet-mapping>
Set CLASSPATH
Servlets and JSP are not part of the Java 2 platform, standard edition that is why the
compiler (i.e., javac) you use for development does not know about Servlet and JSP,
and you will have to identify the servlet classes to the compiler. So, you have to set
your CLASSPATH properly, and attempt to compile servlets, tag libraries, Web app
listeners, or other classes that use the servlet and JSP, otherwise, APIs will fail with
error messages saying unknown classes. You need to include both files in your
CLASSPATH for standard Tomcat Server:
12
C:\Tomcat5.5\apache-tomcat-5.5.15\common\lib\servlet-api.jar, and
C:\Tomcat5.5\apache-tomcat-5.5.15\common\lib\jsp-api.jar
If, you have created your own development directory then you also need to put that
directory in the CLASSPATH. It will be good (learning purpose), for you to write
simple package less servlets. After you gain enough experience you have to use
packages for your applications development.
Advanced Internet
Technologies Lab
Now, you are at the stage where you can test your own html, servlet, and jsp programs
in Tomcat Server. But, before you test your own programs, I will suggest the
execution of some examples of Servlet and JSP page given by Tomcat.
Figure 12: Link to Servlet and JSP Example on Tomcat Default Page
Now you can try Some Simple HTML and JSP Page
First, verify that the server is running. If, the server is running, you have to make sure
that you can install and access simple HTML and JSP:
If, you can access an HTML page successfully, it will help you ensure that
HTML and JSP files are loaded in the right/correct directories, and the URLs
correspond make sure for you that in which directories,
If, you are able to successfully access a new JSP page then, it shows that you
have configured the Java compiler properly.
13
Lab Manual
</head>
Advanced Internet
Technologies Lab
<body>
<%
%>
<% out.println(Welcome to first JSP page); %>
<BR>
<%
out.println(Currently the day,date,and time is);
out.println( date );
%>
</body>
</html>
Save this Test.jsp file in C:\Tomcat5.5\apache-tomcat-5.5.15\webapps\ROOT
directory and access it with URL http://localhost/Test.jsp, you will get Web Page
shown in Figure 15a.
Now, let us see an example in which a java class is used for counting the number of
times this JSP is accessed.
Java Class File - Counter.java
public class Counter
15
Lab Manual
{
private static int counter;
public static int getCounter()
{
counter++;
return counter;
}
}
JSP File Cont.jsp
<html>
<body>
The page is:
<% out.println(Counter.getCounter());
%>
</body>
</html>
You have to compile Counter.java file and save Counter.class file in
C:\Tomcat5.5\apache-tomcat-5.5.15\webapps\ROOT\WEB-INF\classes directory.
Save Count.jsp in C:\Tomcat5.5\apache-tomcat-5.5.15\webapps\ROOT directory.Start
server and access URL : http://localhost/Count.jsp trough Internet Explorer , you
will get following screen :
16
The number of times you will access through URL : : http://localhost/Count.jsp will
increase by one The page is: 1, The page is: 2 etc.
Advanced Internet
Technologies Lab
17
Lab Manual
18
Save this HTML code in FirstServlet.html file. Now, you have an HTML code for
invoking TesrServlet program. Start your Tomcat and invoke the URL
C:\MyJava\FirstServlet.html. You will find following the form as the output asking
for your name. Enter a name into textfield and press Submit Query button, you will go
to URL http://localhost/servlet/TestServlet and you will find web page shown in
Figure 19a.
Advanced Internet
Technologies Lab
If, you are developing servlet applications which uses Packages and Utilities then, you
have to be more careful in terms of putting class files in appropriate directories both
during development and when deployed to the server. For example, if, you have a
package named myServlet then, compiling your application.java, put application.class
in install_dir/webapps/ROOT/WEB-INF/classes/myServlet. Once you have placed the
.class file of servlet in the proper directory, access it with the URL
http://localhost/servlet/myServlet.application.
Now, let us see one more servlet program. This program is for counting the number of
times this application is accessed. Here, we will use session tracing for this counting.
Servlet File AccessCount.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
19
Lab Manual
import java.net.*;
import java.util.*;
/** An example of session tracking. */
public class AccessCount extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType(text/html);
PrintWriter out = response.getWriter();
String title = Access Count Example By Session Tracking ;
String heading ;
//Creating HTTP Session
HttpSession session = request.getSession(true);
Integer Count = (Integer)session.getAttribute(Count);
if (Count == null) {
Count = new Integer(0);
heading = Welcome,for Fist time visit ;
}
else {
heading = Welcome Again;
Count = new Integer(Count.intValue() + 1);
}
session.setAttribute("Count", Count);
out.println(<HTML><HEAD><TITLE> Counting by Session
Tracing</TITLE></HEAD>");
out.println(<BODY BGCOLOR=GREEN>\n +
<H1 ALIGN=\"CENTER\>" + heading + </H1>\n +
<H2>Information on Your Session:</H2>\n +
<TABLE BORDER=1 ALIGN=\CENTER\>\n +
<TR BGCOLOR=RED>\n +
<TH>Information Type<TH>Value\n +
<TR>\n +
<TD>ID\n +
<TD> + session.getId() + \n +
<TR>\n +
<TD>Session Created at:\n +
<TD> +
new Date(session.getCreationTime()) + \n +
<TR>\n +
<TD>Last Time of Access at: \n +
<TD> +
new Date(session.getLastAccessedTime()) + \n +
<TR>\n +
<TD>Number of Previous Accesses:\n +
<TD> + Count + \n +
</TABLE>\n +
</BODY></HTML>);
}
} Compile AccessCount.java file and save AccessCoun.class file in
C:\Tomcat5.5\apache-tomcat-5.5.15\webapps\ROOT\WEB-INF\classes directory.
Now aaccess URL http://localhost/servlet/AccessCount. For the first access of
AccessCount page you will get the screen given in Figure 19b.
20
Advanced Internet
Technologies Lab
Lab Manual
Some Concepts
While writing XML documents take care of the following. Though it is discussed in
the course MCS-051, it will be very useful while writing XML documents:
i)
Similar to HTML, start tags are angle bracket enclosed, and end tags are angle
bracket enclosed with a prepended forward slash.
ii) An entity started by a start tag and ended by an end tag is called an element.
iii) Elements can contain other elements. In our example, the Student element
contains two BCA_Student elements.
iv) An element can contain a mix of different elements.
22
v) An XML file must contain exactly one element at the top level. In our example,
the Student is the top level.
Advanced Internet
Technologies Lab
As you know XML, allows the programmer/author to define their own tags and own
document structure.
While defining your own tag you should take care of that:
For example With XML, the tag <myLetter> is different from the tag
<myletter>.
Opening and closing tags must therefore, be written with the same case:
For example, i is incorrect and ii is correct.
Lab Manual
If you see Test.xml file using Internet Explorer you will find following screen:
XML Parsers
You need parsers to break up your document into its component pieces and make
them accessible to other parts of a program. Some parsers check for well-formedness,
24
and some check for document validity. However, if you just want to check that your
XML is both well formed and valid, all you need is an XML validator.
Advanced Internet
Technologies Lab
SAX: The Simple API for XMLSAX is a well-known parser written by David
Megginson, et al. (http://www.megginson.com/SAX/index.html)
Exercise 2.
Session 2:
Exercise 3.
Write a servlet program that takes your name and address from an
HTML Form and displays it.
25
Lab Manual
Exercise 4.
Write a servlet program that displays current date and time. Your servlet
should also indicate the number of times it has been assessed since it
has been loaded.
Exercise 5.
Session 3:
Exercise 6.
Exercise 7.
Write a program, using servlet and JDBC which takes students roll
number and provides student information, which includes the name of
the student, the address, email-id, program of study, and year of
admission. You have to use a database to store students information.
Exercise 8.
Session 4:
Exercise 9.
Exercise 10. Write a JSP program to output, "Welcome to JSP world. The time now
is: system current time. Use a scriptlet for the complete string, including
the HTML tags.
Exercise 11. write a JSP page that display a randomly generated number in first visit
to this page and repeat displaying this same number in subsequent
visits.
Session 5:
Exercise 12. Write a JSP page to output the values returned by System.getProperty for
various system properties such as java.version, java.home, os.name,
user.name, user.home, user.dir etc. Each property should be displayed
in a separate row.
Exercise 13. Write a JSP page to use either <jsp:include> or <jsp:forward> depending
upon the value of a Boolean variable.
Exercise 14. Write a JSP page using <jsp:forward> to go to a servlet program which
display your name, date of birth and address.
Session 6:
Exercise 15. Create an HTML form to take customer information (Name, Address,
Mobile No.). Write a JSP program to validate this information of
customers.
Exercise 16. Write a JSP program using <jsp:include> to include the program
written in Exercise 9.
26
th
Exercise 17. Create a database of students who are in the 5 Semester of the MCA
program with you at your study center. Write a program using JSP and
JDBC to display the name and address of those students who are born
after 1985.
Advanced Internet
Technologies Lab
Session 7:
Exercise 18. Write a JSP program which display a web page containing your personal
information such as: Name, Date of Birth, Sex, Area of Interest,
Specialisation and a paragraph explaining what you want to be in the
next five years.
Exercise 19. Develop an application that collects/maintains the product information of
an electronics goods production company, in a database. Write a JSP
page to retrieve (to display) selective this information in database on
demand. Make necessary assumptions to develop this application.
Session 8:
Exercise 20.The following example is a note to Ramu from Deenu, stored in XML:
<note>
<to> Ramu </to>
<from>Deenu</from>
<heading>Festival Wishes</heading>
<body>May God give you all the happiness</body>
</note>
Extend this document by adding some more information in this document.
Exercise 21. Imagine that this is the description of a book:
My First Servlet
Introduction to Servlet
What is Servlet
What are the advantages of Servlet
Servlet Initialisation
Servlet Realoading
Destroying a Servlet
27
Lab Manual
Visual Basic
Programming
B.Tech
Mohan
Naveen
Java
Programming
M.Tech
Robert
John
Sudhansh
ASP
Programming
MSc
Neeta
Ravi
1.7
28
FURTHER READINGS
1.
Head First Servlet and JSP By Bryan Basham, Kathy Sierra, and Bert Bates,
O. Reilly publication First Edition Eighth India Reprint- 2006.
2.
http://www.w3schools.com/xml/default.asp
3.
http://www.w3.org/XML/
4.
http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Servlets.html
5.
http://tomcat.apache.org/
6.
http://www.coreservlets.com/Apache-Tomcat-Tutorial/