Professional Documents
Culture Documents
PROBLEM-1: VALIDATION.
DESCRIPTION: Write JavaScript to validate the following fields of the registration page.
1. Name (Name should contains alphabets and the length should not be less than 6 characters).
2. Password (Password should not be less than 6 characters length).
3. E-mail id (should not be invalid and must follow the standard pattern)
(name@domain.com)
4. Phone number (Phone number should contain 10 digits only).
Note: You can also validate the login page with these parameters.
CODE:
//Registation.html
<html>
<head>
<title>Registration</title>
<script>
function abc()
{
error="";
var re1=new RegExp("^[\\w]{6,}$");
var user=document.f.user.value;
if(!(re1.test(user)))
error+="username should contain 6 chars\n";
var re2=new RegExp("^[\\w]{6,}$");
varpwd=document.f.pwd.value;
if(!(re2.test(pwd)))
error+= "password should conatinatleast 6chars\n";
var re3=new RegExp("^[\\w]+@[\\w]+.co(m|.in)$");
var email=document.f.email.value;
if(!(re3.test(email)))
error+= "email should be in the form name@domain.com\n";
var re4=new RegExp("^[\\d]{10}$");
var phone=document.f.phone.value;
if(!(re4.test(phone)))
error+= "phonenumber should cotain 10 digits";
if(error!="")
window.alert(error)
else
window.alert("Succsfull Registration");
return
}
</script>
</head>
<body >
<center>
<form name=f>
<table align=center>
<caption align=center> REGISTRATION </caption>
<tr>
<td>USERNAME :</td>
<td ><input type=text name=user></td>
<td><p id= "uname"></p></td>
</tr>
<tr>
<td>PASSWORD:</td>
<td ><input type=password name=pwd></td>
<td><p id= "pass"></p></td>
</tr>
<tr>
<td>E-mail id:</td>
<td ><input type=text name=email ></td>
<td><p id= "emai"></p></td>
</tr>
<tr>
<td>Phone Number:</td>
<td ><input type=text name=phone ></td>
<td><p id= "ph"></p></td>
</tr>
<tr>
<td><input type=submit value="submit" onclick="abc()"></td>
<td><input type=reset value="reset"></td>
</tr>
</table>
</form>
</body>
</html>
OUTPUT SCREENS:
PROBLEM-2: CSS.
DESCRIPTION: Design a web page using CSS (Cascading Style Sheets) which includes the following:
1. Use different font, styles: In the style definition you define how each selector should work
(font, color etc.).Then, in the body of your pages, you refer to these selectors to activate the
styles.
2. Set a background image for both the page and single elements on the page.
3. You can define the background image for the page like this:
a) Control the repetition of the image with the background-repeat property.
b) As background-repeat: repeat
c) Define styles for links as
d) Work with layers:
e) Add a customized cursor:
Selector {cursor:value}
.xlink{cursor:crosshair}
.hlink{cursor:help}
CODE:
//Font.html
<html>
<head.
<title> styles</title>
<style type="text/css">
B.headline {color:red;font-size:22pt;font-family:arial;text-decoration:none}
</style>
</head>
<body>
<b> this is normal bold</b><br />
<b class="headline"> this is headline style bold</b>
</body>
</html>
Cursot.html
<html>
<head>
<style type="text/css">
.xlink {cursor: crosshair}
.hlink { cursor: help}
</style>
</head>
<body>
<b>
<a href="1.html" class="xlink"> CROSS LINK</a><br />
<a href="1.html" class="hlink" > HELP LINK </a><br />
</body>
</html>
Image.html
<html>
<head>
<style>
body{
background-image:url(1.jpg);}
table
{
background-image:url(3.jpg);}
</style>
<body >
<table border=2>
<tr>
<td> col1</td>
<td> col2 </td>
</tr>
<tr>
<td>raju</td>
<td>ubuntu</td>
</tr>
</table>
</body>
</html>
Background.html
<html>
<head>
<style>
body
{
background-image:url(6.jpg);
background-repear:repeat;
}
</style>
</head>
</body>
<h2> this is the body of the page where the image is repeated</h2>
</body>
</html>
Links.html
<html>
<head>
<style type="text/css">
a.link {text-decoration:underline}
a.visited{ text-decoration:overline}
a.active{ text-decoration:none}
a.hover{ text-decoration:overline; color=red}
</style>
</head>
<body>
<h2> these are links </h2>
<a href=1.html>LINK1</a><br />
<a href=2.html>LINK2</a><br />
<a href=3.html>LINK3</a><br />
<a href=4.html>LINK4</a><br />
</body>
</html>
Layers.html
<html>
<head>
<title> WORKING WITH LAYERS</TITLE>
</head>
<body>
<div style="position: absolute;top: 50px;left: 5px;z-index: 2;">LAYER 1 </div>
<div style="position: absolute;top: 50px;left: 5px; z-index: 1;color=”blue”">raju</div>
</body>
</html>
OUTPUT SCREENS:
AIM: To write an XML file which will display the Book information which includes the following:
1. Title of the book
2. Author Name
3. ISBN number
4. Publisher name
5. Edition
6. Price
DESCRIPTION: Write a Document Type Definition (DTD) to validate the above XML file.
Display the XML file as follows:
1. The contents should be displayed in a table. The header of the table should be in color
GREY.
2. And the Author names column should be displayed in one color and should be capitalized
and in bold.
3. Use your own colors for remaining columns.
4. Use XML schemas XSL and CSS for the above purpose.
Note: Give at least for 4 books. It should be valid syntactically.
Hint: You can use some xml editors like XML-spy.
CODE:
Books.xml
<?xml:stylesheet type="text/XSL" href="books.xsl" ?>
<!DOCTYPE books SYSTEM "books.dtd">
<books>
<book isbn="1342">
<title>Java2</title>
<author>schidth</author>
<publisher>tata</publisher>
<edition>5</edition>
<price>$500</price>
</book>
<book isbn="1242">
<title>HTML</title>
<author>Dietel</author>
<publisher>tata</publisher>
<edition>7</edition>
<price>$300</price>
</book>
</books>
Books.dtd
<!ELEMENT books (book+)>
<!ELEMENT book (title,author,publisher,edition,price)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT publisher (#PCDATA)>
<!ELEMENT edition (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST book isbn CDATA #REQUIRED>
Books.xsl
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<table border="1">
<caption> Books information</caption>
<tr>
<th><font color="grey">Title</font></th>
<th><font color="grey">Author</font></th>
<th><font color="grey">ISBN</font></th>
<th><font color="grey">Publisher</font></th>
<th><font color="grey">Edition</font></th>
<th><font color="grey">Price</font></th>
</tr>
<xsl:variable name="lower">abcdefghijklmnopqrstuvwxyz</xsl:variable>
<xsl:variable name="upper">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>
<xsl:for-each select="books/book">
<tr>
<td><font color="red"><xsl:value-of select="title"/></font></td>
<td><font color="green"><xsl:value-of select="translate(author,$lower,$upper)"/></font></td>
<td><font color="blue"><xsl:value-of select="@isbn"/></font></td>
<td><font color="red"><xsl:value-of select="publisher"/></font></td>
<td><font color="red"><xsl:value-of select="edition"/></font></td>
<td><font color="red"><xsl:value-of select="price"/></font></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
OUTPUT SCREEN:
PROBLEM-4: BEANS:
AIM: 1. Create a simple visual bean with area filled with a color.
2. Create a JavaBean which gives the exchange value of INR (Indian Rupees) into equivalent
American/Canadian/Australian Dollar Value.
3. Create two simple Bean with label – which is the count of number of clicks. Than create a
Bean Info class such that only the “count” property is visible in the Property Window.
DESCRIPTION:
1. The shape of the area depends on the property shape. If it is set to true then the shape of the
area is Square and it is Circle, if it is false.
2. The color of the area should be changed dynamically for every mouse click. The color
should also be changed if we change the color in the “property window”.
CODE:
//Colros.java
package sunw.demo.colors;
import java.awt.*;
import java.awt.event.*;
public class Colors extends Canvas {
private Color color;
private boolean rectangular;
// The zero argument constructor defining an anonymous inner class
public Colors() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
changeColor();
}
}
setSize(150,150);
changeColor();
}
// end of default constructor
public void changeColor() {
color = randomColor();
repaint();
}
public void setRectangular(boolean flag)
{
this.rectangular=flag;
repaint();
}
public booleangetRectangular()
{
return rectangular;
}
private Color randomColor() {
int a = (int)( 255*Math.random());
int b = (int)( 255*Math.random());
int c = (int)( 255*Math.random());
return new Color(a, b, c);
}
public void paint(Graphics g) {
Dimension d = getSize();
int h = d.height;
int w = d.width;
g.setColor(color);
if(rectangular)
g.fillRect(0,0,w-1, h-1);
else
g.fillOval(0,0,w-1, h-1);
}
}
ColrosBeanInfo.java
package sunw.demo.colors2;
import java.beans.*;
public class ColorsBeanInfo extends SimpleBeanInfo
{
public PropertyDescriptor[] getPropertyDescriptors()
{
try
{
PropertyDescriptorrectangular=new popertyDescriptor("rectangular",Colors.class);
PropertyDescriptorpd[]={rectangular};
return pd;
}
catch(Exception e)
{
}
return null;
}
}
OUPUT SCREEN:
2. Write two simple Bean with label – which is the count of number of clicks. Than create a Bean
Info class such that only the “count” property is visible in the Property Window.
AIM: Create two simple Bean with label – which is the count of number of clicks. Than create a Bean
Info class such that only the “count” property is visible in the Property Window.
CODE:
//Count.java
package sunw.demo.count;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Count extends JPanel implements ActionListener
{
private String name="box";
int label=0,len=200,i=0;
Button btCount=new Button("Count");
TextFieldtfCount=new TextField(4);
Label lCount=new Label("Count");
public Count()
{
add(lCount);
add(tfCount);
add(btCount);
btCount.addActionListener(this);
btCount.setBackground(Color.orange);
setBackground(Color.green);
setSize(200,200);
setLayout(new GridLayout(2,1,5,10));
}
public void actionPerformed(ActionEventae)
{
String ac=ae.getActionCommand();
if(ac.equals("Count"))
{
setLabel(++i);
tfCount.setText(new Integer(i).toString());
}
}
public intgetLabel()
{
return label;
}
public void setLabel(int label)
{
this.label = label;
}
public intgetLen()
{
return len;
}
public void setLen(intlen)
{
this.len = len;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
}
countBeanInfo.java
package sunw.demo.count;
import java.beans.*;
public class CountBeanInfo extends SimpleBeanInfo
{
public PropertyDescriptor[] getPropertyDescriptors()
{
try
{
PropertyDescriptor label = new PropertyDescriptor("label", Count.class);
PropertyDescriptorpd[] = {label};
return pd;
}
catch(Exception e) {
}
return null;
}
}
OUPUT SCREEN:
3. Write a JavaBean which gives the exchange value of INR(Indian Rupees) into equivalent
American/Canadian/Australian Dollar Value.
AIM: Create a JavaBean which gives the exchange value of INR(Indian Rupees) into equivalent
American/Canadian/Australian Dollar Value.
CODE:
Currency.java
package sunw.demo.currency;
public class Currency
{
double inr;
double eq,ausd,cand;
public Currency()
{
System.out.println("constructing currency coneverter");
}
public void setInr(double c)
{
System.out.println("setting currency to"+c);
this.inr=c;
convert(c);
}
public double getEq()
{
return eq;
}
public void setEq(double eq)
{
this.eq=eq;
}
public double getAusd()
{
return ausd;
}
public void setAusd(double ausd)
{
this.ausd = ausd;
}
public double getCand()
{
return cand;
}
public void setCand(double cand)
{
this.cand = cand;
}
public void convert(double r)
{
eq=r/40;
ausd=r/33.6;
cand=r/38.9;
setEq(eq);
setAusd(ausd);
setCand(cand);
}
public double getInr()
{
return inr;
}
}
OUPUT SCREEN:
AIM: To design dynamic web pages required for online book store?
DESCRIPTION: Access the above developed static web pages for books web site, using these servers
by putting the web pages developed in PROBLEM-1 and PROBLEM-2 in the document root.
Access the pages by using the urls: http://localhost:8080/books.html (for Apache).
CODE:
Frameset.html
<html>
<head>
<title>frame set</title>
</head>
<frameset rows="10%,*">
<frame src="frame1.html"name="frame1">
<frameset cols="10%,*">
<frame src="index.html" name="index" noresize>
<frame src="frame2.html" name="frame2">
</frameset>
</frameset>
</html>
Logo.html
</html>
<head>
<title>logo</title>
</head>
<body bgcolor="yellow">
<imgsrc="a.jpg" width=50 height=50 align=left>
<center><font size=6 color="green"> ONLINE BOOK STORE</font>
</center></br>
<pre>
</pre>
<font face="copperplate Gothic Bold" size=5>
<pre>
<a href=main.html target="frame2">HOME</a>
<a href=login.html target="frame2">LOGIN</a>
<a href=registration.html target="frame2">REGISTRATION</a>
<a href=catalog.html target="frame2">CATALOG</a>
<a href=cart.html target="frame2">CART</a>
</pre>
</BODY>
</HTML>
Links.html
<html>
<head>
<title>Links</title>
</head>
<body bgcolor="#999999">
<a href=cse.html alt="raju"> CSE</a></br>
<a href=ece.html target="frame2">CSE</a></br>
<a href=eee.html target="frame2"> CSE</a></br>
<a href=mech.html target="frame2"> CSE</a></br>
</body>
</html>
Main.html
<html>
<head>
<title>Links</title>
</head>
<body bgcolor="black">
<font color="gree".
<pre>
</pre>
<center>
<p>
This is information about the site
</p></center>
</body>
</html>
HomePage.html
<html>
<head>
<title>Home Page</title>
</head>
<frameset rows="20%,*">
<frame src="logo.html"name="top" noresize>
<frameset cols="15%,*">
<frame src="navi.html" name="left" noresize>
<frame src="main.html" name="frame2" noresize>
</frameset>
</frameset>
</html>
Catalog.html
<html>
<head>
<title>Catalog</title>
</head>
<body>
<table border=1 align=center width=700 height=250>
<tr>
<td><imgsrc="a.jpg" align=center width=200 height=150>
<td>Book:xml Bible<br>
Author:winston<br>
publisher:wiley<br>
</td>
<td> $40</td>
<td><input type=button value="add to cart" name=cart></td>
</tr>
<tr>
<td><imgsrc="shingles.gif" width=200 height=150>
<td>Book:AI<br>
Author:B-russel<br>
publisher:Princeton<br>
</td>
<td> $50</td>
<td><input type=button value="add to cart" align=cnetername=cart></td>
</tr>
<tr>
<td><imgsrc="sky_2.gif" width=200 height=150>
<td> Book:Java2<br>
Author:watson<br>
publisher:wiley<br>
</td>
<td> $60</td>
<td><input type=button value="add to cart" name=cart></td>
</tr>
</table>
</body>
</html>
Cart.html
<html>
<head>
<title> cart</title>
</head>
<body>
<table align="center" width=500 height=250 >
<head>
<th><font color=green> Book Name </font></th>
Registration.html
<html>
<head>
<title>Registration</title>
</head>
<body >
<center>
<table align=centercellspacing=10 cellpadding=5>
<caption align=center> REGISTRATION </caption>
<tr>
<td>USERNAME :</td>
<td ><input type=text name=login></td>
<td></td>
<td></td>
</tr>
<tr>
<td>PASSWORD:</td>
<td>Languages</td>
</tr>
<tr>
<td></td>
<td><input type=checkbox name=lgroup>Telugu</td>
<td><input type=checkbox name=lgroup>Hindi</td>
<td></td>
</tr>
<tr>
<td></td>
<td><input type=checkbox name=lgroup>English</td>
<td><input type=checkbox name=lgroup>Tamil</td></tr>
<tr>
<td> Address</td><td><textarea rows=5 columns=10></textarea></td></tr>
<tr>
<td><input type="submit" value="submit"></td>
<td><input type="reset" value="clear"></td>
</tr>
</body>
</html>
CODE:
<web-app>
<servlet>
<servlet-name>a</servlet-name>
<servlet-class>Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>a</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>b</servlet-name>
<servlet-class>Validate</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>b</servlet-name>
<url-pattern>/validate</url-pattern>
</servlet-mapping>
</web-app>
Login.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Login extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// Get parameter from HTTP request.
String name = request.getParameter("name");
String pass = request.getParameter("pass");
// Create cookie.
Cookie cookie1 = new Cookie("user1", "pwd1");
Cookie cookie2 = new Cookie("user2", "pwd2");
Cookie cookie3 = new Cookie("user3", "pwd3");
Cookie cookie4 = new Cookie("user4", "pwd4");
// Add cookie to HTTP response.
response.addCookie(cookie1);
response.addCookie(cookie2);
response.addCookie(cookie3);
response.addCookie(cookie4);
// Write output to browser.
response.setContentType("text/html");
PrintWriterpw = response.getWriter();
pw.println("<form method=get action='validate'>USERNAME:<input type=text name='name'
></br>PASSWORD:<input type=password name='pass'></br></br><input type='submit'
value='submit'><input type='reset' value='clear'></form>");
pw.close();
}
}
Validate.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Validate extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name = request.getParameter("name");
String pass = request.getParameter("pass");
boolean valid=false;
Cookie[] cookies = request.getCookies();
if(cookies!=null){
for(int i = 0; i <cookies.length; i++)
if(name.equals(cookies[i].getName()))
{
if(pass.equals(cookies[i].getValue()))
valid=true;
else
{
break;
}}
if(valid)
out.println("<h1>Valid User</h1>");
else
out.println("<h1>InValid User</h1>");
}
out.close();
}
}
OUTPUT SCREENS:
CODE:
login.html
<html>
<head>
<title>LOGIN</title>
</head>
<body bgcolor=blue>
<center>
</br>
</br>
</br>
</br>
<form method=get action="validate">
USERNAME:<input type=text name=name></br>
<p>PASSWORD:<input type=password name=pass></br></p>
</br>
<p><pre><input type="submit" value="submit"><input type="reset" value="clear"></p>
</pre>
</form>
</body>
</html>
web.xml
<web-app>
<servlet>
<servlet-name>a</servlet-name>
<servlet-class>Validate</servlet-class>
<init-param>
<param-name>user1</param-name>
<param-value>pwd1</param-value>
</init-param>
<init-param>
<param-name>user2</param-name>
<param-value>pwd2</param-value>
</init-param>
<init-param>
<param-name>user3</param-name>
<param-value>pwd3</param-value>
</init-param>
<init-param>
<param-name>user4</param-name>
<param-value>pwd4</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>a</servlet-name>
<url-pattern>/validate</url-pattern>
</servlet-mapping></web-app>
Validate.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Validate extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriterpw = response.getWriter();
String name = request.getParameter("name");
String pass = request.getParameter("pass");
ServletConfigsc=getServletConfig();
if(pass.equals(sc.getInitParameter(name)))
pw.println("Valid user");
else
pw.println("InValid user");
pw.close();
}
}
OUTPUT SCREENS:
DESCRIPTION:
1. Create a table which should contain at least the following fields: name, password, email-id,
phone number (these should hold the data from the registration form).
2. Practice 'JDBC' connectivity.
3. Write a java program servlet/JSP to connect to that database and extract data from the tables and
display them. Experiment with various SQL queries.
4. Insert the details of the users who register with the web site, whenever a new user clicks the
submit button in the registration page (PROBLEM2).
CODE:
import java.io.*;
import java.sql.*;
public class Display
{
public static void main(String a[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsn","system","system");
Statement stmt=con.createStatement();
ResultSetrs=stmt.executeQuery("select * from registration");
while(rs.next())
System.out.println("UserName: "+rs.getString(1)+" Email: "+rs.getString(3)+" Phone:
"+rs.getInt(4));
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
OUTPUT SCREEN:
CODE:
web.xml
<web-app>
<servlet>
<servlet-name>a</servlet-name>
<servlet-class>ServletDisplay</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>a</servlet-name>
<url-pattern>/servletdisplay</url-pattern>
</servlet-mapping>
</web-app>
ServletDisplay.java
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletDisplay extends HttpServlet
{
public void doGet(HttpServletRequestreq,HttpServletResponseres)throws
ServletException,IOException
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsn","system","system");
Statement stmt=con.createStatement();
res.setContentType("text/html");
PrintWriter out=res.getWriter();
ResultSetrs=stmt.executeQuery("select * from registration");
while(rs.next())
out.println("UserName:"+rs.getString(1)+"Email:"+rs.getString(3)+"Phone:+rs.getLong
(4)+"</br></br>");
}
catch(Exception e){e.printStackTrace();
}
}
}
OUTPUT SCREEN:
CODE:
OUTPUT SCREEN:
CODE:
Registration.html
<html><head><title>Registration</title></head>
<body bgcolor="orange">
<center>
<form method=get action="register">
<table cellspacing=10 cellpadding=5>
<caption> REGISTRATION </caption>
<tr>
<td>USERNAME :</td>
<td><input type=text name=name></td></tr>
<tr>
<td>PASSWORD:</td>
<td><input type=password name=pass></td></tr>
<tr>
<td>E-mail id:</td>
<td><input type=text name=email></td>
</tr>
<tr>
<td>Phone Number:</td>
<td><input type=text name=phone></td>
</tr><tr>
<td><input type=submit value=submit>
</td><td><input type=reset value=clear>
</table></form></center></body></html>
web.xml
<web-app>
<servlet>
<servlet-name>date</servlet-name>
<servlet-class>Registration</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>date</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
</web-app>
Registartion.java
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Registration extends HttpServlet
{
public void doGet(HttpServletRequestreq,HttpServletResponse res)throws
ServletException,IOException
{
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsn","system","system");
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String name=req.getParameter("name");
String pass=req.getParameter("pass");
String email=req.getParameter("email");
String ph=req.getParameter("phone");
int phone=Integer.parseInt(ph);
Statement stmt=con.createStatement();
intn=stmt.executeUpdate("insertintoregistrationvalues('"+name+"','"+pass+"','"+email+"',
"+phone+")");
if(n>0)
out.println("Inserted Successfully");
else
out.println("Insertion Failed");
}
catch(Exception e){e.printStackTrace();}
}
}
OUTPUT SCREENS:
DESCRIPTION:
1. Insert the details of the 3 or 4 users who register with the web site (PROBLEM 6) by using
registration form.
2. Authenticate the user when he submits the login form using the user name and password
from the database (similar to PROBLEM 7 instead of cookies).
Reigistration.html
<html>
<head>
<title>Registration</title>
</head>
<body bgcolor="orange">
<center>
<form method=get action="register.jsp">
<table cellspacing=10 cellpadding=5>
<caption> REGISTRATION </caption>
<tr>
<td>USERNAME :</td>
<td><input type=text name=name></td>
</tr>
<tr>
<td>PASSWORD:</td>
<td><input type=password name=pass></td>
</tr>
<tr>
<td>E-mail id:</td>
<td><input type=text name=email></td>
</tr>
<tr>
<td>Phone Number:</td>
<td><input type=text name=phone></td>
</tr>
<tr>
<td><input type=submit value=submit>
</td>
<td><input type=reset value=clear>
</table>
</form>
</center>
</body>
</html>
Login.html
<html>
<head>
<title>LOGIN</title>
</head>
<body bgcolor=PINK>
<center>
</br>
</br>
</br>
</br>
<form method=get action="validate.jsp">
USERNAME:<input type=text name=name></br>
<p>PASSWORD:<input type=password name=pass></br></p>
</br>
<p><pre><input type="submit" value="submit"><input type="reset" value="clear"></p>
</pre>
</form>
</body>
</html>
register.jsp
<%@ page import="java.sql.*"%>
<%
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsn","system","system");
response.setContentType("text/html");
String name=request.getParameter("name");
String pass=request.getParameter("pass");
String email=request.getParameter("email");
String ph=request.getParameter("phone");
int phone=Integer.parseInt(ph);
Statement stmt=con.createStatement();
int n=stmt.executeUpdate("insert into
registrationvalues('"+name+"','"+pass+"','"+email+"',"+phone+")");
if(n>0)
out.println("Inserted Successfully<br><a href='login.html'>Click Here </a>to login");
else
out.println("Insertion Failed");
}
catch(Exception e){e.printStackTrace();}
%>
validate.jsp
<%@ page import="java.sql.*"%>
<%
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsn","system","system");
response.setContentType("text/html");
boolean valid=false;
String name=request.getParameter("name");
String pass=request.getParameter("pass");
Statement stmt=con.createStatement();
ResultSetrs=stmt.executeQuery("select * from registration");
while(rs.next())
{
if(name.equals(rs.getString(1)))
if(pass.equals(rs.getString(2)))
{out.println("<h1>Welcome "+ rs.getString(1)+"<h1>");
valid=true;break;}
}
if(!valid)
out.println("Invalid User");
}
catch(Exception e){out.println(e);
}
%>
OUTPUT SCREENS:
DESCRIPTION:
1. Create tables in the database which contain the details of items (books in our case like Book
name, Price, Quantity, Amount) of each category.
2. Modify your catalogue page (PROBLEM 2) in such a way that you should connect to the
database and extract data from the tables and display them in the catalogue page using JDBC.
CODE:
create table books (bid number primary key, bimage varchar2(30), bname varchar2(30), bauthor
varchar2(30), bpublisher varchar2(30), bprice number);
insert into books values(1,'css.jpg','css and html','dietel n dietel','phi',30);
insert into books values(2,'servlet.jpg','servlets','chris bates','orielly',40);
insert into books values(3,'head first.jpg','java','schildth','phi',50);
select * from books;
2 rows returned in 0.02 seconds CSV Export create table selected (name varchar2 (30) references
registration(name) ,bid number references books(bid));
Catalog.jsp
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsn","system","system");
Statement stmt=con.createStatement();
response.setContentType("text/html");
ResultSetrs=stmt.executeQuery("select * from books");
out.println("<table width='90%'>");
while(rs.next())
{
out.println("<form method=get action='cartpage'><input type=hidden name=bid
value='"+rs.getString(1)+"'></input><tr><td><imgsrc='"+rs.getString(2)+"'/><td>name:
"+rs.getString(3)+"<br>author:"+rs.getString(4)+"<br>publisher:"+rs.getString(5)+"<td>
price"+rs.getInt(6)+"<td><inputtype=submit value=addtoCart></td></tr></form>");
out.println("");
}
out.println("</table>");
}
catch(Exception e){}
%>
DESCRIPTION:
1. HTTP is a stateless protocol. Session is required to maintain the state.
2. The user may add some items to cart from the catalog page.
3. He can check the cart page for the selected items.
4. He may visit the catalogue again and select some more items.
5. Here our interest is the selected items should be added to the old cart rather than a new cart.
6. Multiple users can do the same thing at a time (i.e., from different systems in the LAN using the
IP-address instead of localhost).
7. This can be achieved through the use of sessions.
8. Every user will have his own session which will be created after his successful login to the
website.
9. When the user logs out his session should get invalidated (by using the method
session.invalidate()).
10. Modify your catalogue and cart JSP pages to achieve the above mentioned functionality using
sessions.
CODE:
register.jsp
<%@ page import="java.sql.*"%>
<%
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsn","system","system");
response.setContentType("text/html");
String name=request.getParameter("name");
String pass=request.getParameter("pass");
String email=request.getParameter("email");
String ph=request.getParameter("phone");
int phone=Integer.parseInt(ph);
Statement stmt=con.createStatement();
int n=stmt.executeUpdate("insert into registration
values('"+name+"','"+pass+"','"+email+"',"+phone+")");
if(n>0)
out.println("Inserted Successfully<br><a href='login.html'>Click Here </a>to login");
else
out.println("Insertion Failed");
}
catch(Exception e){e.printStackTrace();}
%>
login.jsp
<%@ page import="java.sql.*"%>
<%
try
{
boolean valid=false;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsn","system","system");
response.setContentType("text/html");
String name=request.getParameter("name");
String pass=request.getParameter("pass");
Statement stmt=con.createStatement();
ResultSetrs= stmt.executeQuery("select * from registration");
RequestDispatcherrd;
while(rs.next())
{
if(name.equals(rs.getString(1))&&pass.equals(rs.getString(2)))
valid=true;
}
if(valid)
{
HttpSessionhs=request.getSession();
hs.setAttribute("name",name);
rd=request.getRequestDispatcher("catalog.jsp");
rd.forward(request,response);
//out.println("Login Succeful");
}
else
{
out.println(" Not succesful");
}}
catch(Exception e){
out.println(e);
}
%>
validate.jsp
<%@ page import="java.sql.*"%>
<%
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsn","system","system");
response.setContentType("text/html");
boolean valid=false;
String name=request.getParameter("name");
String pass=request.getParameter("pass");
Statement stmt=con.createStatement();
ResultSetrs=stmt.executeQuery("select * from registration");
while(rs.next())
{
if(name.equals(rs.getString(1)))
if(pass.equals(rs.getString(2)))
{out.println("<h1>Welcome "+ rs.getString(1)+"<h1>");
valid=true;break;}
}
if(!valid)
out.println("Invalid User");
}
catch(Exception e)
{
out.println(e);
}
%>
cart.jsp
<%@ page import="java.sql.*"%>
<%
try
{
boolean valid=false;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsn","system","system");
response.setContentType("text/html");
HttpSessionhs=request.getSession();
String uid=(String)hs.getAttribute("name");
if(uid==null)
response.sendRedirect("login.html");
Statement stmt=con.createStatement();
ResultSetrs= stmt.executeQuery("select * from books where bid in(select bid from
selected where name='"+uid+"')");
out.println("<table width='100%'>");
int total=0;
while(rs.next())
{
String img=rs.getString(2);
String name=rs.getString(3);
String author=rs.getString(4);
String publish=rs.getString(5);
int price=rs.getInt(6);
total+=price;
out.println("<tr><td><imgsrc='"+img+"'/><td>name "+name+"<br>author
"+author+"<br>publisher "+publish+"<td>price $"+price+"</tr>");
}
out.println();
out.println("<tr><thcolspan=2>Total Amount"+"<th>$"+total+"</tr></table>");
}
catch(Exception e){out.println(e);
}
%>
cartpage.jsp
<%@ page import="java.sql.*"%>
<%
try
{
boolean valid=false;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsn","system","system");
response.setContentType("text/html");
HttpSessionhs=request.getSession();
String uid=(String)hs.getAttribute("name");
if(uid==null)
response.sendRedirect("login.html");
int bid=Integer.parseInt(request.getParameter("bid"));
Statement stmt=con.createStatement();
int n= stmt.executeUpdate("insert into selected values('"+uid+"',"+bid+")");
response.sendRedirect("cart.jsp");
}
catch(Exception e){
out.println(e);
}
%>
session.jsp
<%@ page import="java.sql.*"%>
<%
response.setContentType("text/html");
HttpSessionhs=request.getSession();
String uid=(String)hs.getAttribute("name");
if(uid!=null)
hs.invalidate();
response.sendRedirect("main.html");
%>
catalog.jsp
<%@ page import="java.sql.*"%>
<%
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsn","system","system");
Statement stmt=con.createStatement();
response.setContentType("text/html");
ResultSetrs=stmt.executeQuery("select * from books");
out.println("<table width='90%'>");
while(rs.next())
{
out.println("<form method=get action='cartpage.jsp'><input type=hidden
name=bid value='"+rs.getString(1)+"'></input><tr><td>
<imgsrc='"+rs.getString(2)+"'/><td>name"+rs.getString(3)+"<br>author"+rs.get
String(4)+"<br>publisher"+rs.getString(5)+"<td>price"+rs.getInt(6)+"<td><input
type=submitvalue=addtoCart></td></tr></form>");
out.println("");
}
out.println("</table>");
}
catch(Exception e)
{
out.println(e);
}
%>