Professional Documents
Culture Documents
Date:
Registration Page Validation using Java Script
Aim: To write a JavaScript program to validate the following fields of the registration page.
1. Name (Name should contain 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 contain any invalid and must follow the standard pattern
name@domain.com)
4. Phone number (Phone number should contain 10 digits only).
Description: To insert a JavaScript into a HTML page, we use the <script> tag. Inside the
<script> tag we use the type attribute to define the scripting language. We can use regular
expressions to validate the fields of the registration page. The fields are placed in the body part
of the document; at the time of submitting form those fields will be submitted to a JavaScript
function for validations.
Program:
<html>
<head><title>Sample Program</title>
<script language="javascript">
function validate_form()
{ var name_regex = /[^a-zA-Z ]/;
var email_regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
var phone_regex = /[^0-9]/;
var name = document.form.txtName.value;
var pass = document.form.txtPass.value;
var email = document.form.txtEmail.value;
var phone = document.form.txtPhone.value;
if( name_regex.test( name ) )
{ window.alert("Name must contain characters only");
return false;
}
if ( name.length < 6 )
{ window.alert("Name must contain atleast 6 characters");
return false;
}
if ( pass.length < 6 )
{ window.alert("Password must contain atleast 6 characters length");
return false;
}
if ( ( (email.length> 0) && ( email_regex.test( email ) ) ) != true )
{ window.alert("Email must follow the standard pattern name@domain.com ");
return false;
}
if( phone_regex.test( phone) )
{ window.alert("Phone number should contain digits only");
return false;
Output:
Description: The XML document contains data whereas DTD file contains rules that apply to
the data. A DTD is a file with a dot dtd (filename.dtd) extension. The contents of these files are
purely textual in nature. A DOCTYPE declaration in an XML document specifies that we want to
include a reference to a DTD file. XSL is a language for expressing style sheets. With XSL a
template is created and this template is used to format XML elements which match a specified
pattern.
Program:
book.dtd
Output:
Description:
Steps to create a Java-Bean:
1) Create a new directory in C:\beans\demo\sunw\demo with a new folder name colors
2) Create a java source file
3) Compile the java source file
4) Create a manifest file colors.mft in the directory called as C:\beans\demo
5) Create a jar file- to create a jar file type the following command in the command prompt
jar cfm ..\jars\colors.jar colors.mft sunw\demo\colors\*.class
6) Start the BDK
7) Check whether the colors bean is placed in toolbox or not
8) Create an Instance of the Colors Bean After you complete the preceding steps, create an
instance of the Colors Bean in the BeanBox window. Test your new component by
pressing the mouse anywhere within its borders. Its color immediately changes. Use the
Properties window to change the rectangular property from false to true. Its shape
immediately changes.
Program: Colors.java
package sunw.demo.colors;
import java.awt.*;
import java.awt.event.*;
public class Colors extends Canvas
{ transient private Color color;
private boolean rectangular;
public Colors()
{ addMouseListener(new MouseAdapter()
{ public void mousePressed(MouseEvent me)
{ change();
}
});
rectangular = false;
setSize(200, 200);
change();
}
public boolean getRectangular()
{ return rectangular;
}
public void setRectangular(boolean flag)
{ this.rectangular = flag;
repaint();
}
colors.mft
Name: sunw/demo/colors/Colors.class
Java-Bean: True
Output:
Procedure:
By default Tomcat will take port no as 8080. If you want, you can change that port
number to 8000.
Procedure: According to javasoft a web application is a collection of web resources. The web
resource can be a static resource like HTML/XML/WML etc or dynamic resource like CGI,
Servlet etc.
Create a directory in C:\Program Files\Apache Software Foundation\Tomcat 7.0\
webapps\ to place everything related to the web application. This directory can be called
as webroot or document root. (e.g. \ourwapp)
Under webroot create a directory with the name WEB-INF (e.g. \ourwapp\WEB-INF)
Under WEB-INF create the following two directories and web.xml file:
o classes
o lib
o web.xml
The jar files that are required for the web application must be stored under the lib folder.
The java classes must be stored under classes folder. Place the static resources under the
webroot directory or subdirectory of webroot directory (i.e. HTML/XML files).
ourwapp
WEB-INF
classes
lib
web.xml
Output:
Conclusion: The web pages developed in week-1 and week-2 is placed in the document root and the
web pages are accessed successfully.
Description: First login name and password must be submitted from login page to the servlet.
In the servlet the parameters are read from login page and then cookie is created with that data.
To create cookie Cookie class is used. Java Servlets transparently supports HTTP cookies. Server
script sends a set of cookies to the browser. Browser stores this information on local machine for
future use. While creating a cookie we must provide a name and a value for that cookie. In this
program consider login name as cookie name and password as its value.
Program:
Login.html
<html><head><title>Login Page</title>
</head>
<body bgcolor="d3d3d3">
<form method=”post” action=”test1”>
Output:
Description: Java Servlets transparently supports HTTP cookies. Server script sends a set of
cookies to the browser. Browser stores this information on local machine for future use. Get
cookie name and its value by using getCookies() method and store these details in two variables
and then compare it with initialization parameters by reading it by using ServletConfig object to
two variables. Compare the variables respectively and if the match is found then display
welcome message otherwise display an error message.
Program: ShowCookie.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class ShowCookie extends HttpServlet
{ public void service (HttpServletRequest req, HttpServletResponse res) throws
ServletException, IOException
{ String user1,pass1,user2,pass2;
PrintWriter out = null;
boolean flag = false;
res.setContentType ("text/html");
out = res.getWriter ();
Cookie ck []= req.getCookies ();
if (ck!=null)
{ user1 = ck [0].getName ();
pass1 = ck [0].getValue ();
// reading init parameters
ServletConfig config=getServletConfig ();
Enumeration en=config.getInitParameterNames ();
while (en.hasMoreElements ())
{ Object obj=en.nextElement ();
user2= (String) obj;
pass2=config.getInitParameter (user2);
if( user1.equals(user2) && pass1.equals(pass2) )
flag = true;
}
if( flag == true)
out.println("<h1>Welcome " + user1+"</h1>");
else
out.println("<h1>You are not an authenticated user");
}
else
Output:
Description: First we can create a table to store details like name, password, email-id, phone
number in oracle. By writing a Java Servlet program, first oracle thin driver can be loaded then
connection has to be established. After getting connection query the database using insert query
with the values by using prepared statement. After inserting the values into the database display
all the records that are available in that table.
Program:
registration table (ORACLE)
create table registration (uname varchar2(20), pass varchar2(20), email varchar2(20), phone
varchar2(20));
register.html
<html> <head><title>Registration page</title>
</head>
<body><form METHOD="POST" ACTION="rrr">
<CENTER><h1>REGISTRATION FORM</h1></center><br><br>
Output:
Aim: To write a JSP program to authenticate login information of a user by reading the data from
the database using JDBC connection.
Description: First we must create a database table with some values. After creating a table we
must design login web page and when submit button is clicked in the login web page the details
must be submitted to the JSP page. In the JSP page the user details will be fetched from the
database table and the same is compared with the login form data. If the match is found then
display welcome message otherwise display an error message.
Program:
registration table (ORACLE)
create table registration (uname varchar2(20), pass varchar2(20), email varchar2(20), phone
varchar2(20));
Login.html
<html><head>
<title>Login Page</title></head>
<body bgcolor="d3d3d3">
<form method="post" action="validate.jsp">
<table border="0"align="center" cellspacing="5"cellpading="5">
<caption><h1>Login Page</h1></caption>
<tr> <td>Login:</td>
<td><input type="text" Name="textName" size="30"></td>
</tr>
<tr> <td>Password:</td>
<td><input type= "Password" name="Password1"size="30"></td>
</tr>
<tr> <td><input type="submit" name="submit" value="submit"></td>
<td><input type="submit" name="Submit" value="Reset"></td>
</tr>
</table> </form>
</body>
</html>
validate.jsp
<%@ page import="java.sql.*" %>
<% String user1 = request.getParameter("textName");
String pass1 = request.getParameter("Password1");
boolean flag = false;
try
{ Class.forName ("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:XE","system","manager");
Statement stmt = con.createStatement();
Output:
Aim: To create a table in the database which contains the details of items (books in our case like
Book name, Author, Publisher, Price) of each category. Modify your catalogue page (week 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.
Description: First we must create a database table with some values. After creating a table we
must design modification web page with the field book name and submit button. When submit
button is clicked the details of the respective book will be displayed by using a JSP page. After
modifications and updations the details will be submitted to another JSP page which will update
the data to the database and by using the same JSP page the details in the database table will be
displayed back.
Program:
Book_details database (oracle)
Create table book_details( name varchar2(50), author varchar2(50), publisher varchar2(50), price
number);
Modify.html
<html><head>
<title>Catalogue Modification</title></head>
<body bgcolor=”d3d3d3”>
<form method=”post” action=”modify.jsp”>
<table border=”0”align=”center” cellspacing=”5”cellpading=”5”>
<caption><b>Modification of Catalogue</b></caption>
<tr><td>Book Name :</td>
<td><input type=”text” name=”bookname” length=”70”></td>
</tr>
<tr> <td><input type=”submit” name=”submit” value=”Get Details”></td>
<td><input type=”reset” name=”reset” value=”Reset”></td>
</tr></table>
</form>
</body>
</html>
modify.jsp
<%@ page import="java.sql.*" %>
<% response.setContentType("text/html");
String bname = request.getParameter("bookname");
try
{ Class.forName ("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection
("jdbc:oracle:thin:@localhost:1521:XE","system","manager");
PreparedStatement pstmt = con.prepareStatement("select * from book_details
where name = ?");
pstmt.setString( 1,bname);
Output:
Aim: HTTP is a stateless protocol. Session is required to maintain the state. The user may add
some items to cart from the catalog page. He can check the cart page for items. He may visit
catalogue again and select some more items. Here our interest is the selected items should be
added to older cart rather than new cart.
Description: HttpSession object is used to store entire session with a specific client. We can
store, retrieve and remove attribute from HttpSession object. Any servlet can have access to
HttpSession object through the getSession() method of the HttpServletRequest object. On client's
first request, the Web Container generates a unique session ID and gives it back to the client with
response. The client sends back the session ID with each request. The Web Container uses this
ID, finds the matching session with the ID and associates the session with the request.
Program:
Catalogue.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
Cart.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Cart extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)throws
IOException, ServletException
{
res.setContentType("text/html");
PrintWriter pw=res.getWriter();
HttpSession hs=req.getSession();
ArrayList<String> cart=(ArrayList<String>)hs.getAttribute("cart");
if(cart==null)
{
pw.println("No items in your cart");
cart=new ArrayList<String>();
hs.setAttribute("cart",cart);
}
web.xml
<web-app>
<servlet>
<servlet-name>cat</servlet-name>
<servlet-class>Catalogue</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cat</servlet-name>
<url-pattern>/Cat</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>cart</servlet-name>
<servlet-class>Cart</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cart</servlet-name>
<url-pattern>/cart</url-pattern>
</servlet-mapping>
</web-app>
Output: