Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views44 pages

Web Technologies Lab Manual Web Technologies Lab Manual

Uploaded by

sshamu46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views44 pages

Web Technologies Lab Manual Web Technologies Lab Manual

Uploaded by

sshamu46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

lOMoARcPSD|37890669

Web technologies lab manual

Web Technology (Anna University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by SHEIK MOHAMED ([email protected])
lOMoARcPSD|37890669

(Approved by AICTE and Affiliated to Anna University, Chennai)


27, Thayanur, Trichy - 620009
Department of Computer Science and Engineering
CCS375 -Web Technologies Laboratory
Lab Manual

Class : III year


Semester : V Semester
Subject : CCS375 /Web Technologies Laboratory
Academic Year: 2023-2024
Regulation : R21

Name Signature
Prepared By Ms.M.Lakshana/AP
Verified By Dr.J.Suresh,Hod/CSE
Approved BY Dr.s.Shanthi, Principal

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

CCS375 WEB TECHNOLOGIES LABORATORY

COURSE OBJECTIVES:
 To understand different Internet Technologies
 To learn java-specific web services architecture
 To Develop web applications using frameworks

LIST OF EXPERIMENTS

1. Create a web page with the following using HTML.


 To embed an image map in a web page.
 To fix the hot spots.
 Show all the related information when the hot spots are clicked.
2. Create a web page with all types of Cascading style sheets.
3. Client Side Scripts for Validating Web Form Controls using DHTML.
4. Installation of Apache Tomcat web server.
5. Write programs in Java using Servlets:
 To invoke servlets from HTML forms.
 Session Tracking.
6. Write programs in Java to create three-tier applications using JSP and Databases
 For conducting on-line examination.
 For displaying student mark list. Assume that student information is available in a database
which has been stored in a database server.
7. Programs using XML – Schema – XSLT/XSL.

COURSE OUTCOMES:
CO1: Construct a basic website using HTML and Cascading Style Sheets
CO2: Build dynamic web page with validation using Java Script objects and by applying different
event handling mechanisms
CO3: Develop server side programs using Servlets and JSP.
CO4: Construct simple web pages in PHP and to represent data in XML format.
CO5: Develop interactive web applications.

Ex.No:1 Create a web page with the following using HTML

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

 To embed a map in a web page


 To fix the hot spots in that map
 Show all the related information when the hot spots are clicked.
AIM
To create a web page with image map to fix the hotspots and show its related information
Algorithm:
1. Create a html program for insert the India Map
2. Fix the hotspots for the districts
3. The related information for the hotspots should be viewed when the district to be clicked.
CODE: main.html
<html>
<head>
<BODY bgcolor="GREEN">
<img src ="indiamap.jpg" usemap="#indiamap" />
<map name=indiamap>
<area shape="circle" coords="476,1453,1452" href="TN.html" alt="Tamilnadu">
</map>
</head>
</BODY>
</html>

TN.Html
<html>
<head><title>Tamil Nadu - India</title></head>
<body bgcolor="palegreen">
<h1><center>Tamil Nadu</center></h1>
<h3>is one of the 29 states of India. Its capital and largest city is Chennai.
Tamil Nadu lies in the southernmost part of the Indian Peninsula and
is bordered by the States of puducherry, Kerala, Karnataka, Andha Pradesh.
</h3>
<h3>
<ul>
<li>Districts<i> - 33</i>
<li>Capital City<i> - Chennai</i>
<li>Largest City<i> - Chennai</i>
<li>Governor<i> - R.N.RAVI</i>
<li>Chief Minister<i> -M.K.STALIN </i>
<li>Population<i> - 79.4MILLION </i>
<li>Tourist spots<i> - Mamallapuran, Ooty, Kodaikanal, Marina,
Mudurai Meenakshi Amman Temple, Thanjavur etc.,</i>
</ul>
</body>
</html>

OUTPUT:

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

Result:

Thus the web page with image map to fix the hotspots has been executed
Ex.No:2 Create a web page with all types of Cascading style sheets.

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

 Cascading Style sheets


 Embedded Style Sheets
 Inline Style sheets. Use our college information for the web pages
AIM
To create a webpage with all types of Cascading Style Sheets.
ALGORITHM
Internal CSS:
STEP 1: Create a HTML program with <style> tag.
STEP 2: Inside the <style> tag, specify the format required for that web page.
STEP 3: Run the program with a web browser.
External CSS:
STEP 4: Open a notepad, type the needed CSS in it and save it with .css extension.
STEP5: Refer this .css file in the HTML using the tag <link>.
STEP 6: Run the program with a web browser
Code:Cascading Stylesheets
<!DOCTYPE html>
<html>
<head>
<style>
body {
background-color: lightblue;
}

h1 {
color: white;
text-align: center;
}
p{
font-family: verdana;
font-size: 20px;
}
</style>
</head>
<body>
<h1>3rd year cse</h1>
<p>Welcome guys</p>
</body>
</html>

Code: Inline and Embedded Style sheets

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

<html>
<head>
<title>EMBEDDED&INLINE</title>
<!-- Embed Style Sheet-->
<style type="text/css">
p{
background-color: yellow;
text-align: justify;
margin: 7px;
}
</style>
</head>
<body id="body">
<h1>CARE</h1>
<p>
<span style="font: 200 x-large fantasy">CARE GROUP OF INSTITUTIONS</span>
Approved by AICTE | Affiliated to Anna University
</p>
<!-- Inline Sytle Sheet-->
<table style="background-position: center;text-align: center;padding: 3px;">
<tr>
<td align="left">
<div class="div">
<ul>
<li><a href="ENGG.HTML">ENGG</a></li>
<li><a href="ARCH.HTML">ARCH</a></li>
<li><a href="ARTS&SCIENCE">A&S</a></li>
<li><a href="SCHOOL">SCHOOL</a></li>
</ul>
</div>
</td>
</tr>
</table>
</body>
</html>

Result:

Thus the embedding, Cascading and inline sheets have been executed.

Ex.No:3 Client Side Scripts for Validating Web Form Controls using DHTML

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

AIM
To create a Client Side Scripts for Validating Web Form Controls using DHTML
FACILITIES REQUIRED AND PROCEDURE
Facilities Required:
S.No Facilities required Quantit
. y
1 System 1
2 O/S Windows / Linux OS
3 S/W name Any Browser

Procedure:
Step no. Details of the
step
1 The form will include one text field called " Name", and a submit buton.
2 Validation script will ensure that the user enters their name before the
form is
sent to the server.
3 Open this page to see it in action.
4 Try pressing the Send Details button without filling anything in the
"Name”
field.
5 We might like to open the source code for this form in the separate
window.
6 The page consists of a JavaScript function called validate_form()
that per-
forms the form validation, followed by the form itself.

Program:
<html>
<head>
<title>A Simple Form with JavaScript Validation</title>
<script type="text/javascript">
<!--
function validate_form ( )
{
valid = true;
if ( document.contact_form.contact_name.value == "" )
{
alert ( "Please fill in the 'Your Name' box." );
valid = false;
}
return valid;
}
//-->
</script>
</head>
<body bgcolor=”#FFFFFF”>
<form name="contact_form" method="post"

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

action="/cgi-bin/articles/development/javascript/form-validation-with-javascript/contact_simple.cgi"
onSubmit="return validate_form ( );">
<h1>Please Enter Your Name</h1>
<p>Your Name: <input type="text" name="contact_name"></p>
<p><input type="submit" name="send" value="Send Details"></p>
</form>
</body>
</html>
a)Output:

Result:
 Thus to create a Client Side Scripts for Validating Web Form Controls using DHTML
 was successfully executed and verified

Ex.No:4 Install TOMCAT web server. Convert the static web pages of programs into dynamic web
pages using servlets (or JSP) and cookies. Hint: Users information (user id, password, credit card
number) would be stored in web.xml. Each user should have a separate Shopping Cart.

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

Apache Tomcat HTTP Server


Apache Tomcat is a Java-capable HTTP server, which could execute special Java programs known as
"Java Servlet" and "Java Server Pages (JSP)". Tomcat is an open-source project, under the "Apache
Software Foundation" (which also provides the most use, open-source, industrial-strength Apache HTTP
Server). The mother site for Tomcat is http://tomcat.apache.org. Alternatively, you can find tomcat via the
Apache mother site @ http://www.apache.org.Tomcat was originally written by James Duncan Davison
(then working in Sun) in 1998,based on an earlier Sun's server called Java Web Server (JWS). It began at
version 3.0 afterJWS 2.1 it replaced. Sun subsequently made Tomcat open-source and gave it to Apache.
The various Tomcat releases are:
Tomcat 3.0 (1999): Reference Implementation (RI) for Servlet 2.2 and JSP 1.1.
Tomcat 4.1 (Sep 2002): RI for Servlet 2.3 and JSP 1.2.
Tomcat 5.0 (Dec 2003): RI for Servlet 2.4 and JSP 2.0.
Tomcat 6.0 (Feb 2007): RI for Servlet 2.5 and JSP 2.1.
Tomcat 7.0 (Jan 2011): RI for Servlet 3.0, JSP 2.2 and EL 2.2.
Tomcat 8.0 (Jun 2014): RI for Servlet 3.1, JSP 2.3, EL 3.0 and WebSocket 1.0. Tomcat 8.5
(June 2016) supports HTTP/2, OpenSSL, TLS virtual hosting and JASPIC 1.1.
Tomcat 9.0 (Jan 2018): RI for Servlet 4.0, JSP 2.3, EL 3.0, WebSocket 1.0, JASPIC 1.1.
Tomcat 10.0 (???):
How to Install Tomcat and Get Started with Java Servlet Programming
2.1 STEP 0: Create a Directory to Keep all your Works
I shall assume that you have created a directory called "c:\myWebProject" (for Windows) or"~\
myWebProject" (for Mac OS X) in your earlier exercises. Do it otherwise. This step is important;
otherwise, you will be out-of-sync with this article and will not be able to find your files later.
2.2 STEP 1: Download and Install Tomcat
For Windows
Goto http://tomcat.apache.org ⇒ Under "Tomcat 9.0.{xx} Released", where {xx} is the latest update
number ⇒ Click "Download" ⇒ Under "9.0.{xx}" ⇒ Binary Distributions ⇒ Core ⇒"zip" (e.g., "apache-
tomcat-9.0.{xx}.zip", about 11 MB).UNZIP the downloaded file into your project directory "c:\
myWebProject". Tomcat shall be unzipped into directory "c:\myWebProject\apache-tomcat-9.0.{xx}".For
EASE OF USE, we shall shorten and rename this directory to "c:\myWebProject\tomcat".Take note of
Your Tomcat Installed Directory. Hereafter, I shall refer to the Tomcat installed
directory as <TOMCAT_HOME>.
Tomcat's Sub-Directories
Take a quick look at the Tomcat installed directory. It contains the these sub-directories:bin: contains the
binaries and scripts (e.g., startup.bat and shutdown.bat for Windows;startup.sh and shutdown.sh for
Unixes and Mac OS X).conf: contains the system-wide configuration files, such as server.xml, web.xml,
and context.xml.webapps: contains the webapps to be deployed. You can also place the WAR (Webapp
Archive) file for deployment here.lib: contains the Tomcat's system-wide library JAR files, accessible by
all webapps. You could also place external JAR file (such as MySQL JDBC Driver) here.
logs: contains Tomcat's log files. You may need to check for error messages here. work: Tomcat's working
directory used by JSP, for JSP-to-Servlet conversion.
STEP 2: Create an Environment Variable JAVA_HOME
(For Windows)
You need to create an environment variable (system variable available to all applications)called
"JAVA_HOME", and set it to your JDK installed directory.Many Java applications (such as Tomcat)
require the environment variable JAVA_HOME to
be set to the JDK installed directory.To set the JAVA_HOME environment variable:First, find your JDK
installed directory. For JDK 11, the default is "c:\ProgramFiles\Java\jdk-11.0.{x}", where "{x} is the
update number. Use your "File Explorer" to find this directory and take note of your update number
{x}.Check if JAVA_HOME is already set. Start a CMD and issue:
. If not, proceed to the next step.
To set the environment variable JAVA_HOME in Windows 10:
Launch "Control Panel" ⇒ (Optional) "System and Security" ⇒ "System" ⇒ Click
"Advanced system settings" on the left pane.
Switch to "Advanced" tab ⇒ Click "Environment Variables"

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

Under "System Variables" (the bottom pane) ⇒ Click "New" (or Look for "JAVA_HOME"
and "Edit" if it is already set) ⇒ In "Variable Name", enter "JAVA_HOME" ⇒ In "Variable
Value", enter your JDK installed directory you noted in Step 1. (In the latest Windows 10:
you can push the "Browse Directory" button and navigate to the JDK installed directory to
avoid typo error.)To verify, RE-START a CMD (restart is needed to refresh the environment variables)
and
issue:set JAVA_HOME
JAVA_HOME=c:\Program Files\Java\jdk-11.0.{x} <== Verify that this is YOUR JDK installed directory
Notes: Windows' environment variables (such as JAVA_HOME, PATH) are NOT case sensitive.
STEP 3: Configure the Tomcat Server
The Tomcat configuration files, in XML format, are located in the "conf" sub-directory of your Tomcat
installed directory, e.g. "c:\myWebProject\tomcat\conf" (for Windows) or "~/myWebProject/tomcat/conf"
(for Mac OS X). The important configuration files are:
server.xml
web.xml
context.xml
Make a BACKUP of the configuration files before you proceed!!!
Step 3(a) "conf\server.xml" - Set the TCP Port Number
Use a programming text editor (e.g., Sublime Text, Atom) to open the configuration file "server.xml".The
default TCP port number configured in Tomcat is 8080, you may choose any number between 1024 and
65535, which is not used by existing applications. We shall choose 9999 in this article. (For production
server, you should use port 80, which is pre-assigned to HTTP server as the default port number.)Locate
the following lines (around Line 69) that define the HTTP connector, and change port="8080" to
port="9999".<!-- A "Connector" represents an endpoint by which requests are received and responses are
returned. Documentation at :Java HTTP Connector: /docs/config/http.htmlJava AJP Connector:
/docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.htmlDefine a non-SSL HTTP/1.1 Connector on port 8080-->
<Connector port="9999" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" />
Step 3(b) "conf\web.xml" - Enable Directory Listing
Again, use a programming text editor to open the configuration file "web.xml".We shall enable directory
listing by changing "listings" from "false" to "true" for the "default" servlet. This is handy for test system,
but not for production system for security.Locate the following lines (around Line 108) that define the
"default" servlet; and change the "listings" from "false" to "true".
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Step 3(c) "conf\context.xml" - Enabling Automatic Reload
We shall add the attribute reloadable="true" to the <Context> element to enable automatic reload after
code changes. Again, this is handy for test system but not recommended for production, due to the
overhead of detecting changes. Locate the <Context> start element (around Line 19), and change it to
<Context reloadable="true">.
<Context reloadable="true">
......
......
</Context>

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

2.5 STEP 4: Start Tomcat Server


The Tomcat's executable programs and scripts are kept in the "bin" sub-directory of the Tomcat installed
directory.
Step 4(a) Start Server
For Windows
I shall assume that Tomcat is installed in "c:\myWebProject\tomcat". Launch a CMD shell and issue:
c: // Change drive
cd \myWebProject\tomcat\bin // Change directory to your Tomcat's binary directory
startup // Run startup.bat to start tomcat server
Step 4(b) Start a Client to Access the Server
Start a browser (Firefox, Chrome) as an HTTP client. Issue URL "http://localhost:9999" to access the
Tomcat server's welcome page. The hostname "localhost" (with IP address of 127.0.0.1) is meant for local
loop-back testing within the same machine. For users on the other machines over the net, they have to use
the server's IP address or DNS domain name in
the form of "http://serverHostnameOrIPAddress:9999".
(Optional) Try issuing URL http://localhost:9999/examples to view the servlet and JSP examples. Try
running some of the servlet examples.

Step 4(c) Shutdown Server


For Windows
You can shutdown the tomcat server by either:Press Ctrl-C on the Tomcat console; OR Run
TOMCAT_HOME>\bin\shutdown.bat" script. Open a new "cmd" and issue:c:// Change the current drive
d \myWebProject\tomcat\bin// Change directory to your Tomcat's binary directory
shutdown// Run shutdown.bat to shutdown the server

To Convert Static web pages into Dynamic web pages


Aim:
To Convert Static web pages into Dynamic web pages using html and java
Program:
Index.html
<html xmlns="http://www.w3.org/1999/xhtml">
<body bgcolor="pink">
<br /><br /><br /><br /><br />
<h1 align="center"><U>ONLINE BOOK STORAGE</U></h1><br /><br /><br />
<h2 align="center"><pre>
<b>Welcome to online book storage.
Press LOGIN if you are having id otherwise press REGISTRATION
</b></pre></h2>
<br /><br /><pre>
<div align="center"><a href="Login.html">LOGIN</a> <a href="reg.html">
REGISTRATION</a></div></pre>
</body></html>
Login.html
<html>
<body bgcolor="blue"><br /><br /><br />
<form name="myform" method="post" action="login">
<div align="center"><pre>
LOGIN ID :<input type="text" name="i alue="clear" />

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

</div></form></body></html>
Profile.html
<html xmlns="http://www.w3.org/1999/xhtml">
<body bgcolor="yellow"><br /><br /><br />
<form name="myform" method="post" action="profile">
<div align="center"><pre>
LOGIN ID :<input type="text" name="id" /><br />
</pre><br /><br />
</div>
<br /><br />
<div align="center">
<input type="submit" value="ok" onclick="validate()" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="clear" />
</div></form></body></html>
Reg.html
<html xmlns="http://www.w3.org/1999/xhtml">
<body bgcolor="red"><br /><br />
<form name="myform" method="post" action="reg">
<div align="center"><pre>
NAME :<input type="text" name="name" /><br />
ADDRESS :<input type="text" name="addr" /><br />
CONTACT NUMBER :<input type="text" name="phno" /><br />
LOGINID :<input type="text" name="id" /><br />
PASSWORD :<input type="password" name="pwd" /></pre><br /><br />
</div>
<br /><br />
<div align="center">
<input type="submit" value="ok" onclick="validate()" />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="clear" />
</div></form></body></html>
Catlog.html
<html xmlns="http://www.w3.org/1999/xhtml">

<body bgcolor="white"><br /><br /><br />


<form method="post" action="catalog">
<div align="center"><pre>
BOOK TITLE :<input type="text" name="title" /><br />
</pre><br /><br />
</div>
<br /><br />
<div align="center">
<input type="submit" value="ok"
name="button1"/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="reset" value="clear" name="button2"/>
</div></form></body></html>
Order.html
<html xmlns="http://www.w3.org/1999/xhtml">
<body bgcolor="green"><br /><br />
<form method="post" action="reg">
<div align="center"><pre>
NAME :<input type="text" name="name" /><br />
PASSWORD :<input type="password" name="pwd" />
TITLE :<input type="text" name="title" /><br />
NO. OF BOOKS :<input type="text" name="no" /><br />
DATE :<input type="text" name="date" /><br />

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

CREDIT CARD NUMBER:<input type="password" name="cno" /><br /></pre><br /><br


/>
</div>
<br /><br />
<div align="center">
<input type="submit" value="ok" name="button1"/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="reset" value="clear"
name="button2"/>
</div></form></body></html>
Login.java
import java.io.IOException;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class login extends HttpServlet
{
@Override
public void service(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException
{ PrintWriter pw=resp.getWriter();
pw.println("<html><body bgcolor=green>");
String id=req.getParameter("id");

String pwd=req.getParameter("pwd");
try
{
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection
con=DriverManager.getConnection("jdbc:derby://localhost:1527/OnlineBookStorage","root",
"root");
Statement stmt=con.createStatement();
String sqlstmt="select id,password from login";
ResultSet rs=stmt.executeQuery(sqlstmt);
int flag=0;
while(rs.next())
{
if(id == null ? rs.getString("name") == null : id.equals(rs.getString("name")))
{
flag=1;
}
if(flag==0)
{
pw.println("SORRY INVALID ID TRY AGAIN ID<br><br>");
pw.println("<a href=\"login.html\">press LOGIN to RETRY</a>");
}
else
{

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

pw.println("VALID LOGIN ID<br><br>");


pw.println("<h3><ul>");
pw.println("<li><ahref=\"profile.html\"><fontcolor=\"black\">USER PROFILE</font>
</a></li><br><br>");
pw.println("<li><ahref=\"catalog.html\"><fontcolor=\"black\">BOOKS
CATALOG</font></a></li><br><br>");
pw.println("<li><ahref=\"order.html\"><fontcolor=\"black\">ORDER
CONFIRMATION</font> </a></li><br><br>");
}
pw.println("</body></html>");
}}
catch(ClassNotFoundException | SQLException e)
{ resp.sendError(500,e.toString());
}
}
}
Reg.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class reg extends HttpServlet


{
public void service(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException
{
PrintWriter pw=resp.getWriter();
pw.println("<html><body bgcolor=pink>");
String name=req.getParameter("name");
String addr=req.getParameter("addr");
String phno=req.getParameter("phno");
String id=req.getParameter("id");
String pwd=req.getParameter("pwd");
int no=Integer.parseInt(phno);
try
{
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection
con=DriverManager.getConnection("jdbc:derby://localhost:1527/OnlineBookStorage","root",
"root");
Statement stmt=con.createStatement();
String sqlstmt="select id,password from login";
ResultSet rs=stmt.executeQuery(sqlstmt);
int flag=0;
while(rs.next())
{
if(rs.getString("name") == null ? id == null : rs.getString("name").equals(id))

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

{
pw.println("SORRY INVALID ID ALREADY EXITS TRY AGAIN WITH NEW
ID<br><br>");
pw.println("<a href=\"/tr/reg.html\">press REGISTER to RETRY</a>");
}
else
{ Statement stmt1=con.createStatement();
stmt1.executeUpdate("insert into login
values("+name+","+addr+","+no+","+id+","+pwd+")");
pw.println("YOUR DETAILS ARE ENTERED<br><br>");
pw.println("<a href=\"login.html\">press LOGIN to login</a>");
}
pw.println("</body></html>");
}
}
catch(ClassNotFoundException | SQLException e)
{ resp.sendError(500,e.toString());
} }}
Catlog.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Catlog extends HttpServlet
{
@Override
public void service(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException
{
PrintWriter pw=resp.getWriter();
pw.println("<html><body bgcolor=green>");
String title=req.getParameter("title");
try
{
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection
con=DriverManager.getConnection("jdbc:derby://localhost:1527/OnlineBookStorage","root",
"root");
Statement stmt=con.createStatement();
String sqlstmt="select id,password from login";
ResultSet rs=stmt.executeQuery(sqlstmt);
int flag=0;
while(rs.next())
{
pw.println(",div align=\"center\">");
pw.println("TITLE :"+rs.getString(1)+"<br>");
pw.println("AUTHOR :"+rs.getString(2)+"<br>");
pw.println("VERSION :"+rs.getString(3)+"<br>");

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

pw.println("PUBLISHER :"+rs.getString(4)+"<br>");
pw.println("COST :"+rs.getString(5)+"<br>");
pw.println("</div");
flag=1;
}
if(flag==0)
{
pw.println("SORRY INVALID TITLE TRY AGAIN <br><br>");
pw.println("<a href=\"/tr/catalog.html\">press HERE to RETRY</a>");
}
pw.println("</body></html>");
}
catch(ClassNotFoundException | SQLException e)
{
resp.sendError(500,e.toString());
}
}
}
Profile.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Profile extends HttpServlet
{
@Override
public void service(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException
{
PrintWriter pw=resp.getWriter();
pw.println("<html><body bgcolor=green>");
String id=req.getParameter("id");
try
{
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection
con=DriverManager.getConnection("jdbc:derby://localhost:1527/OnlineBookStorage","root",
"root");
Statement stmt=con.createStatement();
String sqlstmt="select * from login where id="+id+"";
ResultSet rs=stmt.executeQuery(sqlstmt);
int flag=0;
pw.println("<br><br><br>");
while(rs.next())
{
pw.println("<div align=\"center\">");
pw.println("NAME :"+rs.getString(1)+"<br>");
pw.println("ADDRESS :"+rs.getString(2)+"<br>");

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

pw.println("PHONE NO :"+rs.getString(3)+"<br>");
pw.println("</div>");
flag=1;
}
if(flag==0)
{
pw.println("SORRY INVALID ID TRY AGAIN ID<br><br>");
pw.println("<a href=\"/tr/profile.html\">press HERE to RETRY</a>");
}
pw.println("</body></html>");
}
catch(Exception e)
{
resp.sendError(500,e.toString());
}
}
}
Order.java
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class order extends HttpServlet
{
@Override
public void service(HttpServletRequest req,HttpServletResponse resp)
throws ServletException,IOException
{
PrintWriter pw=resp.getWriter();
pw.println("<html><body bgcolor=green>");
String id=req.getParameter("id");
String pwd=req.getParameter("pwd");
String title=req.getParameter("title");
String count1=req.getParameter("no");
String date=req.getParameter("date");
String cno=req.getParameter("cno");
int count=Integer.parseInt(count1);
try
{
Class.forName("org.apache.derby.jdbc.ClientDriver");
try (Connection con =
DriverManager.getConnection("jdbc:derby://localhost:1527/OnlineBookStorage","root","root
")) {
Statement stmt=con.createStatement();
String sqlstmt="select id,password from login";
ResultSet rs=stmt.executeQuery(sqlstmt);
int flag=0,amount,x;
while(rs.next())

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

{
if(id.equals(rs.getString(1))&&pwd.equals(rs.getString(2)))
{
flag=1;
}
} if(flag==0)
{
pw.println("SORRY INVALID ID TRY AGAIN ID<br><br>");
pw.println("<a href=\\order.html\\>press HERE to RETRY</a>");
}
else
{
Statement stmt2=con.createStatement();
String s="select cost from book where title="+title+"";
ResultSet rs1=stmt2.executeQuery(s);
int flag1=0;
while(rs1.next())
{
flag1=1;
x=Integer.parseInt(rs1.getString(1));
amount=count*x;
pw.println("AMOUNT :"+amount+"<br><br><br><br>");
Statement stmt1=con.createStatement();
stmt1.executeUpdate("insert into details
values("+id+","+title+","+amount+","+cno+")");
pw.println("YOUR ORDER has taken<br>");
}
if(flag1==0)
{
pw.println("SORRY INVALID ID TRY AGAIN ID<br><br>");
pw.println("<a href=\\order.html\\>press HERE to RETRY</a>");
}
} pw.println("</body></html>");
}
}
catch(ClassNotFoundException | SQLException | NumberFormatException e)
{
resp.sendError(500,e.toString());
}
}}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>login</servlet-class>
</servlet>
<servlet>
<servlet-name>reg</servlet-name>
<servlet-class>reg</servlet-class>
</servlet>
<servlet>

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

<servlet-name>Catlog</servlet-name>
<servlet-class>Catlog</servlet-class>
</servlet>
<servlet>
<servlet-name>Profile</servlet-name>
<servlet-class>Profile</servlet-class>
</servlet>
<servlet>
<servlet-name>order</servlet-name>
<servlet-class>order</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>

Output:

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

Result:
Thus the TOMCAT web server has been installed and the static web pages of programs into
dynamic web pages using servlets (or JSP) and cookies has been excuted.

Ex.No:5a Write programs in Java using Servlets: To invoke servlets from HTML forms

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

AIM
To create a simple application to invoke servlets from HTML forms.

ALGORITHM:
1.Create a Servlet program using java
2.Create a html program for invoke the servlets

Servlet.java
import java.io.*;
import java.util.*;

import javax.servlet.*;

public class PostParam extends GenericServlet

public void service(ServletRequest request,ServletResponse response) throws


ServletException,IOException

PrintWriter pw = response.getWriter();

Enumeration e = request.getParameterNames();

while(e.hasMoreElements())

String pname = (String)e.nextElement();

pw.print(pname + " = ");

String pvalue = request.getParameter(pname);

pw.println(pvalue);

pw.close();

HTML CODE:

<HTML><head>

<TITLE>INVOKING SERVLET FROM HTML</TITLE> </head>

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

<BODY> <CENTER>

<FORM name = "PostParam" method = "Post" action="http://localhost:8084/IP/PostParam">

<TABLE> <tr>

<td><B>Employee </B> </td>

<td><input type = "textbox" name="ename" size="25" value=""></td></tr>

<tr><td><B>Phone </B> </td>

<td><input type = "textbox" name="phoneno" size="25" value=""></td></tr></TABLE>

<INPUT type = "submit" value="Submit"></FORM>

</CENTER></body></html>

Output:

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

Result:
Thus the simple application to invoke servlets from HTML forms has been executed.

Ex.No:5b Session tracking using hidden form fields and Session tracking for a hit count

AIM
To create a simple application to perform session tracking using servlet.

ALGORITHM
client.html
1. Create a web page using HTML form that contains the fields such as text, password
and one submit button.
2. Set the URL of the server as the value of form’s action attribute.
3. Run the HTML program.
4. Submit the form data to the server.
server.java
1. Define the class server that extends the property of the class GenericServlet.
2. Handle the request from the client by using the method service() of GenericServlet
class.
3. Get the parameter names from the HTML form by using the method
getParameterNames().
4. Get the parameter values from the HTML forms by using the method getParameter().
5. Send the response to the client by using the method of PrintWriter class.

index.html
<!DOCTYPE html>
<html>

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

<head>
<meta charset="ISO-8859-1">
<title>Servlet Login Example</title>
</head>
<body>
<h1>Welcome to Login App by Cookie</h1>
<a href="login.html">Login</a>|
<a href="LogoutServlet">Logout</a>|
<a href="ProfileServlet">Profile</a>
</body>
</html>
link.html
<a href="login.html">Login</a> |
<a href="LogoutServlet">Logout</a> |
<a href="ProfileServlet">Profile</a>
<hr>
login.html
<form action="LoginServlet" method="post">
Name:<input type="text" name="name"><br>
Password:<input type="password" name="password"><br>
<input type="submit" value="login">
</form>
LoginServlet.java
package com.javatpoint;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
request.getRequestDispatcher("link.html").include(request, response);
String name=request.getParameter("name");
String password=request.getParameter("password");
if(password.equals("admin123")){
out.print("You are successfully logged in!");
out.print("<br>Welcome, "+name);
Cookie ck=new Cookie("name",name);
response.addCookie(ck);
}else{
out.print("sorry, username or password error!");
request.getRequestDispatcher("login.html").include(request, response);
}
out.close();
}
}
LogoutServlet.java
package com.javatpoint;
import java.io.IOException;
import java.io.PrintWriter;

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LogoutServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
request.getRequestDispatcher("link.html").include(request, response);
Cookie ck=new Cookie("name","");
ck.setMaxAge(0);
response.addCookie(ck);
out.print("you are successfully logged out!");
}
}
File: ProfileServlet.java
package com.javatpoint;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ProfileServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
request.getRequestDispatcher("link.html").include(request, response);
Cookie ck[]=request.getCookies();
if(ck!=null){
String name=ck[0].getValue();
if(!name.equals("")||name!=null){
out.print("<b>Welcome to Profile</b>");
out.print("<br>Welcome, "+name);
}
}else{
out.print("Please login first");
request.getRequestDispatcher("login.html").include(request, response);
}
out.close();
}
Page 27/61
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml
/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<description></description>

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

<display-name>LoginServlet</display-name>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.javatpoint.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>ProfileServlet</display-name>
<servlet-name>ProfileServlet</servlet-name>
<servlet-class>com.javatpoint.ProfileServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ProfileServlet</servlet-name>
<url-pattern>/ProfileServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>LogoutServlet</display-name>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>com.javatpoint.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/LogoutServlet</url-pattern>
</servlet-mapping>

Output:

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

Result :

Thus the simple application to perform session tracking using servlet has been executed.

Ex.No:6 CREATION OF THREE-TIER APPLICATIONS USING JSP AND DATABASES


For conducting on-line examination

AIM
To create a three-tier application using JSP and Databases for the conduction of online-examination.
ALGORITHM

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

Design the HTML page (stud.html) with the following


a. Create a form to get the input (Register Number) from the user.
b. Set the URL of the server (marklist.jsp) as the value of the action attribute.
c. Use submit button to invoke the server and send the form data to the server.
2. Create the JSP file with the following
a. Read the parameter value (Register Number) from the form by using the method
getParameter().
b. Server retrieves the details from the database table with respect to the form input.
c. Server displays the mark list to the client as the response.
CODE
marklist.jsp:
<%@ page contentType="text/html" language="java" import="java.sql.*"%>
<html>
<head>
<title>Three Tier Application</title>
<style type="text/css">
body{color:blue;font-family:courier;text-align:center}
</style>
</head>
<body>
<h2>EXAMINATION RESULT</h2><hr/>
<%
String str=request.getParameter("regno");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:markDS");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("SELECT*FROM markTab WHERE rno="+str);
while(rs.next())
{
%>
Name:<%=rs.getObject(2)%><br/>
<table border="1">
<th>SUBJECT</th><th>Mark</th>
<tr><td>Network Programming and
Management</td><td><%=rs.getObject(3)%></td></tr>
<tr><td>Object Oriented Analysis and Design</td><td><%=rs.getObject(4)%></td></tr>
<tr><td>Cryptography and Network Security</td><td><%=rs.getObject(5)%></td></tr>
<tr><td>Embedded Systems</td><td><%=rs.getObject(6)%></td></tr>
<tr><td>Web Technology</td><td><%=rs.getObject(7)%></td></tr>
<tr><td>Software Requirement and Engineering</td><td><%=rs.getObject(8)%></td></tr>
</table>
<%
}
%>
<br/>
<a href="stud.html">Back</a>
</body>
</html>
stud.html
<html>
<head>
<title>Three Tier Application</title>
<style type="text/css">
body{color:blue;font-family:courier;text-align:center}
</style>

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

</head>
<body>
<h2>EXAMINATION RESULT</h2><hr/>
<form name="f1" method"GET" action="marklist.jsp">
Enter Your Reg.No:
<input type="text" name="regno"/><br/><br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;
<input type="submit" value="SUBMIT"/>
</form>
</body>
<html>
Register No:<%=rs.getObject(1)%><br/>

Output:

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

Result:

To create a three-tier application using JSP and Databases for the conduction of online-
examination.

Ex.No:6 b Redo the previous task using JSP by converting the static web pages into dynamic web
pages. Create a database with user information and books information. The books catalogue should
be dynamically loaded from the database.

Aim:
To redo the task by converting the static web pages to dynamic web pages.
ALGORITHM:
1) Create your own directory under tomcat/webapps (e.g.tr1)
2) Copy the html files intr1
3) Copy the jsp files into tr1
4) Start tomcat give the following
command Catalina.batrun
At install‐dir/bin
5) at I.E give url ashttp://localhost:8081/tr1/main.html
Main.html:
<html>
<body bgcolor=”pink”>
<br><br><br><br><br><br>
<h1 align=”center”>>U>ONLINE BOOK STORAGE</u></h1><br><br><br>
<h2 align=”center”><PRE>
<b> Welcome to online book storage. Press LOGIN if you are having id Otherwise press registration
</b></PRE></h2>
<br><br><pre>
<div align=”center”>

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

<a href=”/tr/login.html”>LOGIN</a>
<a href=”/tr/login.html”>REGISTRATION</a></div></pre>
</body></html>
Login.html:
<html>
<body bgcolor=”pink”><br><br><br>
<form name="myform" method="post" action=/tr1/login.jsp">
<div align="center"><pre>
LOGIN ID :<input type="text" name="pwd"></pre><br><br>
PASSWORD : <input type="password" name="pwd"></pre><br><br>
</div>
<br><br>
<div align="center">
<inputtype="submit"value="ok"onClick="validate()">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="reset" value="clear">
</form>
</body></html>
Reg.html:
<html>
<body bgcolor="pink"><br><br>
<form name="myform" method="post" action="/tr1/reg.jsp">
<div align="center"><pre>
NAME :<input type="text" name="name"><br>
ADDRESS :<input type="text"name="addr"><br>
CONTACT NUMBER :<input type="text" name="phno"><br>
LOGINID : <input type="text"name="id"><br>
PASSWORD :<input type="password" name="pwd"></pre><br><br>
</div>
<br><br>
<div align="center">
<inputtype="submit"value="ok"
onClick="validate()">()">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input
type="reset" value="clear">
</form>
</body>
</html>
Profile.html:
<html>
<body bgcolor="pink"><br><br>
<form name="myform" method="post" action="/tr1/profile.jsp">
<div align="center"><pre>
LOGINID : <input type="text"name="id"><br>
</pre><br><br>
</div>
<br><br>
<div align="center">
<inputtype="submit"value="ok" onClick="validate()">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="reset" value="clear">
</form>
</body>
</html>
Catalog.html:
<html>
<body bgcolor="pink"><br><br><br>
<form method="post" action="/tr1/catalog.jsp">

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

<div align="center"><pre>
BOOK TITLE :<input type="text" name="title"><br>
</pre><br><br>
</div>
<br><br>
<div align="center">
<inputtype="submit"value="ok"
name=”button1”>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<inputtype="reset"value="clear"
name=”button2”>
</form>
</body>
</html>
Order.html:
<html>
<body bgcolor="pink"><br><br><br>
<form method="post" action="/tr1/order.jsp">
<div align="center"><pre>
LOGINID :<input type="text" name="id"><br>
PASSWORD :<input type="password" name="pwd"><br>
TITLE:<input type="text"name="title"><br>
NO. OF BOOKS :<input type="text" name="no"><br>
DATE : <input type="text"name="date"><br>
CREDIT CARD NUMBER :<input type="password" name="cno"><br></pre><br><br>
</div>
<br><br>
<div align="center">
<input type="submit" value="ok" name=”button1”>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input
type="reset" value="clear"name=”button2”>
</form>
</body>
</html>
Login.jsp:
%@page import=”java.sql.*”%
%@page import=”java.io.*”%
<%out.println(“<html><body bgcolor=\”pink\”>”);
String id=request.getParameter(“id”);
String pwd=request.getParameter(“pwd”);
Driver d=new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(d);
Connection con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:orcl”,”scott”,”tiger”);
Statement stmt=con.createStatement();
String sqlstmt=”selectid,password from login where id=”+id+” and password=”+pwd+””;
ResultSetrs=stmt.executeQuery(sqlstmt);
int flag=0;
while(rs.next())
{
flag=1;
}
if(flag==0)
{
out.println(“SORRY INVALID ID TRY AGAIN ID<br><br>”);
out.println(“ <a href=\”/tr1/login.html\”>press LOGIN to RETRY</a>”);
}
else
{

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

out.println(“VALID LOGIN ID<br><br>”);


out.println(“<h3><ul>”);
out.println(“<li><ahref=\”profile.html\”><fontcolor=\”black\”>USER
PROFILE</font></a></li><br><br>”);
out.println(“<li><ahref=\”catalog.html\”><fontcolor=\”black\”>BOOKS
CATALOG</font></a></li><br><br>”);
out.println(“<li><ahref=\”order.html\”><fontcolor=\”black\”>ORDER
CONFIRMATION</font></a></li><
br><br>”);
out.println(“</ul>”);
}
out.println(“<body></html>”);
%>

