You are on page 1of 37

Ex.

No :01
Date :
HOTSPOTS IN MAP

Aim:

Create a web page with the following using HTML


a. To embed a map in a web page
b. To fix the hot spots in that map
c. Show all the related information when the hot spots are clicked.

Procedure:

1. Download a map image from internet and save the image with map.jpg
2. Create a webpage using html by embedding map image using <img> tag
3. Identify positions on the map using paint application for creating hotspots
4. Create hotspots on the map images using <map> tag
5. Create web pages for each hotspots
6. Run the application

Program:

map.html
<!DOCTYPE html>
<html>
<head>
<title>INDIA MAP</title>
</head>
<body>
<imgsrc="india.jpg" alt="India-map" usemap="#indiamap">
<map name="indiamap">
<area coords="336,987,50" shape="circle" href="tn.html">
<area coords="261,975,50" shape="circle" href="kr.html">
<area coords="362,792,50" shape="circle" href="an.html">
<area coords="247,644,50" shape="circle" href="ma.html">
</map>
</body>
</html>

kl.html
<!DOCTYPE html>
<html>
<head>
<title>KERALA MAP</title>
</head>
<body>
<imgsrc="kl.jpg" >
</body>
</html>

tn.html
<!DOCTYPE html>
<html>
<head>
<title>TAMIL NADU MAP</title>
</head>
<body>
<imgsrc="tnmap.jpg" >
</body>
</html>
Output:

Result:

Thus a web pages for embedding map were created and tested successfully.
Ex.No :02
Date :
IMPLEMENTATION OF CSS TYPES

Aim:

Create a web page with the following.


a) Cascading style sheets.
b) Embedded style sheets.
c) Inline style sheets. Use our college information for the web pages.

Procedure:

1. Create a webpage using html from displaying college information


2. Create external css file for styling few tags
3. Link the external css using <link> tag
4. Embed a style sheet in a web page using <style> tag
5. Add inline css using style attribute
6. Run the application

Program:

<!DOCTYPE html>
<html>
<head>
<title>CSS Types</title>
<style type="text/css">
<!--2.Internal Stylesheet -->
h3
{
font-family:arial;
color:blue;

}
h5
{
text-align:center;
}
p
{
font-sise:14pt;
font-family:verdana
}
</style>
<!--3.External Stylesheet -->
<link rel="stylesheet" href="ext.css">
</head>
<body>
<h1>
ABC Institute of Technology
</h1>
<h5>
Approved by AICTE, Affiliated to Anna University
</h5>
<hr>
<h2>About Institution:</h2>
<p>
ABC Institue of Technology was established in the 1995 with five undergraduate
engineering courses.
</p>
<!--1. INLINE STYLE SHEET -->
<h2 style="color:blue;">Course Offered: </h2>
<p>
<ul>
<li>B.E.-CSE</li>
<li>B.E.-ECE</li>
<li>B.E.-EEE</li>
<li>B.E.-MECH</li>
<li>B.E.-CIVIL</li>
</ul>
</p>
<h2>Contact:</h2>
<p>mailtoabc@abc.ac.in</p>
</body>
</html>

ext.css
body{
background-color: #f0e68c;
}
h1{
font-family:arial;
color:green;
text-align:center;
}
h2{
font-family:arial;
color:red;
left:20px
}

Output:

Result:
Thus the web pages for implementing various types of CSS were created and tested successfully.
Ex.No :03
Date :
FORM VALIDATION USING JAVASCRIPT

Aim:
To write a program to Validate the Registration, user login, user profile and payment by credit card pages using JavaScript

Procedure:

1. Create form in a webpage using <form>tage for registration


2. Create javascript function inside <script> tag for validating all input fields of the html form
3. Read the values of input fields in the javascript
4. Validate those values
5. Show error message, if input fields are empty.
6. Run the application

Program:
REGISTRATION FORM VALIDATEION
<html>
<head>
<script>
function validate()
{
var name = document.forms["RegForm"]["Name"];
var email = document.forms["RegForm"]["EMail"];
var phone = document.forms["RegForm"]["Telephone"];
var what = document.forms["RegForm"]["Subject"];
var password = document.forms["RegForm"]["Password"];
var address = document.forms["RegForm"]["Address"];

if (name.value == "")
{
window.alert("Please enter your name.");
name.focus();
return false;
}

if (address.value == "")
{
window.alert("Please enter your address.");
address.focus();
return false;
}

if (email.value == "")
{
window.alert("Please enter a valid e-mail address.");
email.focus();
return false;
}

if (phone.value == "")
{
window.alert("Please enter your telephone number.");
phone.focus();
return false;
}

if (password.value == "")
{
window.alert("Please enter your password");
password.focus();
return false;
}

if (what.selectedIndex< 1)
{
alert("Please enter your course.");
what.focus();
return false;
}

return true;
}
</script>
<style>
div
{
box-sizing: border-box;
width: 100%;
border: 100px solid black;
float: left;
align-content: center;
align-items: center;
}
h1
{
text-align: center
}
form
{
margin: 0 auto;
width: 600px;
}
</style>
</head>

<body>
<h1> REGISTRATION FORM </h1>
<form name="RegForm" action="" onsubmit="return validate()" method="post">

<p>Name: <input type="text" size=65 name="Name"></p><br>


<p> Address: <input type="text" size=65 name="Address"></p><br>
<p>E-mail Address: <input type="text" size=65 name="EMail"></p><br>
<p>Password: <input type="text" size=65 name="Password"></p><br>
<p>Telephone: <input type="text" size=65 name="Telephone"></p><br>

<p>Select Your Course


<select type="text" value="" name="Subject">
<option>CSE</option>
<option>ECE</option>
<option>EEE</option>
<option>MECH</option>
<option>CIVIL</option>
</select></p><br><br>
<p>Comments: <textarea cols="55" name="Comment"></textarea></p>
<p><input type="submit" value="SUBMIT" name="Submit">
<input type="reset" value="RESET" name="Reset">
</p>
</form>
</body>
</html>
LOGIN FORM VALIDATION

<!DOCTYPE html>
<html>
<head>
<script>
functionvalidateForm()
{
var un = document.loginform.usr.value;
varpw = document.loginform.pword.value;
var username = "username";
var password = "password";
if ((un == username) && (pw == password))
{
return true;
}
else
{
alert ("Login was unsuccessful, please check your username and password");
return false;
}
}
</script>
</head>
<body>
<h1>LOGIN FORM VALIDATION</h1>

<form name="loginform" onSubmit="return validateForm();" action="ex3.html" method="post">


<label>User name</label>
<input type="text" name="usr" placeholder="username">
<label>Password</label>
<input type="password" name="pword" placeholder="password">
<input type="submit" value="Login"/>
</form>
</body>
</html>

Output:
Result:

Thus programs to Validate the Registration, user login, user profile and payment by credit card pages using JavaScript were created
and tested successfully.
Ex.No :04.a
Date :
INVOKE SERVLET FROM HTML FORM

Aim:

To Write programs in Java using Servlets to Invoke Servlets from HTML Forms Using Servlets

Procedure:

1. Open netbeans IDE


2. Create a project using following:
a. File  New Project  Java Web  Web Application  Type Project Name  Finish
3. Create form in the index.html file which invokes various servlets.
4. Create servlet using following:
a. Right Click on the project name  New  Servlet  Type Servlet Name  Check the check box  Finish
5. Repeat step-4 for creating 3 different servlets
6. Edit servlet program
7. Run Application

Program:

Index.html

<!DOCTYPE html>
<html>
<head>
<title>Invoke Servlet</title>
<style>
div{
text-align: center;
}
</style>
</head>
<body>
<div>
<h1><center>INVOKING SERVLETS FROM HTML</center></h1>
<hr>
<form action="MyServlet1" method="get">
User Name:<input type="text" name="uname"><br><br>
Password:<input type="password" name="pass"><br><br>
<input type="submit" value="Invoke Servlet">
</form>
</div>
</body>
</html>

MyServlet1.java

importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;

public class MyServlet1 extends HttpServlet


{

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throwsServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String uname=request.getParameter("uname");
String pass=request.getParameter("pass");
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet MyServlet1</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>This is Servlet Page</h1><br>");
if(uname.equals("abc")&&pass.equals("123"))
{
out.println("<h2>LOGIN SUCCESS</h2>");
}
else
{
out.println("<h2>LOGIN FAILED</h2>");
}
out.println("</body>");
out.println("</html>");
}
catch(Exception e)
{
System.out.println(e);
}

}
}
Output:

