You are on page 1of 56

WEB TECHNOLOGY

TECHNOLOGYLAB
(KCS-652)

LAB FILE

B.TECH 3RD Year, Semester


Semester--VI

COMPUTER SCIENCE & ENGINEERING

Faculty Name: Submitted By:


y:
Assistant Professor Roll Number:
Vision of the Institute

“To become a leading institute of providing professionally competent and socially responsive
technocrats with high moral values”

Mission of the Institute

M1: To create an eco-system for dissemination of technical knowledge, to achieve academic


excellence.
M2: To develop technocrats with creative skills and leadership qualities, to solve local and global
challenges.
M3: To impart human values and ethics in students, to make them socially and Eco-friendly
responsible.
Vision of the Department

“To produce globally competent professionals having social values and committed to serve the
global needs with ability to work in an interdisciplinary environment”.

Mission of the CSE Department

M1: To impart quality education to enhance ethical, professional and leadership qualities to make
them globally competitive.
M2: To create a conducive environment in which students can explore computational problems,
analyze them and identify their optimal solutions.
M3: To strive for continual enhancement of technical knowledge & innovation through industry
interface to accomplish global needs.

Program Educational Objectives (PEOs)

PEO1: Students must be able to apply software engineering principles to analyze complex
computing problems and identify their solutions.

PEO2:Students must be able to analyze, design, and implement the latest technology-driven
projects.

PEO3:Students must be able to work in a collaborative environment and understand the ethical,
social, and economic impact of their work.
Program Outcome(POs)
Engineering Graduates will be able to:
PO:1 Engineering knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering problems.
PO:2 Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.
PO:3 Design/development of solutions: Design solutions for complex engineering problems and
design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.
PO:4 Conduct investigations of complex problems: Use research-based knowledge and research
methods including design of experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions.
PO:5 Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities with
an understanding of the limitations.
PO:6 The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.
PO:7 Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and need for
sustainable development.
PO:8 Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
PO:9 Individual and team work: Function effectively as an individual, and as a member or leader
in diverse teams, and in multidisciplinary settings.
PO:10 Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and receive
clear instructions.
PO:11 Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member and leader
in a team, to manage projects and in multidisciplinary environments.
PO:12 Life-long learning: Recognize the need for, and have the preparation and ability to engage
in independent and life-long learning in the broadest context of technological change.
Program Specific Outcomes (PSOs)

PSO 1: Able to design and implement the data structures and algorithms to deliver quality software
products.

PSO 2: Able to apply Artificial Intelligence and Machine Learning concepts to solve society-
related needs.

Course Outcomes (COs)

Bloom's
Course Outcome ( CO) At the end of course , the student will be able to
Level (L)

CO 1 Develop static web pages using HTML L3

CO 2 Develop Java programs for window/web-based applications. L3

CO 3 Design dynamic web pages using Javascript and XML. L4

Design dynamic web page using server site programming Ex.


CO 4 L4
ASP/JSP/PHP

Design server site applications using JDBC,ODBC and section tracking


CO 5 L4
API
INDEX

S. No. Name of Experiment CO's BTL Date Sign

1. Write HTML program to display your CV in a browser, CO1 L3

Develop and demonstrate the usage of inline, internal and CO1 L3


2. external style sheet using CSS

Design the following static web pages required for an online CO1 L3
bookstore website.
3. 1. LOGIN PAGE
2. REGISTRATION PAGE

Write a program in XML for creation of DTD, which CO3 L4


4. specifies a set of rules. Create a style sheet in CSS/ XSL &
display the document in internet explorer.

Write a programs using Java script for Web Page to display CO3 L4
5.
browsers information.

Write a Java applet to display the Application Program CO2 L3


6.
screen i.e. calculator and other.

Install a database (Mysql or Oracle). Create a table which CO5 L4


should contain at least the following fields: name, password,
email-id, phone number Write a java program/servlet/JSP to
7. connect to that database and extract data from the tables and
display them. Insert the details of the users who register with
the web site, whenever a new user clicks the submit button in
the registration page.

Install TOMCAT web server and APACHE. Access the above CO4 L4
8. developed static web pages for books website, using these
servers by putting the web pages developed.

Write a JSP which inserts the details of the 3 or 4 users who CO4 L4
register with the web site by using registration form.
9.
Authenticate the user when he submits the login form using
the username and password from the database

Assume four users user1, user2, user3 and user4 having the CO4 L4
passwords pwd1, pwd2, pwd3 and pwd4 respectively. Write a
servlet for doing the following.
1. Create a Cookie and add these four user
10.
id’s and passwords to this Cookie.
2. Read the user id and passwords entered in
the Login form and authenticate with the
values available in the cookies
PROGRAM 1

Objective: To write an HTML program to display your CV in a


browser.

Program:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>CV</title>
<style>
body {
font-family: Arial, sans-serif;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 8px;
border-bottom: 1px solid #ddd;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<table>
<tr>
<th colspan="2">Personal Information</th>
</tr>
<tr>
<td>Name</td>
<td>John Doe</td>
</tr>
<tr>
<td>Email</td>
<td>john@example.com</td>
</tr>
<tr>
<td>Phone</td>
<td>+1234567890</td>
</tr>
</table>
<table>
<tr>
<th colspan="2">Education</th>
</tr>
<tr>
<td>Degree</td>
<td>Bachelor of Science in Computer Science</td>
</tr>
<tr>
<td>School</td>
<td>University of XYZ</td>
</tr>
<tr>
<td>Year</td>
<td>2010-2014</td>
</tr>
</table>
<table>
<tr>
<th colspan="2">Experience</th>
</tr>
<tr>
<td>Company</td>
<td>ABC Solutions</td>
</tr>
<tr>
<td>Position</td>
<td>Software Engineer</td>
</tr>
<tr>
<td>Duration</td>
<td>2014-Present</td>
</tr>
</table>
</body>
</html>
Output:
PROGRAM-2

Objective: To develop and demonstrate the usage of inline, internal and external style sheet
using CSS.

1) Use different font styles


2) Control the repetition of image with background-repeat and no-repeat property
3) Define style for links as a: link, a: active, a: hover, a: visited.
4) Add customized cursors for links

Program:

 style.css
p.left
{
text-align:left;
color:blue;
font-family:Cambria;
font-size:large;
text-indent:20px;
}
p.center
{
text-align:center;
text-decoration:underline;
text-transform:uppercase;
letter-spacing:-3px;
word-spacing:20px;
font-size:larger;
}
p.right
{
text-align:right;
color:red;
font-family:Tahoma;
font-size:15pt;
text-decoration:overline;
font-style:italic;
}
b#headline
{
color:orange;
font-size:22px;
font-family:arial;
text-decoration:underline;
}
 sample.html
<html>
<head>
<style type="text/css">
body
{
background-image:url('images/cse.png');
background-repeat:no-repeat;
background-position:center center;
background-attachment:fixed;
background-color:pink;
}
a:link { text-decoration:none;color:orange; }
a:visited { text-decoration:none;color:red; }
a:hover { text-decoration:underline;color:blue; }
a:active { text-decoration:underline;color:purple; }
h3 { color:green; }
.c1{cursor:crosshair}
.c2{cursor:pointer}
.c3{cursor:move}
.c4{cursor:text}
.c5{cursor:wait}
.c6{cursor:help}
</style>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body bgcolor="cyan">
<h1 style="color:blue;text-align:center;"> CSS (Inline, Internal and External) </h1>
<p>This Paragraph is a Not Styled</p>
<p class="left">This Paragraph is Styled by class "Left"</p>
<p class="center">This Paragraph is Styled by class "Center"</p>
<p class="right">This Paragraph is Styled by class "Right"</p>
<b>This is normal Bold</b><br>
<b id="headline">This Bold Text is Styled </b>
<h2><b><a href="">This is a link</a></b></h2>
<h3 class="c1">The cursor over this element is plus sign</h3>
<h3 class="c2">The cursor over this element is a pointing hand</h3>
<h3 class="c3">The cursor over this element is a grasping hand</h3>
<h3 class="c4">The cursor over this element is a I bar</h3>
<h3 class="c5">The cursor over this element is a wait</h3>
<h3 class="c6">The cursor over this element is a question mark</h3>
</html
Output:
PROGRAM-3

Objective: To design the following static web pages required for an online
bookstore website.

1. LOGIN PAGE
2. REGISTRATION PAGE

Program:

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Online Bookstore - Login & Registration</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 0;
}