Reg.jsp:
%@page import=”java.sql.*”%
%@page import=”java.io.*”%
<% out.println(“<html><body bgcolor=\”pink\”>”);
String name=request.getParameter(“name”);
String addr=request.getParameter(“addr”);
String phno=request.getParameter(“phno”);
String id=request.getParameter(“id”);
String pwd=request.getParameter(“pwd”);
int no=Integer.parseInt(phno);
Driver d=new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(d);
Connection con=DriverManager.getConnection
(“jdbc:oracle:thin:@localhost:1521:orcl”,”scott”,”tiger”);
Statement stmt=con.createStatement();
String sqlstmt=”select id from login”;
ResultSetrs=stmt.executeQuery(sql stmt);
int flag=0;
while(rs.next())
{
if(id.equals(rs.getString(1)))
{
flag=1;
}
}
if(flag==1)
{
out.println(“SORRY LOGIN ID ALREADY EXISTS TRY AGAIN WITH NEW ID <br><br>”);
out.println(“<a href=\”/tr1/reg.html\”>press REGISTER to RETRY</a>”);
}
else
{
Statement stmt1=con.createStatement ();
stmt1.executeUpdate (“insert into login values (“+name+”,”+addr+”,”+no+”,”+id+”,”+pwd+”)”);
out.println (“YOU DETAILS
ARE ENTERED <br><br>”);
out.println (“<a href =\”/tr1/login.html\”>press LOGIN to login</a>”);
}
out.println (“</body></html>”);
%>
Profile.jsp:

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

