Ex. No.
:4
DATE:
INSTALLATION OF APACHE TOMCAT WEB SERVER
Aim :
Installation of Apache Tomcat web server.
Procedure:
What is Apache Tomcat
It is an application server or web server or servlet container developed by the Apache
Software Foundation (ASF) and released under the Apache License version 2. HTTP web servers
provide an environment for Java code to run in. It includes tools for configuration and management,
but can also be configured by editing XML configuration files. Most of the modern Java web
frameworks are based on servlets and JavaServer Pages and can run on Apache Tomcat, for example
Struts, JavaServer Faces, Spring, etcetera.
Apache Tomcat7.0.XX new released
The Apache Tomcat team has released version 7.0.40 of Apache. They removed several fixes
that stop Tomcat attempting to parse text, improved handling and reporting if a
ConcurrentModificationException occurs while checking for memory leaks, etcetera.
How to Install Tomcat 7
There are certain steps we must follow for configuring Apache Tomcat 7.
Step 1
Download and Install Tomcat
1. Go to http://tomcat.apache.org/download-70.cgi then go to the Binary Distribution/Core/ and
download the "zip" package (for example "apache-tomcat-7.0.40.zip", about 8MB).
2. Now unzip the downloaded file into a directory of our choice. Don't unzip onto the dekstop
(since its path is hard to locate). I suggest using "e:\myserver". Tomcat will be unzipped into
the directory "e:\myserver\tomcat-7.0.40".
Step 2
Check the installed directory to ensure it contains the following sub-directories:
bin folder
logs folder
webapps folder
work folder
temp folder
conf folder
lib folder
Step 3
Now, we need to create an Environment Variable JAVA_HOME.
We need to create an environment variable called "JAVA_HOME" and set it to our JDK installed
directory.
1. To create the JAVA_HOME environment variable in Windows XP/Vista/7 we need to push
the "Start" button then select "Control Panel" / "System" / "Advanced system settings". Then
switch to the "Advanced" tab and select "Environment Variables" / "System Variables" then
select "New" (or "Edit" for modification). In "Variable Name", enter "JAVA_HOME". In
"Variable Value", enter your JDK installed directory (e.g., "c:\Program Files\Java\
jdk1.7.0_{xx}").
2. For ensuring that it is set correctly, we need to start a command shell (to refresh the
environment) and issue:
set JAVA_HOME
JAVA_HOME=c:\Program Files\Java\jdk1.7.0_{xx} <== Check that this is OUR JDK
installed directory
3. Sometimes we need to set JRE_HOME also. So for creating JRE_HOME we need to use the
same procedure. Push the "Start" buttonthen select "Control Panel" / "System" / "Advanced
system settings". Then switch to the "Advanced" tab and select "Environment Variables" /
"System Variables" then select "New" (or "Edit" for modification). In "Variable Name", enter
"JRE_HOME". In "Variable Value", enter your JRE installed directory (e.g., "C:\Program
Files\Java\jre7\").
Step 4
Configure Tomcat Server
The configuration files of the Apache Tomcat Server are located in the "conf" sub-directory of our
Tomcat installed directory, for example "E:\myserver\tomcat7.0.40\conf". There are 4 configuration
XML files:
1. context.xml file
2. tomcat-users.xml file
3. server.xml file
4. web.xml file
Before proceeding, make a BACKUP of the configuration files.
Step 4(a) "conf\web.xml"; Enabling a Directory Listing
Open the configuration file "web.xml". We shall enable the directory listing by changing "listings"
from "false" to "true" for the "default" servlet.
<param-value>true</param-value> like:
Step 4(b) "conf\server.xml file"; set the TCP Port Number
Open the file "server.xml" in a text editor.
The default port number of Tomcat is 8080. Now we need to change the TCP port number for
Tomcat, since the same port number can be used by other servers like SQL Server. We may choose
any number between 1024 and 65535. We shall choose 9999 in this article.
Locate the following lines, and change port="8080" to port="9999". Like:
<Connector port="9999" protocol="HTTP/1.1" Like
Step 4(c) "conf\context.xml"; Enabling Automatic Reload
In that we set reloadable="true" to the <Context> element to enable automatic reload after code
changes.
Add reloadable="true" as in the following:
<Context reloadable="true">
......
</Context> Like
Step 4(d) (Optional) "conf\tomcat-users.xml"
It is used to manage Tomcat by adding the highlighted lines, inside the <tomcat-users> elements.
In that we can add a password and username as an optional step.
Step 5
Now, start the tomcat server
Executable programs and scripts are kept in the "bin" sub-directory of the Tomcat installed directory,
e.g., "E:\myserver\tomcat7.0.40\bin".
Step 5(a) Start Server
Launch a command shell. Set the current directory to "<TOMCAT_HOME>\bin" like E:\myserver\
tomcat7.0.40\bin, and run "startup.bat" as follows:
After that a new Tomcat console window appears. Read the messages on the console. Look out for
the Tomcat's port number (double check that Tomcat is running on port 9999).......
We saw a figure like:
Step 5(b) Access the Server
Open a browser then enter the URL "http://localhost:9999" to access the Tomcat server's welcome
page.
If we get this type of page then it means we are done.
Now try the URL http://localhost:9999/examples to view JSP and servlet examples.
Step 5(c) How to Shutdown Server
We can stop the server using one of the following:
1. Press ctrl-c on the Tomcat console; or
2. Run "<TOMCAT_HOME>\bin\shutdown.bat" script:
// Change the current directory to Tomcat's "bin"
> e: // Change the current drive
e:\> cd E:\myserver\tomcat7.0.40\bin // Change Directory to YOUR Tomcat's "bin" directory
// Shutdown the server
E:\myserver\tomcat7.0.40\bin> shutdown
Result :
Thus the installation of Tom cat web server was completed successfully.
Ex. No.:5a
DATE:
INVOKE SERVLETS FROM HTML FORMS
Aim
To write a program in Java using servlets to invoke servlets from HTML forms.
Algorithm:
Step 1.Create a index page , which accepts user name, phone number, gender, address and age as
user input. Invoke the servlet page by submit button trigger.
Step 2.Create a servlet called Servlet3 which accepts the values given from form and prints them.
Step 3.Save the appropriate files in the following tomcat hierarchy for deployment
C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\[project-root-directory]\
index.html
C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\[project-root-directory]\
helloservlet.java
Create two new folders namely WEB-INF within [project-root-directory] folder which has the
web.xml and classes folder within WEB-INF which has .class of the java file
Step 4: Compile the servlet program and create servlet class file , save this class file
into (E:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0\webapps\sample\WEB-INF\
classes)classes folder.
Step5: Run the Tomcat server services, call the html file from the browser URL
http://localhost:8080/sample/index.html and configure web.xml for url link and servlet name ,
class link
Step 6: Call the servlet program from html and display the result
Step 7: Stop the services
Program
index.jsp
<html>
<head>
<title>Processing get requests with data</title>
</head>
<body>
<form action = "Servlet3" method = "get">
<b><p><label>Enter Your name Please!!
<br />
<input type = "text" name = "firstname" />
<input type = "submit" value = "Submit" />
</label></p></b>
</form>
</body>
</html>
Servlet3.java
//Servlet3.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet3 extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String firstName = request.getParameter( "firstname" );
response.setContentType( "text/html" );
PrintWriter out = response.getWriter();
// send XHTML document to client
// start XHTML document out.println( "<html>" );
// head section of document out.println( "<head>" );
out.println("<title>Processing get requests with data</title>" );
out.println( "</head>" );
// body section of document out.println( "<body>" );
out.println( "<h1>Hello " + firstName + ",<br />" );
out.println( "Welcome to Servlets!</h1>" );
out.println( "</body>" );
// end XHTML document
out.println( "</html>" );
out.close(); // close stream to complete the page
}
public String
getServletInfo() {
return "Short description";
}
}
Output
Result :
Thus the servlets are invoked from HTML forms successfully.
Ex. No.:5b
DATE:
SESSION TRACKING USING HIT COUNT
Aim
To write a program in java to implement session tracking in servlets using hit count.
.
Algorithm:
Step 1: Write a servlet program with HttpSession object to trcack the session count
Step 2: Compile the servlet program and create servlet class file , save this class file
into(E:\Program Files\Apache Software Foundation\Tomcat
7.0\webapps\sessioncount\WEB- INF\classes) folder.
Step3: Run the Tomcat server services, call theservlet program from the browser
URL (http://localhost:8080/sessioncount/SessionServlet)
and configure web.xml for url link and servlet name , class link
Step 4: Call the servlet program from browser and check the staus of session count
Step 5: Every time visit by client will get increment the session count.
Step 6: Stop the Services.
Program
SERVLET CODE:
GfgSession.java
// Import required java libraries
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class GfgSession extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws
ServletException, IOException
{
// Create a session object if it is already not
// created.
HttpSession session = request.getSession(true);
// Get session creation time.
Date createTime= new Date(session.getCreationTime());
// Get last access time of this web page.
Date lastAccessTime = new Date(session.getLastAccessedTime());
String title = "Welcome Back to Care Colleeg of Engineering ....";
Integer visitCount = new Integer(0);
String visitCountKey = new String("visitCount");
String userIDKey = new String("userID");
String userID = new String("GFG");
// Check if this is new comer on your web page.
if (session.isNew()) {
title = "Welcome to Care College of Engineering ....";
session.setAttribute(userIDKey, userID);
}
else {
visitCount = (Integer)session.getAttribute( visitCountKey);
visitCount = visitCount + 1;
userID= (String)session.getAttribute(userIDKey);
}
session.setAttribute(visitCountKey, visitCount);
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType= "<!doctype html public \"-//w3c//dtd html 4.0 "+"transitional//en\">\n";
out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n"+
"<body bgcolor = \"#f0f0f0\">\n"
+ "<h1 align = \"center\">" + title + "</h1>\n"
+ "<h2 align = \"center\">Gfg Session Information</h2>\n"
+ "<table border = \"1\" align = \"center\">\n"
+ "<tr bgcolor = \"#949494\">\n"
+ " <th>Session info</th><th>value</th>"
+ "</tr>\n"
+ "<tr>\n"
+ " <td>id</td>\n"
+ " <td>" + session.getId() + "</td>"
+ "</tr>\n" + "<tr>\n" + " <td>Creation Time</td>\n" + " <td>" + createTime + " </td>"
+ "</tr>\n" + "<tr>\n" + " <td>Time of Last Access</td>\n" + " <td>" + lastAccessTime +
"</td>"
+ "</tr>\n" + "<tr>\n" + " <td>User ID</td>\n" + " <td>" + userID + "</td>" + "</tr>\n" +
"<tr>\n"
+ " <td>Number of visits</td>\n" + " <td>" + visitCount + "</td>" + "</tr>\n" + "</table>\n" +
"</body>"
+ "</html>");
}
}
OUTPUT
Session Servlet Fist time run
Session Servlet Multiple time access
Result :
Thus the session tracking was completed successfully in servlets
ExNo:6 Creation of webpage for conducting online examination using JSP
DATE:
AIM:
To write a java servlet program to conduct online examination and to display the student mark list
available in a database.
ALGORITHM:
Step 1: Create a jsp file which consists of login page for online examination.
Step2: The login page consist of label box, text box and button
Step 3: Create servlet programs for online questions and answers
and displaying the results.
Step 4:The 5 question are defined into true or false model and close
the all tags.
Step 5: Import the necessary packages and declare class, class name in
exam.
Step 6: Declare the connection, statement and result set object.
Program:
index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome to Online Examination!!!!</title>
</head>
<body>
Welcome to Online Examination!!!!
<form action="exam" method="get">
<label><p> Enter Your name Please!!<br/> <input type="text" name="name"/>
<br/>
<input type="submit" name="SUBMIT"/>
</p></label>
</form>
</body>
</html>
Exam.java
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import
javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
public class exam extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
String name=request.getParameter("name");
out.println("<html>");
out.println("<head>");
out.println("<title>Online Examination</title>");
out.println("</head>");
out.println("<body bgcolor=PINK>");
out.println("<h2 align=center>Online Examination</h2><hr>");
out.println("<h3 align=center> Welcome Mr."+name+"</h3><hr>");
out.println("<h4><u>Terms and Conditions:</u></h4>");
out.println("<ul type=disc>");
out.println("<li>The Paper consists a set of five questions.</li>");
out.println("<li>Every question consists of two options.</li>");
out.println("<li>All must be answered</li></ul><hr>");
out.println("<center><h5><u>Your Questions</u></h5></center>");
out.println("<hr>");
out.println("<form method=get action=exam2>");
out.println("<p>1.Operating System is a </p>");
out.println("<input type=radio name=q1 value=0>Hardware");
out.println("<br>");
out.println("<input type=radio name=q1 value=1>Software");
out.println("<hr>");
out.println("<p>2.Developer of C Language is </p>");
out.println("<br>");
out.println("<input type=radio name=q2 value=0>Dennis Richee");
out.println("<br>");
out.println("<input type=radio name=q2 value=1>James Thompson");
out.println("<hr>");
out.println("<p>3.Which of the following is a multitasking,multiuser,multiprocessing ");
out.println("OS. </p>");
out.println("<br>");
out.println("<input type=radio name=q3 value=0>MS DOS");
out.println("<br>");
out.println("<input type=radio name=q3 value=1>Windows NT");
out.println("<hr>");
out.println("<p>4.Father of Computers is </p>");
out.println("<br>");
out.println("<input type=radio name=q4 value=0>Charles babbage");
out.println("<br>");
out.println("<input type=radio name=q4 value=1>Charles Dickson");
out.println("<hr>");
out.println("<p>5.What is the current generation of computers ?</p>");
out.println("<br>");
out.println("<input type=radio name=q5 value=0>Fifth");
out.println("<br>");
out.println("<input type=radio name=q5 value=1>Sixth");
out.println("<hr>");
out.println("<input type=submit value=Done>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
public String getServletInfo()
{
return "A Servlet of the user";
}
}
Exam2.java
import java.io.IOException; import java.io.PrintWriter;
import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import
javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public
class exam2 extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException
{
int count=0,j; response.setContentType("text/html"); PrintWriter out=response.getWriter();
String q1=request.getParameter("q1"); String q2=request.getParameter("q2");
String q3=request.getParameter("q3"); String q4=request.getParameter("q4");
String q5=request.getParameter("q5");
if(q1.equals("1"))
{
count=count+1;
}
if(q2.equals("0"))
{
count=count+1;
}
if(q3.equals("1"))
{
count=count+1;
}
if(q4.equals("0"))
{
count=count+1;
}
if(q5.equals("0"))
{
count=count+1;
}
out.println("<html>");
out.println("<head><title>Examination Results</title></head>"); out.println("<body>");
out.println("<h2 align=center<Online Examination</h2><hr>"); out.println("<h3>Number of
Questions answered correctly:</h3>"+count);if(count>=3)
{
out.println("<hr><h3>Congrats!!! You Have Passed!!!</h3><hr>"); out.println("<h4><b>Try Other
Tests!!</b></h4>");
}
else
{
out.println("<hr><h3>Sorry!!! You Have Failed!!!</h3><hr>"); out.println("<h4><b>Try
Again:</b></h4>");
}
out.println("</body>"); out.println("</html>");
}
public String getServletInfo() { return "A Servlet of the User";
}
}
OUTPUT :
Result :
Thus the program written in jsp and java are executed successfully for the online examination
EX NO 7
DATE: PROGRAMS USING XML – SCHEMA – XSL
Aim
To write a program in XML for displaying student information and searching user details .
Algorithm
Step 1 : Open netbeans IDE and create new project.
Step 2 : Create new file in xml and write the coding part for displaying student information
like Rnumber, Name and Marks.
Step 3 : Create another new file under xml project for searching the user details like name,
address etc.
Step 4 : Run the files individually and get the output in the web page.
Step 5 : Stop the program.
Program
Student Database
Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Student Database</title>
<style>
table {
width: 50%;
border-collapse: collapse;
margin: 20px 0;
}
th, td {
border: 1px solid #ccc;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<h1>Student Database</h1>
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Major</th>
</tr>
<tr>
<td>1</td>
<td>John Doe</td>
<td>20</td>
<td>Computer Science</td>
</tr>
<tr>
<td>2</td>
<td>Jane Smith</td>
<td>22</td>
<td>Mathematics</td>
</tr>
<tr>
<td>3</td>
<td>Emily Johnson</td>
<td>21</td>
<td>Biology</td>
</tr>
</table>
</body>
</html>
Student Database.xml
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student>
<id>1</id>
<name>John Doe</name>
<age>20</age>
<major>Computer Science</major>
</student>
<student>
<id>2</id>
<name>Jane Smith</name>
<age>22</age>
<major>Mathematics</major>
</student>
<student>
<id>3</id>
<name>Emily Johnson</name>
<age>21</age>
<major>Biology</major>
</student>
</students>
7 B. USER DETAILS
PROGRAM
Index.html
<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE>Searching for XML Elements </TITLE>
<SCRIPT>
function readXMLData()
{
var xmlDocumentObject, id , name , addr, phone, email;
xmlDocumentObject=new XMLHttpRequest();
xmlDocumentObject.open("GET","userlist.xml",false);
xmlDocumentObject.send();
xmlDocumentObject=xmlDocumentObject.responseXML;
id = xmlDocumentObject.getElementsByTagName("userid");
name = xmlDocumentObject.getElementsByTagName("username");
address = xmlDocumentObject.getElementsByTagName("address");
phone = xmlDocumentObject.getElementsByTagName("phone");
email = xmlDocumentObject.getElementsByTagName("email");
for (i = 0; i < id.length; i++)
{
output=id[i].firstChild.nodeValue;
if (output == document.getElementById("myText").value)
{displayDIV.innerHTML = id[i].firstChild.nodeValue + "<br> " + name[i].firstChild.nodeValue
+"<br> " +address[i].firstChild.nodeValue + "<br> " +
phone[i].firstChild.nodeValue+"<br>"+email[i].firstChild.nodeValue;
}}}
</SCRIPT>
</HEAD>
<BODY>
<H1>Search User</H1>
<input type="text" id="myText" value="">
<input type="BUTTON" VALUE="Get User Details" ONCLICK="readXMLData()">
<P>
<DIV ID="displayDIV"> </DIV>
</BODY>
</HTML>
Userlist.xml
<userlist>
<userid>usr01</userid>
<username>Sho</username>
<address>trichy</address>
<phone>9000000078</phone>
<email>[email protected]</email>
<userid>usr02</userid>
<username>Pooja</username>
<address>chennai</address>
<phone>9111111111</phone>
<email>[email protected]</email>
<userid>usr03</userid>
<username>sadhana</username>
<address>telugana</address>
<phone>9222222222</phone>
<email>[email protected]</email>
<userid>usr04</userid>
<username>sameera</username>
<address>bangalore</address>
<phone>9555555555</phone>
<email>[email protected]</email>
<userid>usr05</userid>
<username>naveena</username>
<address>Perambalur</address>
<phone>9666666666</phone>
<email>[email protected]</email>
</userlist>
OUTPUT: (7a)
OUTPUT(7B):
Result :
Thus the program written XML was executed successfully for student data base and searching the
user details.