.container {
max-width: 400px;
margin: 50px auto;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

h2 {
text-align: center;
}

input[type="text"],
input[type="password"],
input[type="submit"] {
width: 100%;
padding: 10px;
margin: 8px 0;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}

input[type="submit"] {
background-color: #4caf50;
color: #fff;
cursor: pointer;
}

input[type="submit"]:hover {
background-color: #45a049;
}

.switch-form {
text-align: center;
}
</style>
</head>

<body>

<div class="container">
<div id="login-form">
<h2>Login</h2>
<form action="/login" method="post">
<input type="text" name="username" placeholder="Username" required>
<input type="password" name="password" placeholder="Password" required>
<input type="submit" value="Login">
</form>
<p class="switch-form">Don't have an account? <a href="#"
onclick="toggleForm('registration')">Register here</a>.</p>
</div>

<div id="registration-form" style="display: none;">


<h2>Register</h2>
<form action="/register" method="post">
<input type="text" name="username" placeholder="Username" required>
<input type="password" name="password" placeholder="Password" required>
<input type="password" name="confirm_password" placeholder="Confirm Password"
required>
<input type="text" name="email" placeholder="Email" required>
<input type="submit" value="Register">
</form>
<p class="switch-form">Already have an account? <a href="#"
onclick="toggleForm('login')">Login here</a>.</p>
</div>
</div>

<script>
function toggleForm(form) {
if (form === 'login') {
document.getElementById('login-form').style.display = 'block';
document.getElementById('registration-form').style.display = 'none';
} else if (form === 'registration') {
document.getElementById('login-form').style.display = 'none';
document.getElementById('registration-form').style.display = 'block';
}
}
</script>

</body>

</html>

Output:
PROGRAM 4

Objective: To write a program in XML for creation of DTD, which specifies a set of
rules, Create a style sheet in CSS/ XSL & display the document in internet explorer.

Program:

 book.html
<?xml-stylesheet href="book.css"?>
<!DOCTYPE book SYSTEM "book.dtd">
<book>
<details>
<title> C</title>
<author> BalaGuru Swami</author>
<ISBN_Number>2536</ISBN_Number>
<publisher>pearson</publisher>
<edition>2</edition>
<price>255/-</price>
</details>
<details>
<title> E-Commerce</title>
<author> Kalakata</author>
<ISBN_Number>8562</ISBN_Number>
<publisher>pearson</publisher>
<edition>5</edition>
<price>300/-</price>
</details>
<details>
<title> CO</title>
<author> Marris </author>
<ISBN_Number>4578</ISBN_Number>
<publisher>Dream Tech</publisher>
<edition>5</edition>
<price>270/-</price>
</details>
<details>
<title> Web Technologies</title>
<author> Kumar </author>
<ISBN_Number>5423</ISBN_Number>
<publisher>Willay</publisher>
<edition>6</edition>
<price>500/-</price>
</details>
<details>
<title> Web Programming</title>
<author> Kumar </author>
<ISBN_Number>1258</ISBN_Number>
<publisher>Willay</publisher>
<edition>6</edition>
<price>500/-</price>
</details>
</book>

 book.dtd
<!ELEMENT details (title, author, ISBN_Number, publisher, edition,
price) >
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT ISBN_Number (#PCDATA)>
<!ELEMENT publisher (#PCDATA)>
<!ELEMENT edition (#PCDATA)>
<!ELEMENT price (#PCDATA)>

 book.css
book{
font-family: Serif ;
background-color: #003
}
title, ISBN_Number {
color: orange;
font-size: x-large;
font-style: italic ;
}
author,edition{
color: white;
font-size: large;
}
publisher{
color: #00f73f;
text-indent: 1em;
text-align: justify
}
price{
color: yellow;
font-size: large;
}
PROGRAM 5

Objective:.To write a program using Java script for Web Page to display browsers information.

Program:

<!DOCTYPE html>
<html>
<head>
<title>Browser Information Using JavaScript</title>
</head>
<body>
<h2>Browser Information</h2>
<script>
document.writeln("<br/>Code_Name: "+navigator.appCodeName);
document.writeln("<br/>App_Name: "+navigator.appName);
document.writeln("<br/>App_Version: "+navigator.appVersion);
document.writeln("<br/>Cookie_Enabled: "+navigator.cookieEnabled);
document.writeln("<br/>Language: "+navigator.language);
document.writeln("<br/>UserAgent: "+navigator.userAgent);
document.writeln("<br/>Platform: "+navigator.platform);
document.writeln("<br/>OnLine: "+navigator.onLine);
</script>
</body>
</html>

Output:
PROGRAM 6

Objective: To write a Java applet to display the Application Program screen i.e.
calculator and other.

Theory

Applet is a special type of program that is embedded in the web page to generate
dynamic content. It runs inside the browser and works at the client side.

There are many advantages of applets. They are as follows

a) It works at client side so less response time

b) Secured

c) It can be executed by browsers running under many platforms, including Linux,


Windows, Mac Os etc.

Program:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet code="Calculator" width="700" height="200">
</applet>*/
public class Calculator extends Applet implements ActionListener {
String msg = "";
TextField t1, t2, t3;
Button b1, b2, b3, b4;
Label l1, l2, l3;
public void init() {
l1 = new Label("First Number");
add(l1);
t1 = new TextField(15);
add(t1);
l2 = new Label("Second Number");
add(l2);
t2 = new TextField(15);
add(t2);
l3 = new Label("Result");
add(l3);
t3 = new TextField(15);
add(t3);
b1 = new Button("ADD");
add(b1);
b1.addActionListener(this);
b2 = new Button("SUB");
add(b2);
b2.addActionListener(this);
b3 = new Button("MULT");
add(b3);
b3.addActionListener(this);
b4 = new Button("DIV");
add(b4);
b4.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
int x = Integer.parseInt(t1.getText());
int y = Integer.parseInt(t2.getText());
int sum = x + y;
t3.setText(" " + sum);
}
if (e.getSource() == b2) {
int x = Integer.parseInt(t1.getText());
int y = Integer.parseInt(t2.getText());
int sub = x - y;
t3.setText(" " + sub);
}
if (e.getSource() == b3) {
int x = Integer.parseInt(t1.getText());
int y = Integer.parseInt(t2.getText());
int mul = x * y;
t3.setText(" " + mul);
}
if (e.getSource() == b4) {
int x = Integer.parseInt(t1.getText());
int y = Integer.parseInt(t2.getText());
int div = x / y;
t3.setText(" " + div);
}
showStatus(" text & button example");
repaint();
}
}

Output:
PROGRAM 7

Objective: To install a database (Mysql or Oracle). Create a table which should contain
at least the following fields: name, password, email-id, phone number Write a java
program/servlet/JSP to connect to that database and extract data from the tables and
display them. Insert the details of the users who register with the web site, whenever a
new user clicks the submit button in the registration page..

Program:

Install MySQL

Step 1: Windows configures MySQL Installer

Step 2 – Welcome Screen: A welcome screen provides several options. Choose the first
option: Install MySQL Products
Step 3 – Download the latest MySQL products: MySQL installer checks and
downloads the latest MySQL products including MySQL server, MySQL Workbench,
etc.

Step 4: Click the Next button to continue

Step 5 – Choosing a Setup Type: there are several setup types available. Choose the
Full option to install all MySQL products and features.
Step 6 – Checking Requirements

Step 7 – Installation Progress: MySQL Installer downloads all selected products. It will
take a while, depending on which products you selected and the speed of your internet
connection.
Step 7 – Installation Progress: downloading Products in progress.

Step 7 – Installation Progress: Complete Downloading. Click the Next button to


continue…
Step 8 – Configuration Overview. Click the Next button to configure MySQL Database
Server

Step 8.1 – MySQL Server Configuration: choose Config Type and MySQL port (3006
by default) and click Next button to continue.
Step 8.1 – MySQL Server Configuration: choose a password for the root account.
Please note the password download and keep it securely if you are installing MySQL
database server on a production server. If you want to add a more MySQL user, you
can do it in this step.

Step 8.1 – MySQL Server Configuration: choose Windows service details including
Windows Service Name and account type, then click Next button to continue.
Step 8.1 – MySQL Server Configuration – In Progress: MySQL Installer is configuring
MySQL database server. Wait until it is done and click the Next button to continue.

Step 8.1 – MySQL Server Configuration – Done. Click the Next button to continue.
Step 8.2 – Configuration Overview: MySQL Installer installs sample databases and
sample models.

Step 9 – Installation Completes: the installation completes. Click the Finish button to
close the installation wizard and launch the MySQL Workbench.

Program:

 Registration.html:
<html>
<head>
<title>Registration page</title>
</head>
<body bgcolor="#00FFFf">
<form METHOD="POST" ACTION="register">
<CENTER>
<table>
<center>
<tr><td> Username </td>
<td><input type="text" name="usr"></td></tr>
<tr><td> Password </td>
<td><input type="password" name="pwd"></td></tr>
<tr><td>Age</td>
<td><input type="text" name="age"></td></tr>
<tr><td>Address</td>
<td><input type="text" name="add"></td></tr>
<tr><td>email</td>
<td><input type="text" name="mail"></td></tr>
<tr><td>Phone</td>
<td><input type="text" name="phone"></td></tr>
<tr><td colspan=2 align=center><input type="submit" value="submit"></td></tr>
</center>
</table>
</form>
</body>

 Login.html
<html>
<head>
<title>Registration page</title>
</head>
<body bgcolor=pink><center><table>
<form METHOD="POST" ACTION="authent">
<tr><td> Username </td>
<td><input type="text" name="usr"></td></tr>
<tr><td> Password </td>
<td><input type="password" name="pwd"></td></tr>
<tr><td align=center colspan="2"><input type="submit" value="submit"></td></tr>
</table></center>
</form>
</body>
</html>

 Ini.java:
import javax.servlet.*;
import java.sql.*;
import java.io.*;
public class Ini extends GenericServlet
{
private String user1,pwd1,email1;

public void service(ServletRequest req,ServletResponse res) throws


ServletException,IOException
{
user1=req.getParameter("user");
pwd1=req.getParameter("pwd");
email1=req.getParameter("email");
res.setContentType("text/html");
PrintWriter out=res.getWriter();
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@195.100.101.158:1521:cclab","
scott","tiger");
PreparedStatement st=con.prepareStatement("insert into personal
values(?,?,?,?,?,?)");
st.setString(1,user1);
st.setString(2,pwd1);
st.setString(3,"25");
st.setString(4,"hyd");
st.setString(5,email1);
st.setString(6,"21234");
st.executeUpdate();
con.close();
}
catch(SQLException s)
{ out.println("not found "+s);
}
catch(ClassNotFoundException c)
{
out.println("not found "+c);
}
}}

 web.xml:
<web-app>
<servlet>
<servlet-name>init1</servlet-name>
<servlet-class>Ini</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>init1</servlet-name>
<url-pattern>/regis</url-pattern>
</servlet-mapping>
</web-app>

Output:
PROGRAM 8

Objective: To install
nstall TOMCAT web server and APACHE. Access the ab above
ove developed
static web pages for books website, using these servers by putting the web pages
developed.

Install the TOMCAT web server:


STEP:1 Download Apache Tomcat
STEP:2 Extract it to Document folder.

STEP:3

 Open Eclipse Environment


 Click on Servers Tab
 Click on No servers are available. Click this link to create a new server...
 Click Tomcat v9.0 Server and Next.
STEP : 4 Select Apache installation Directory and click Finish.

STEP:5. You should see Tomcat v9.0 Server at localhost [Stopped,


Republish] under Servers tab. Double click on it verify HTTP ports information.
By default HTTP port is 8080.
STEP:6 Now right click on Server and click Start.
PROGRAM 9

Objective: To write a JSP which inserts the details of the 3 or 4 users who register with
the web site by using registration form. Authenticate the user when he submits the login
form using the username and password from the database.

Program:

 Main.html:
<html>
<body>
<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><br /><br /><br />
<form name="myform" method="post" action="login.jsp">
<div align="center"><pre>
LOGIN ID :<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>
 Reg.html:
<html>
<body><br /><br />
<form name="myform" method="post" action="reg.jsp">
<table align="center">
<tr>
<td>NAME</td>
<td>:<input type="text" name="name" /></td>
</tr>
<tr>
<td>ADDRESS</td>
<td>:<input type="text" name="addr" /></td>
</tr>
<tr>
<td>CONTACT NUMBER</td>
<td>:<input type="text" name="phno" /></td>
</tr>
<tr>
<td>LOGINID</td>
<td>:<input type="text" name="id" /></td>
</tr>
<tr>
<td>PASSWORD</td>
<td>:<input type="password" name="pwd" /></td>
</tr>
</table>
<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.jsp:
<%@page import="java.sql.*"%>
<%@page import="java.io.*"%>
<% response.setContentType("text/html");
out.println("<html><body>");
String name=request.getParameter("name");
String addr=request.getParameter("addr");
String phno=request.getParameter("phno");
String id1=request.getParameter("id");
String pwd1=request.getParameter("pwd");
int no=Integer.parseInt(phno);
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","ti
ger");
Statement stmt=con.createStatement();
String sqlstmt="select id,pwd from login";
ResultSet rs=stmt.executeQuery(sqlstmt);
int flag=0;
while(rs.next())
{ if(id1.equals(rs.getString(1))&&pwd1.equals(rs.getString(2)))
{ flag=1;
}
}i
f(flag==1)
{ out.println("<br><br>SORRY INVALID ID ALREADY EXITS TRY AGAIN WITH
NEW
ID<br><br>");
out.println("<a href=\"reg.html\">press REGISTER to RETRY</a>");
} else
{ Statement stmt1=con.createStatement();
stmt1.executeUpdate("insert into login
values('"+name+"','"+addr+"',"+phno+",'"+id1+"','"+pwd1+"');");
out.println("<br><br>YOUR DETAILS ARE ENTERED<br><br>");
out.println("<a href=\"login.html\">press LOGIN to login</a>");
}
out.println("</body></html>");
con.close();
%>
 Login.jsp:
<%@page import="java.sql.*"%>
<%@page import="java.io.*"%>
<html>
<body>
<% String id=request.getParameter("id");
String pwd=request.getParameter("pwd");
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","ti
ger")
;
Statement stmt=con.createStatement();
String sqlstmt="select id,pwd from login";
ResultSet rs=stmt.executeQuery(sqlstmt);
int flag=0;
while(rs.next())
{
if(id.equals(rs.getString(1))&&pwd.equals(rs.getString(2)))
{
flag=1;
}
}i
f(flag==0)
{
out.println("<br><br>SORRY INVALID ID TRY AGAIN ID<br><br>");
out.println("<a href=\"login.html\">press LOGIN to RETRY</a>");
} else
{ out.println("<br><br>VALID LOGIN ID<br><br>");
out.println("WELCOME <br>To<br>”+id);
} con.close();
%>
</body>
</html>
Output:

Main.html:

REGISTRATION PAGE:
LOGIN PAGE
PROGRAM 10
Objective: Assume four users user1, user2, user3 and user4 having the passwords pwd1, pwd2,
pwd3 and pwd4 respectively. Write a servlet for doing the following.

1. Create a Cookie and add these four user id’s and passwords to this Cookie.

Program:

 cologin.html:

<html>
<head>
<title> login Page </title>
<p style= "background:yellow; top:100px; left:250px; position:absolute; ">
</head>
<body>
<form ACTION="clogin">
<label> Login </label>
<input type="text" name="usr" size="20"> <br> <br>
<label> Password </label>
<input type="password" name="pwd" size="20"> <br> <br>
<input type="submit" value="submit">
</form>
</body>
</html>
cologin1.html
<html>
<head>
<title> login Page </title>
<p style= "background:yellow; top:100px; left:250px; position:absolute; ">
</head>
<body>
<form ACTION="clogin1">
<label> Login </label>
<input type="text" name="usr" size="20"> <br> <br>
<label> Password </label>
<input type="password" name="pwd" size="20"> <br> <br>
<input type="submit" value="submit">
</form>
</body>
</html>
Addcook.java:
import javax.servlet.* ;
import javax.servlet.http.*;
import java.io.*;
public class Addcook extends HttpServlet
{
String user,pas;
public void service(HttpServletRequest req,HttpServletResponse res) throws
ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
Cookie c1=new Cookie("usr1","suni");
Cookie p1=new Cookie("pwd1","ani");
Cookie c2=new Cookie("usr2","abc");
Cookie p2=new Cookie("pwd2","123");
Cookie c3=new Cookie("usr3","def");
Cookie p3=new Cookie("pwd3","456");
Cookie c4=new Cookie("usr4","mno");
Cookie p4=new Cookie("pwd4","789");
res.addCookie(c1);
res.addCookie(p1);
res.addCookie(c2);
res.addCookie(p2);
res.addCookie(c3);
res.addCookie(p3);
res.addCookie(c4);
res.addCookie(p4);
out.println("COOKIE ADDED");
}
}
 Clogin.java:
import javax.servlet.* ;
import javax.servlet.http.*;
import java.io.*;
public class Clogin extends HttpServlet
{
String user,pas;
public void service(HttpServletRequest req,HttpServletResponse res) throws
ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
user=req.getParameter("usr");
pas=req.getParameter("pwd");
Cookie[] c=req.getCookies();
for(int i=0;i<c.length;i++)
{
if((c[i].getName().equals("usr1")&&c[i+1].getName().equals("pwd1"))||
c[i].getName().equals("usr2")
&&c[i+1].getName().equals("pwd2"))||(c[i].getName().equals("usr3")&&
c[i+1].getName().equals("pwd3"))||(c[i].getName().equals("usr4")&&
c[i+1].getName().equals("pwd4") ))
{
if((user.equals(c[i].getValue()) && pas.equals(c[i+1].getValue())) )
{
//RequestDispatcher rd=req.getRequestDispatcher("/cart.html");
rd.forward(req,res);
}
else
{
out.println("YOU ARE NOT AUTHORISED USER ");
//res.sendRedirect("/cookdemo/cologin.html");
}
}
}
}
}

 Web.xml:
<web-app>
<servlet>
<servlet-name>him</servlet-name>
<servlet-class>Clogin</servlet-class>
</servlet>
<servlet>
<servlet-name>him1</servlet-name>
<servlet-class>Addcook</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>him</servlet-name>
<url-pattern>/clogin</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>him1</servlet-name>
<url-pattern>/clogin1</url
pattern>/clogin1</url-pattern>
</servlet-mapping>
</web-app>

Output:
2. Read the user id and passwords entered in the Login form and authenticate with the
values (user id and passwords) available in the cookies.
 If he is a valid user (i.e., user-name and password match) you should welcome him by name
(user-name) else you should display “You are not an authenticated user “.
 Use init-parameters to do this. Store the user-names and passwords in the webinf.xml and
access them in the servlet by using the getInitParameters() method.
Program:

 home.html:
<html>
<head>
<title>Authentication</title>
</head>
<body>
<form action="ex1">
<label>Username </label>
<input type="text"size="20" name="user"><br><br>
password<input type="text" size="20" name="pwd"><br><br>
<input type="submit" value="submit">
</form>
</body>
</html>
Example1.java
import javax.servlet.*;
import java.io.*;
public class Example1 extends GenericServlet
{
private String user1,pwd1,user2,pwd2,user3,pwd3,user4,pwd4,user5,pwd5;
public void init(ServletConfig sc)
{
user1=sc.getInitParameter("username1");
pwd1=sc.getInitParameter("password1");

user2=sc.getInitParameter("username2");
pwd2=sc.getInitParameter("password2");

user3=sc.getInitParameter("username3");
pwd3=sc.getInitParameter("password3");

user4=sc.getInitParameter("username4");
pwd4=sc.getInitParameter("password4");
}
Public void service(ServletRequest req,ServletResponse res)throws
ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
user5=req.getParameter("user");
pwd5=req.getParameter("pwd");

if((user5.equals(user1)&&pwd5.equals(pwd1))||(user5.equals(user2)&&pwd5.equals(pwd2))||(us
er5.equals(user3)&&pwd5.equals(pwd3))||(user5.equals(user4)&&pwd5.equals(pwd4)))
out.println("<p> welcome to"+user5.toUpperCase());
else
out.println("You are not authorized user");
}
}

 web.xml:
<web-app>
<servlet>
<servlet-name>Example</servlet-name>
<servlet-class>Example1</servlet-class>
<init-param>
<param-name>username1</param-name>
<param-value>pvpsit</param-value>
</init-param>
<init-param>
<param-name>password1</param-name>
<param-value>cse</param-value>
</init-param>
<init-param>
<param-name>username2</param-name>
<param-value>1234</param-value>
</init-param>
<init-param>
<param-name>password2</param-name>
<param-value>4567</param-value>
</init-param>
<init-param>
<param-name>username3</param-name>
<param-value>cse</param-value>
</init-param>
<init-param>
<param-name>password3</param-name>
<param-value>pvpsit</param-value>
</init-param>
<init-param>
<param-name>username4</param-name>
<param-value>wt</param-value>
</init-param>
<init-param>
<param-name>password4</param
name>password4</param-name>
<param-value>lab</param
value>lab</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Example</servlet
name>Example</servlet-name>
<url-pattern>/ex1</url-pattern>
pattern>
</servlet-mapping>
</web-app>

Output:

You might also like