<%@page import=”java.sql.*”%>
<%@page import=”java.io.*”%>
<%
out.println (“<html><body bgcolor=\”pink\”>”);
String id=request.getParameter(“id”);
Driver d=new oracle.jdbc.driver.OracleDriver();
DriverManager.regiserDriver(d);
Connection con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:orcl”,”s
cott”,”tiger”);
Statement stmt=con.createStatement ();
String sqlstmt=”select * from login where id=”+id+””;
ResultSetrs=stmt.executeQuery(sqlstmt);
Int flag=0;
while(rs.next())
{
out.println (“<div align=\”center\”>”);
out.println(“NAME :”+rs.getString(1)+”<br>”);
out.println (“ADDRESS:”+rs.getString(2)+”<br>”);
out.println(“PHONE NO :”+rs.getString(3)+”<br>”);
out.println (“</div>”);
flag=1;
}
if(flag==0)
{
out.println(“SORRY INVALID ID TRY AGAIN ID <br><br>”);
out.println(“<a href=\”/tr1/profile.html\”>press HERE to RETRY </a>”);
}
out.println (“</body></html>”);
%>
Catalog.jsp:
<%@page import=”java.sql.*”%>
<%@page import=”java.io.*”%>
<%
out.println (“<html><body bgcolor=\”pink\”>”);
String title=request.getParameter (“title”);
Driver d=new oracle.jdbc.driver.OracleDriver ();
DriverManager.regiserDriver (d);
Connection con=DriverManager.getConnection(“jdbc:oracle:thin:@localhost:1521:orcl”,”scott”,”tiger”);
Statement stmt=con.createStatement ();
String sqlstmt=”select * from book where title=”+title+””;
ResultSetrs=stmt.executeQuery(sqlstmt);
Int flag=0;
while(rs.next())
{
out.println (“<div align=\”center\”>”);
out.println(“TITLE:”+rs.getString(1)+”<br>”);
out.println (“AUTHOR:”+rs.getString(2)+”<br>”);
out.println(“VERSION:”+rs.getString(3)+”<br>”);
out.println (“PUBLISHER :”+rs.getString(4)+”<br>”);
out.println(“COST :” +rs.getString(5)+”<br>”);
out.println (“</div>”);
flag=1;
}
if(flag==0)
{

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

out.println(“SORRY INVALID ID TRY AGAIN ID <br><br>”);


out.println(“<a href=\”/tr1/catalog.html\”>press HERE to RETRY </a>”);
}
out.println (“</body></html>”);
%>
Order.jsp:
<%@page import=”java.sql.*”%>
<%@page import=”java.io.*”%>
<%out.println (“<html><body bgcolor=\”pink\”>”);
String id=request.getParameter (“id”);
String pwd=request.getParameter (“pwd”);
String title=request.getParameter (“title”);
String count1=request.getParameter(“no”);
String date=request.getParameter (“date”);
String cno=request.getParameter(“cno”);
Int count=Integer.parseInt(count1);
Driver d=new oracle.jdbc.driver.OracleDriver ();
DriverManager.regiserDriver (d);
Connection con=DriverManager.getConnection
(“jdbc:oracle:thin:@localhost:1521:orcl”,”scott”,”tiger”);
Statement stmt=con.createStatement ();
String sqlstmt=”select id, password from login”;
ResultSet rs=stmt.executeQuery(sqlstmt);
Int flag=0,amount,x;
while(rs.next())
{
if(id.equals(rs.getString(1))&&pwd.equals(rs.getString(2)))
{
flag=1;
}
}
if(flag==0)
{
}
else{
{
}
out.println(“SORRY INVALID ID TRY AGAIN ID <br><br>”);
out.println(“<a href=\”/tr1/order.html\”>press HERE to RETRY </a>”);
Statement stmt2=con.createStatement();
String s=”select cost from book where title=”+title+””;
ResultSet rs1=stmt2.executeQuery(s);
int flag1=0;
while(rs1.next())
flag1=1;
x=Integer.parseInt(rs1.getString(1));
amount=count*x;
out.println(“AMOUNT:”+amount+”<br><br><br><br>”);
Statement stmt1=con.createStatement ();
stmt1.executeUpdate (“insert into details(“+id+”,”+title+”,”+amount+”,”+date+”,”+cno+”)”); out.println
(“YOU ORDER HAS TAKEN<br>”);
if(flag1==0)
{
out.println(“SORRY INVALID BOOK TRY AGAIN <br><br>”);
out.println(“<a href=\”/tr1/order.html\”>press HERE to RETRY </a>”);

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

}
} out.println (“</body></html>”);%>

Output:

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

Result

Thus the JSP by converting the static web pages into dynamic web pages has been executed.
Ex.No:7 Create and save an XML document at the server, which contains 10 users
Information. Write a Program, which takes user Id as an input and returns the User details by
taking the user information from the XML document

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

AIM:
Generating an xml file containing 10 users information .Display the user details by taking user information
from xml file using DOM Parsers.
Algorithm:
1. Create an XML Program for getting 10 users input
2. Using DOM Parsers, java program retrieve the user details
3. Display the user details based on user id

XML Program
<employees>
<employee id="111">
<firstName>Chandrika</firstName>
<lastName>Gupta</lastName>
<location>India</location>
</employee>
<employee id="222">
<firstName>Srinivas</firstName>
<lastName>Reddy</lastName>
<location>Russia</location>
</employee>
<employee id="333">
<firstName>Anupama</firstName>
<lastName>P</lastName>
<location>USA</location>
</employee>

<employee id="444">
<firstName>Lokesh</firstName>
<lastName>Gupta</lastName>
<location>India</location>
</employee>
<employee id="555">
<firstName>Vishnu</firstName>
<lastName>Gussin</lastName>
<location>Russia</location>
</employee>
<employee id="666">
<firstName>Veeru</firstName>
<lastName>Feezor</lastName>
<location>USA</location>

</employee>
<employee id="777">
<firstName>Pavan</firstName>
<lastName>Feezor</lastName>
<location>USA</location>
</employee>
<employee id="888">
<firstName>Narayana</firstName>
<lastName>Gussin</lastName>
<location>Russia</location>
</employee>
<employee id="999">

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

<firstName>David</firstName>
<lastName>Feezor</lastName>
<location>USA</location>
</employee>
<employee id="1000">
<firstName>Sunder</firstName>
<lastName>Feezor</lastName>
<location>USA</location>
</employee>
</employees>

ReadXML.java:
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import java.util.Scanner;
publicclassReadXML {
publicstaticvoidmain(String a[]) throws Exception{ DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
//Build Document
Document document = builder.parse(new ile("C:\\Users\\Narayana\\Desktop\\employees.xml"));
//Normalize the XML Structure; It's just too important !! document.getDocumentElement().normalize();
Element root = document.getDocumentElement();
//Get all employees
NodeListnList = document.getElementsByTagName("employee"); System.out.println("enter employee
id:");
Scanner s=new Scanner(System.in); String id=s.next();
for (int temp = 0; temp <nList.getLength(); temp++)
{
Node node = nList.item(temp);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) node;
if(eElement.getAttribute("id").equals(id)){
System.out.println("First Name : "+
eElement.getElementsByTagName("firstName").item(0).getTextContent());
System.out.println("Last Name:" +
eElement.getElementsByTagName("lastName").item(0).getTextContent());
System.out.println("Location:" +
eElement.getElementsByTagName("location").item(0).getTextContent());
}
}
}
}
}

OUTPUT:-
Enter Employee Id: 222

Downloaded by SHEIK MOHAMED ([email protected])


lOMoARcPSD|37890669

Result
Thus the document created using xml has been executed.

Downloaded by SHEIK MOHAMED ([email protected])

You might also like