Result:

Thus the programs in Java using Servlets to Invoke Servlets from HTML Forms Using Servlets were created and tested successfully.
Ex.No :04.b
Date :
SESSION TRACKING USING HIDDEN FORM FIELDS

Aim:
To Write programs in Java using Servlets for session tracking using hidden form fileds.

Procedure:

1. Open netbeans IDE


2. Create a project
a. File  New Project  Java Web  Web Application  Type Project Name  Finish
3. Create form in the index.html file which invokes first servlet
4. Create first servlet which invokes second servlet using following:
a. Right Click on the project name  New  Servlet  Type Servlet Name  Check the check box  Finish
5. Edit servlet program to pass the details using hidden form fields from the first servlet.
Eg: <input type='hidden' name='uname'>
6. Create second servlet using step-4
7. Edit servlet program
8. Run Application

Program:

index.html
<html>
<body>
<form action="FirstServlet" method="get">
Name:<input type="text" name="userName"/><br/>
<input type="submit" value="go"/>
</form>
</body>
</html>
FirstServlet.java

import java.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;

public class FirstServlet extends HttpServlet


{
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
try
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("userName");
out.print("Welcome "+n);

//creating form that have invisible textfield


out.print("<form action='SecondServlet'>");
out.print("<input type='hidden' name='uname' value='"+n+"'>");
out.print("<input type='submit' value='go'>");
out.print("</form>");
out.close();
}
catch(Exception e)
{System.out.println(e);}
}

}
SecondServlet.java

import java.io.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
public class SecondServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
try
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();

//Getting the value from the hidden field


String n=request.getParameter("uname");
out.print("Hi"+n);
out.close();
}
catch(Exception e)
{System.out.println(e);}
}
}

Output:
Result:

Thus the programs in Java using Servlets for session tracking using hidden form fileds were created and tested successfully.
Ex.No : 05
Date :

ONLINE EXAMINATION PORTAL USING SERVLET

Aim:
Write programs in Java to create three-tier applications using servlets for conducting online examination for displaying
student mark list. Assume that student information is available in a database which has been stored in a database server.

Procedure:

1. Open netbeans IDE


2. Create a project
a. File  New Project  Java Web  Web Application  Type Project Name  Finish
3. Create a table in Mysqldbms for storing students score
4. Create form in the index.html file for displaying Multiple Choice questions
5. Submit answer to another servlet to calculate score and to store the result into database
6. Create first servlet which invokes second servlet using following:
a. Right Click on the project name  New  Servlet  Type Servlet Name  Check the check box  Finish
7. Edit servlet program to calculate score and to store results into database
Eg: <input type='hidden' name='uname'>
8. Create another servlet to retrieve results from the database and to display on the browser
9. Run Application

Program:

index.html

<!DOCTYPE html>
<html>
<head>
<title>Online Examination</title>
<meta charset="UTF-8">
</head>
<body>
<div>Online Examination</div>
<form name="myform" action="NewServlet1" method="post">
1.Which of the following approach is adapted by C++?<br>
<input type="radio" name="q1" value="topdown">Top-down<br>
<input type="radio" name="q1" value="bottomup">Bottom-up<br>
<input type="radio" name="q1" value="rightleft">Right-Left<br>
<input type="radio" name="q1" value="leftright">Left-Right<br>
<br>
2.Which of the following is not a type of constructor?<br>
<input type="radio" name="q2" value="copy">Copy Constructor<br>
<input type="radio" name="q2" value="default">Default Constructor<br>
<input type="radio" name="q2" value="friend">Friend Constructor<br>
<input type="radio" name="q2" value="parameter">Parameterized Constructor<br>
3.What is the title of the book about Waterloo written by “Simon the Troll”?<br>
<input type="radio" name="q3" value="dream">Dreaming in Technicolor<br>
<input type="radio" name="q3" value="default">Water Under the Bridge<br>
<input type="radio" name="q3" value="friend">Images of Waterloo<br>
<input type="radio" name="q3" value="parameter">Parameterized Constructor<br>
4.Waterloo Counselling Services provides workshops about?<br>
<input type="radio" name="q4" value="study">study skills<br>
<input type="radio" name="q4" value="default">Water Under the Bridge<br>
<input type="radio" name="q4" value="friend">Images of Waterloo<br>
<input type="radio" name="q4" value="parameter">Parameterized Constructor<br>
5.During what period was James Downey the president of Waterloo?<br>
<input type="radio" name="q5" value="copy">1990-1996<br>
<input type="radio" name="q5" value="default">1991-1997<br>
<input type="radio" name="q5" value="year"> 1993-1999<br>
<input type="radio" name="q5" value="parameter">1992-1998<br>
<input type="text" name="rollno" placeholder="Enter your Roll Number">
<input type="text" name="name" placeholder="Enter your name">
<input type="submit" value="Submit">
</form>
</body>
</html>
Servlet1.java

importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjava.sql.*;

public class NewServlet1 extends HttpServlet


{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String rollno=request.getParameter("rollno");
String name=request.getParameter("name");
String q1=request.getParameter("q1");
String q2=request.getParameter("q2");
String q3=request.getParameter("q3");
String q4=request.getParameter("q4");
String q5=request.getParameter("q5");
int score=0;

if(q1.equals("bottomup"))
{
score=score+1;
}
if(q2.equals("friend"))
{
score=score+1;
}
if(q3.equals("dream"))
{
score=score+1;
}
if(q4.equals("study"))
{
score=score+1;
}
if(q5.equals("year"))
{
score=score+1;
}
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet NewServlet1</title>");
out.println("</head>");
out.println("<body>");
Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
PreparedStatementps=con.prepareStatement("insert into student4 values(?,?,?)");
ps.setString(1,rollno);
ps.setString(2,name);
ps.setInt(3,score);
int i=ps.executeUpdate();
if(i>0)
{
out.println("You have completed Exam Successfully!!!");
out.println("<form action='NewServlet2' method='post'>");
out.println("<input type='hidden' name='rollno' value='"+rollno+"'>");
out.println("<input type='submit' value='View Score'>");
out.println("</form>");
}
else
out.println("Failed to insert");
out.println("</body>");
out.println("</html>");
}
catch(Exception e){}
}
}

Servlet2.java
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importjava.sql.*;
public class NewServlet2 extends HttpServlet
{

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


throwsServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
String rollno=request.getParameter("rollno");
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet NewServlet2</title>");
out.println("</head>");
out.println("<body>");
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
Statement stmt=con.createStatement();
ResultSetrs=stmt.executeQuery("select * from student4 where rollno=" +rollno);
while(rs.next())
{
out.println("<center>Your Score is:<\center>"+rs.getInt(3));
}
out.println("</body>");
out.println("</html>");
}
catch(Exception e){}
}

}
Output:

Result:
Thus the programs for conducting online examinations were created and tested successfully.
Ex.No. :06
Date :
SHOPPING CART USING SERVLET

Aim:

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.

Procedure:

1. Open netbeans IDE


2. Create a project
a. File  New Project  Java Web  Web Application  Type Project Name  Finish
3. Create a table in Mysqldbms for storing user information and book details
4. Create form in the index.html file for login
5. Create a SERVLET file(Verify.java) for verifying login information from the web.xml file and store into the cookies using
the following:
a. Right Click on the project name  New  Servlet  Type Servlet Name  Check the check box  Finish
6. Create another SERVLET file (BuyProduct.java) for adding products and its quantity to the cart using cookies.
7. Create another SERVLET file (ShoppingCart.java) for viewing cart from the cookies.
8. Run the application

Program:
Index.html

<!DOCTYPE html>
<html>
<head>
<title>Online Shopping</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
div,h1
{
text-align: center;
}
</style>
</head>
<body>
<h1>ONLINE SHOPPING CART</h1>
<div>
<form method="get" action="Verify">
<input type="text" name="uname" placeholder="Enter User Name"><br><br>
<input type="text" name="pwd" placeholder="Enter Password"><br><br>
<input type="submit" value="Login" name="add">
</form>
</div>
</body>
</html>

Verify.java
import java.io.*;
import java.io.*;
importjavax.servlet.http.*;
importjavax.servlet.*;
public class Verify extends HttpServlet
{

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throwsServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
//Read data from html form
String uname = request.getParameter("uname");
String pwd = request.getParameter("pwd");

//Read data from web.xml


String uname1=getServletContext().getInitParameter("uname");
String pwd1=getServletContext().getInitParameter("pwd");

out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Online Shopping</title>");
out.println("</head>");
out.println("<body>");

if(uname.equals(uname1)&&pwd.equals(pwd1))
{
Cookie c1 = new Cookie("uname", uname);
response.addCookie(c1);
response.sendRedirect("BuyProduct");
}
else
{
response.sendRedirect("index.html");
}
out.println("</body>");
out.println("</html>");
}
catch(Exception e){}
}
}

BuyProduct.java

import java.io.*;
import java.io.*;
importjavax.servlet.http.*;
importjavax.servlet.*;

public class BuyProduct extends HttpServlet


{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter())
{
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Online Shopping</title>");
out.println("</head>");
out.println("<body>");
//Read data from cookies
Cookie clientCookies[] = request.getCookies();
out.println("<h1>Hi "+clientCookies[0].getValue()+"</h1>");

out.println("<form method='get' action='ShoppingCart'>");


out.println("Enter Item Name <input type='text' name='item'><br><br>");
out.println("Enter Item Quantity <input type='text' name='qty'><br><br>");
out.println("<input type='submit' value='Add to Cart' name='add'>");
out.println("<input type='submit' value='View Cart' name='list'>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
catch(Exception e){}
}
}

ShoppingCart.java

importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.*;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;

public class ShoppingCart extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)


throwsServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
String str1 = request.getParameter("item"); // item name
String str2 = request.getParameter("qty"); // item quantity
String str3 = request.getParameter("add"); // submit button by name add
String str4 = request.getParameter("list");
String uname = request.getParameter("uname");
String card=getServletContext().getInitParameter("card");
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Online Shopping</title>");
out.println("</head>");
out.println("<body>");
//Reading data from cookies
Cookie clientCookies[] = request.getCookies();
out.println("<h1>Hi "+clientCookies[0].getValue()+"</h1>");
if(str3 != null)
{
Cookie c1 = new Cookie(str1, str2);
response.addCookie(c1);

response.sendRedirect("BuyProduct");
}
else if(str4 != null)
{
out.println("<br>Your Products:<br>");
for(int i = 1; i <clientCookies.length; i++)
{
out.print("<b>" + clientCookies[i].getName() + " : " + clientCookies[i].getValue() + "</b><br>");
}
out.println("Payment successfull with card number "+card);
}
out.println("</body>");
out.println("</html>");
out.close( ) ;
}
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
processRequest(request, response);
}

@Override
public String getServletInfo() {
return "Short description";
}

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>ShoppingCart</servlet-name>
<servlet-class>ShoppingCart</servlet-class>
</servlet>
<servlet>
<servlet-name>Verify</servlet-name>
<servlet-class>Verify</servlet-class>
</servlet>
<servlet>
<servlet-name>BuyProduct</servlet-name>
<servlet-class>BuyProduct</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ShoppingCart</servlet-name>
<url-pattern>/ShoppingCart</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Verify</servlet-name>
<url-pattern>/Verify</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>BuyProduct</servlet-name>
<url-pattern>/BuyProduct</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<context-param>
<param-name>uname</param-name>
<param-value>abc</param-value>
</context-param>
<context-param>
<param-name>pwd</param-name>
<param-value>123</param-value>
</context-param>
<context-param>
<param-name>card</param-name>
<param-value>123456789</param-value>
</context-param>
</web-app>

Output:
Result:
Thus the programs for online shopping cart using servlet and cookies were created and tested successfully.
Ex.No :07
Date :
ONLINE BOOK STORE USING DATABASE

Aim:
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.

Procedure:

1. Open netbeans IDE


2. Create a project
a. File  New Project  Java Web  Web Application  Type Project Name  Finish
3. Create a table in Mysqldbms for storing user information and book details
4. Create form in the index.html file for login
5. Create a JSP file for verifying login information from the database
a. Right Click on the project name  New  JSP  Type file name  Finish
6. Create another JSP file for displaying books catalogue from the database
7. Run Application

Program:

login.html
<!DOCTYPE html>
<html>
<head>
<title>Signin Page</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
div,h1{
text-align: center;
}
</style>
</head>
<body>
<div>
<h1>Signin Form</h1>
<form action="verify.jsp" method="post">
Name:<input type="text" name="email"><br><br>
Password:<input type="password" name="pass"><br><br>
<input type="submit" value="Login">
</form>
</div>
</body>
</html>

verify.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String email=request.getParameter("email");
String pass=request.getParameter("pass");
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
Statement stmt=con.createStatement();
ResultSetrs=stmt.executeQuery("select * from user");
while(rs.next())
{
if(email.equals(rs.getString(4))&&pass.equals(rs.getString(2)))
{
response.sendRedirect("viewbooks.jsp");
}
else
{
response.sendRedirect("login.html");
}
}
}
catch(Exception e){}
%>
</body>
</html>

viewbooks.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<%@page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>View Books</title>
</head>
<body>
<h1>View Books Page</h1>
<%
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
Statement stmt=con.createStatement();
ResultSetrs=stmt.executeQuery("select * from book");
while(rs.next())
{
out.println(rs.getString(1)+" | "+rs.getString(2)+" | "+rs.getString(3)+" | "+rs.getString(4)+" | $"+rs.getInt(5)+" | "+rs.getString(6)+" |
"+rs.getInt(7));
}
}
catch(Exception e){}
%>
</body>
</html>
Output:

Result:
Thus the programs for online bookstore using jsp were created and tested successfully.
Ex.No. :08
Date :
READING INFORMATION FROM XML

Aim:
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

Procedure:

1. Create student.xml file with information about three students in the web server.
2. Create a html file with javascriptXMLHttpRequest object for accessing data from the XML file located in the web server.
3. Get student roll number from user
4. Display details of particular roll number from the XML file.

Program:

read.xml
<studentdetails>
<student>
<rollno>cs1501</rollno>
<name>Arun</name>
<department>CSE</department>
</student>
<student>
<rollno>cs1502</rollno>
<name>Ashok</name>
<department>CSE</department>
</student>
<student>
<rollno>cs1503</rollno>
<name>Kannan</name>
<department>CSE</department>
</student>
<student>
<rollno>cs1504</rollno>
<name>Dinesh</name>
<department>CSE</department>
</student>
</studentdetails>

readxml.html

