Web Technologies Lab Manual Web Technologies Lab Manual
Web Technologies Lab Manual Web Technologies Lab Manual
Name Signature
Prepared By Ms.M.Lakshana/AP
Verified By Dr.J.Suresh,Hod/CSE
Approved BY Dr.s.Shanthi, Principal
COURSE OBJECTIVES:
To understand different Internet Technologies
To learn java-specific web services architecture
To Develop web applications using frameworks
LIST OF EXPERIMENTS
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.
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:
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.
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>
<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
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"
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.
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>
</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()" />
<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()" />
<input type="reset" value="clear" />
</div></form></body></html>
Catlog.html
<html xmlns="http://www.w3.org/1999/xhtml">
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
{
{
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>");
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>");
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())
{
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>
<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:
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
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.*;
PrintWriter pw = response.getWriter();
Enumeration e = request.getParameterNames();
while(e.hasMoreElements())
pw.println(pvalue);
pw.close();
HTML CODE:
<HTML><head>
<BODY> <CENTER>
<TABLE> <tr>
</CENTER></body></html>
Output:
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>
<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;
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>
<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:
Result :
Thus the simple application to perform session tracking using servlet has been executed.
AIM
To create a three-tier application using JSP and Databases for the conduction of online-examination.
ALGORITHM
</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/>
<input type="submit" value="SUBMIT"/>
</form>
</body>
<html>
Register No:<%=rs.getObject(1)%><br/>
Output:
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”>
<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()">
<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()">()"> <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()">
<input type="reset" value="clear">
</form>
</body>
</html>
Catalog.html:
<html>
<body bgcolor="pink"><br><br><br>
<form method="post" action="/tr1/catalog.jsp">
<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”> <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”> <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
{
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:
<%@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)
{
}
} out.println (“</body></html>”);%>
Output:
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
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">
<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
Result
Thus the document created using xml has been executed.