<!DOCTYPE html>
<html>
<body>
<form name="myform" onsubmit="readXMLData()" method="get">
Enter Roll Number:<input type="text" name="rollno">
<input type="Submit" value="Submit">
</form>
<p id="demo"></p>
<script>
functionreadXMLData()
{
varxhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
if (this.readyState == 4 &&this.status == 200)
{
myFunction(this);
}
};
xhttp.open("GET", "student.xml", true);
xhttp.send();
}
functionmyFunction(xml)
{
var x, i, xmlDoc;
xmlDoc = xml.responseXML;
roll=document.forms['myform'].elements['rollno'].value;
x = xmlDoc.getElementsByTagName("student");
for (i = 0; i <x.length; i++)
{
roll1=x[i].getElementsByTagName("rollno")[0].childNodes[0].nodeValue;
if(!(roll.localeCompare(roll1)))
{
document.write("<br>");
document.write("<br>Roll No.:"+x[i].getElementsByTagName("rollno")[0].childNodes[0].nodeValue);
document.write("<br>Name:"+x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
document.write("<br>Department:"+x[i].getElementsByTagName("department")[0].childNodes[0].nodeValue);
}
}
document.getElementById("demo").innerHTML = txt;
}
</script>
</body>
</html>
Output:

Result:
Thus the programs to read the information from xml document were created and tested successfully.
Ex.No. :09.a
Date :
PHP REGULAR EXPRESSION

Aim:
To write a PHP program to validate the form using regular expression

Procedure:

1. Create a form using html for getting email from user at server
2. Create regular expression in php file for validating email at server
3. Run the html file and pass email id
4. Check your email id valid or invalid

Program:

email.html
<!DOCTYPE html>
<html>
<head>
<title>Email Validation</title>
<style>
h1,div{text-align:center;}
</style>
</head>
<body>
<h1>Email Validation using Regular Expression</h1>
<div>
<form action="emailvalidate.php" method="get">
Email:<input type="text" name="email"><br><br>
<input type="submit" value="Submit">
</form>
</div>
</body>
</html>

emailvalidate.php
<?php
$email=$_GET['email'];
functionvalid_email($str)
{
$patt="/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix";
return (!preg_match($patt, $str)) ? FALSE : TRUE;
}
if(valid_email($email))
{
echo "valid email address.";
}
else
{
echo "Invalid email address.";
}
?>
Output:

Result:
Thus a program to validate form data using regular expression was created and tested successfully.
Ex.No. :09.b
Date :
PHP DATABASE ACCESS

Aim:
To write a PHP program to stores a form data into database

Procedure:
1. Create table student with rollno, name and age attributsin mysql database in the following url:
http://localhost/phpmyadmin
2. Create a form using html for collecting student details from the user
3. Create a php file for storing student details
4. Pass the student details from html form to php file
5. Check the database after storing details

Program:

insert.html
<!DOCTYPE html>
<html>
<head>
<title>PHP Insert Form Data</title>
<style>
h1,div
{
text-align:center;
}
</style>
</head>
<body>
<h1>Insert Student Details</h1>
<div>
<form name="myform" action="insert.php" method="get">
Name:<input type="text" name="uname"><br><br>
Roll Number:<input type="text" name="uroll"><br><br>
Age:<input type="text" name="uage"><br><br>
<input type="submit" value="Insert">
</form>
</div>
</body>
</html>

insert.php

<?php
$rollno=$_GET['uroll'];
$name=$_GET['uname'];
$age=$_GET['uage'];
// Create connection
$conn = new mysqli("localhost", "", "", "test");
// Check connection
if ($conn->connect_error)
{
die("Connection failed:" . $conn->connect_error);
}

$sql = "INSERT INTO STUDENT VALUES('$rollno','$name','$age')";

if(mysqli_query($conn, $sql))
{
echo "Records inserted successfully.";
}
else
{
echo "ERROR: Could not execute $sql. " . mysqli_error($conn);
}
$conn->close();
?>

Output:

Result:
Thus a program to validate form data using regular expression was created and tested successfully.
Ex.No. :10
Date :
WEB SERVICE

Aim:
To create a web service for adding two integer values in netBeans IDE.

Procedure:

1. CREATING, DEPLOYING AND TESTING WEB SERVICE


a. Open netbeans IDE
b. Create a new java web application project
i. File  New Project  Java Web  Web Application  Type Project Name  Finish
c. Right click on the project select NewWeb Service  type Web Service Name, type package name and check
the check box
d. Go to the design Tab and select existing operation and remove the operation
e. Click Add Operation button and give the details about operation name, return type and its argument list
f. Move to the source tab and add coding the operation
g. Right click on the project select deploy

2. CONSUMING A WEB SERVICE


a. Create a new java application project
b. Right click on the project and select web service client
c. Choose web service from the browse button
d. Expand web service references folder
e. Drag and drop the operation below the main function
f. Now call the web service function by passing appropriate arguments.

Program:

CalculatorWS.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
packageorg.me.calculator;

importjavax.jws.WebService;
importjavax.jws.WebMethod;
importjavax.jws.WebParam;
importjavax.ejb.Stateless;

@WebService(serviceName = "CalculatorWS")
@Stateless()
public class CalculatorWS {

/**
* Web service operation
*/
@WebMethod(operationName = "add")
publicint add(@WebParam(name = "a") int a, @WebParam(name = "b") int b) {
//TODO write your implementation code here:
int c=a+b;
return c;
}
}
CalculatorWSClient.java
packagecalculatorwsclient;

public class CalculatorWSClient {

public static void main(String[] args) {


// TODO code application logic here
try
{
int a=10;
int b=50;
int res=add(a,b);
System.out.println("a+b:"+res);
}
catch(Exception e)
{
System.out.println(e);
}
}

private static int add(int a, int b)


{
org.me.calculator.CalculatorWS_Service service = new org.me.calculator.CalculatorWS_Service();
org.me.calculator.CalculatorWS port = service.getCalculatorWSPort();
returnport.add(a, b);
}

Output:
Result:
Thus a web service for adding two integer values was created in netbeans IDE and java application for consuming the web service was
demonstrated successfully.

You might also like