Java Server Pages

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 219

JAVA SERVER PAGES

 Java server pages is a java base technology


that is run on server to facilitate
processing web-based request.
 It is a templating mechanism whereby java-
based logic can be embedded within
HTML.
Conti…JSP

 Java Server Pages (JSP) is a technology based


on the Java language and enables the
development of dynamic web sites.
 JSP was developed by Sun Microsystems to
allow server side development.
 JSP files are HTML files with special Tags
containing Java source code that provide the
dynamic content.
Conti.. JSP

 Java Server Pages (JSP) is a Sun Microsystems


specification for combining Java with HTML to
provide dynamic content for Web pages.
 JSP is part of the Java 2 Enterprise Edition
(J2EE).
The following diagram shows a web server
that supports JSP files. Notice that the web
server also is connected to a database
 JSP source code runs on the web server in the
JSP Servlet Engine.
 The JSP Servlet engine dynamically generates
the HTML and sends the HTML output to the
clients web browser.
Why to use JSP
 JSP is easy to learn and allows developers to
quickly produce web sites and applications in
an open and standard way.
 JSP is based on Java, an object-oriented
language. JSP offers a robust platform for web
development.
 Main reasons to use JSP:
1. Multi platform
2. Component reuse by using
Javabeans and EJB.
3. You can take one JSP file and move it to
another platform,web server or JSP Servlet
engine.
Conti…Why use JSP

 HTML and graphics displayed on the web


browser are classed as the presentation layer.
 The Java code (JSP) on the server is classed as
the implementation.
 By having a separation of presentation and
implementation, web designers work only on
the presentation and Java developers
concentrate on implementing the application.
Advantages of JSP Over Servlet
 Both are web applications used to produce
web content that mean dynamic web pages.
Both are used to take the requests and service
the clients.
The main difference between them is as
follows:
1)Servlet :In servlets both the presentation and
business logic are place it together.
Jsp : Where as in jsp both are separated by
defining by java beans .
2)Servlet : It is a java class so for every change
we have to compile the code to reflect the
change.
 Mainly used for writing business logics.

Jsp :its a file, its automatically converted into


a servlet on deploying. We can't compile it
explicitly. the changes will get reflect by
saving the file.
 Mainly used for presentation of data.
3) Jsp : It is a technology that lets you mix
regular, static HTML with dynamically-
generated HTML.
 When you create dynamic content, JSPs are

more convenient to write than HTTP servlets


because they allow you to embed Java code
directly into your HTML pages.
Servlet : make you generate the entire page via
your program, even though most of it is
always the same. JSP lets you create the two
parts separately.
 In servlet you embed HTML inside Java code.
4)Servlet : servlets are java classes.
 consist of an html file for static content & java
file for dynamic content.
 servlet can support to HTTP,FTP, and SMTP
protocol.
 servlet will manually redeploy.
Jsp : Jsp is not a java class.
 contains java code embedded directly to
in an html page by using special
tags.
 Jsp only support to HTTP protocol.
 jsp page automatic compile.
5) JSP : JSP are compiled to servlets, effectively
allowing you to produce a servlet by just
writing the HTML page, without knowing
Java.
 In JSP implicit objects are present which we
can implement directly into jsp pages.
Servlet : If you write servlets, you need to
know Java to write the servlet body, and
HTML to write the output.
 In servlet implicit objects are not present.
6)Servlet :while using servlet every time even
for a minor change to the HTML,the java
source code must be modified,recompiled and
redeployed.

Jsp :while using JSP it is not necessary to


write or compile any code as modifications to
the JSP can be viewed immediately because
the JSP container will automatically recompile
the code.
JSP architecture

 JSPs are built on top of SUN Micro-systems


servlet technology.
 JSPs are essential an HTML page with special
JSP tags embedded. These JSP tags can
contain Java code.
 The JSP file extension is .jsp rather than .htm
or .html. The JSP engine parses the .jsp and
creates a Java servlet source file.
Conti…JSP Architecture

 It then compiles the source file into a class file,


this is done the first time and this why the JSP
is probably slower the first time it is accessed .
JSP Tags

Using JSP tags,


There are five main tags:
1) Directive tag
2) Declaration tag
3) Expression tag
4) Scriplets tag
5) Action tag
 Directives
In the directives we can import packages,
define error handling pages or the session
information of the JSP page.
 Declarations
This tag is used for defining the functions and
variables to be used in the JSP.
  Scriplets
In this tag we can insert any amount of valid
java code and these codes are placed in
_jspService method by the JSP engine.
   
Conti…JSP Tags
 Expressions
We can use this tag to output any data on the
generated page. These data are automatically
converted to string and printed on the output
stream.
 Action
The Action element is very different from the
Scripting elements. There is only one syntax for
the Action element. Action elements are
basically predefined functions.
Program for “Hello World”

<html>
<head>
<title>My first JSP page
</title>
</head>
<body>
<%@ page language="java" %> //directive tag
<% out.println("Hello World"); %>//expression tag
</body>
</html>
Output:
1) JSP Directives <%@ -- -- %>
a) include: The include directives allows the
insertion of another file into the defining page.
This insertions happens at translation time.
 The syntax is:<%@include file=“URL”%>
Ex: <%@ include file="/header.jsp" %> 

b) taglib : It is used to use the custom tags in


the JSP pages (custom tags allows us to defined
our own tags).
 A Tag Library contains custom tags that are
developed to give JSP page designer new
features use within a JSP
Conti.. JSP Directive
 The taglib directives effectively performs three
function:
• Imports the taglib into the page
• Associates a URI with the taglib to uniquely
identify it
• Maps that URI to a prefix for use within the page
Conti.. JSP Directive
 The syntax is as follows:
<%@ taglib uri=“taglibURI” prefix=“prefix”%>
The uri is used to locate a description of the taglib
Ex: <%@ taglib uri="tlds/taglib.tld"
prefix="mytag" %> 

c) page :page is used to provide the


information about it.
Ex: <%@page language="java" %> 
Program for “include file”

first.jsp
<html>
<body>
<h2>This is the beginning of first.jsp</h2>
<%@ include file="second.jsp" %> //directive
<h2>This is the end of first.jsp</h2>
</body>
</html>
Conti..
second.html
<html>
<body>
<center>
<h1>This is from Date.jsp in build library <br>
The date is
<%= new java.util.Date() %> //expression tag
</h1>
</center>
</body>
</html>
Output
Page directive
 Language – “scriptingLanguage”
 Extends – “classname”
 Import – “importList” – java.lang.*, javax.servlet.*,
javax.servlet.jsp.*, and javax.servlet.http.* are imported
implicitily
 Session “true|false”
 Buffer – “none|sizekb”
 autoFlush- “true|false”
 isThreadSafe – “true|false”
 Info – “info_text”
 errorPage – “error_URL”
 isErrorPage – “true|false”
 contentType – “contentInfo”
 pageEncoding – “pageEncodingInfo
Page Directive attibute description:
 import 
Results in a Java import statement being inserted
into the resulting file.
Ex: <%@ page import="java.util.*" %>
 contentType 

Use this attribute to set the mime type and


character set of the JSP.
Ex: <%@page language="java" session=“true”
ContentType="text/html; %> 
Conti.. Page Directive
 errorPage 
errorPage="error.jsp"
errorPage is used to handle the un-handled
exceptions in the page.
Ex: <%@page language="java" session="true"
errorPage="error.jsp"  %> 
 isErrorPage 
If set to true, it indicates that this is the error
page. Default value is false.
Ex: <%@ page isErrorPage="false" %>
Conti.. Page Directive

 autoFlush 
To autoflush the contents. A value of true, the
default, indicates that the buffer should be
flushed when it is full. A value of false, rarely
used, indicates that an exception should be
thrown when the buffer overflows. A value of
false is illegal when also using buffer="none".
Ex: <%@ page autoFlush="true" %>
Conti.. Page Directive
 session
session="true"
When this value is true session data is
available to the JSP page otherwise not. By
default this value is true.
Ex: <%@page language="java“ session="true"%>
 buffer
To set Buffer Size. The default is 8k and it is
advisable that you increase it.
Ex: <%@ page buffer="20kb" %>
Conti.. Page Directive
 language
language="java"
This tells the server that the page is using
the java language. Current JSP
specification supports only java language.
Ex: <%@ page language="java" %> 
Conti… Page Directive
 extends
extends="mypackage.myclass"
This attribute is used when we want to extend
any class. We can use comma(,) to import
more than one packages.
Ex: <%@page language="java"
import="java.sql.* , mypackage.myclass" %> 
Conti… Page Directive
 info
Defines a String that gets put into the
translated page, just so that you can get it
using the generated servlet's inherited
getServletInfo() method.
 isThreadSafe 
Indicates if the resulting servlet is thread safe.
Ex: <%@ page isThreadSafe="true" %>
Conti… Page Directive

 pageEncoding
Defines the character encoding for the JSP.
The default is "ISO-8859-1"(unless the
contentType attribute already defines a
character encoding, or the page uses XML
document syntax).
Program for “checking session’
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page session="true" %> //declaration tag
<html>
<body>
<h2>This is static template data </h2>
<% if (session.isNew()) //scriplet
{ out.println("<h3>New Session</h3>");
}
%>
</body>
</html>
Output:
Conti…
Program for “Counter”
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<%@page contentType="text/html"%> //directive tag
<%@page language="java"%>// directive tag

<%! int count=0; %> //declaration tag


<% count++; %> //scriplet
Welcome ! You are visitor number
<%= count %> //expression tag
</body>
</html>
Output
Program for “Today’s Date”

<html>
<body>
<center>
<h3>This is from Date.jsp <br>
The date is
<%= new java.util.Date() %>//expression tag
</h3>
</center>
</body>
</html>
Output
2) Declaration Tag <%! --- %>
 This tag allows the developer to declare
variables or methods.
 The declaration are available for reference
by subsequent scriptlet, expression, or
other declaration.
 Before the declaration you must have <
%! and end of the declaration is %>.
Conti…. Declaration tag
 Code placed in this tag must end in a
semicolon ( ; ).
 Declarations do not generate output so are
used with JSP expressions or scriptlets.
Ex:
<%!
   private int counter = 0 ;
   private String get Account ( int accountNo) ;
%>
Program for “Loop”
<%@page contentType="text/html“%> //directive tag
<HTML>
<HEAD>
<TITLE> JSP loop</TITLE>
</HEAD>
<BODY bgcolor=pink>
<font face=verdana color=darkblue>JSP loop<BR> <BR>
<%! //declaration tag
public String writeThis(int x)
{ String myText="";
for (int i = 1; i < x; i++ )
myText = myText + "<font size= i color=darkred
face=verdana>kirtis loop program</font><br>" ;
return myText;
}
%>
Conti…
This is a loop example from the
<br>
<%= writeThis(8) %> //expression tag
</font>
</BODY>
</HTML>
Output
3) JSP Expression <%= -- - %>
 Expression are an evaluation of some Java
statement (without the”;”).
 The result must be able to be cast to a String, and is
includes in the servlet’s output.
 Expression may be used to set attributes of the
following action tags:
<jsp:setProperty>, <jsp:include>,<jsp:getProperty>,
<jsp:forward>, and <jsp:param>tags.
(we will see after words)
Conti… JSP Expression

 The syntax for JSP expression:


<%=validJavaExpression%>
 Between <%= - - %> you can put anything
and that will converted to the String and that
will be displayed.
Ex: <%= new java.util.Date() %>
Above code will display Today’s date .
Program for “expression tag”
<HTML>
<HEAD>
<TITLE>JSP Expressions</TITLE>
</HEAD>
<BODY>
<H2>JSP Expressions</H2>
<UL>
<LI>Current time: <%= new java.util.Date()%> //expression tag
<LI>Your hostname: <%= request.getRemoteHost() %>
<LI>Your session ID: <%= session.getId() %> //expression tag
<LI>The <CODE>testParam</CODE> form parameter:
<%= request.getParameter("testParam") %> //expression tag
</UL>
</BODY>
</HTML>
Output:
4) JSP Scriplets <% - - - %>
 Scriplets allow the introduction of any valid
Java code.
 The JSP Scriplets syntax is:
<% java statement %>
 JSP scriplet begins with <% and ends %> We can
embed any amount of java code in the JSP
scriplet.
 JSP Engine places this java code in the
_jspService() method.
Variable available to the JSP Scriplet’s
are:
request:
request represents the clients request and is
a subclass of HttpServletRequest. Use this
variable to retrieve the data submitted along
the request.
Ex:
<%
String userName=null;
userName=request.getParameter("userName");
%>
Conti… variable JSP scriplet
 response:
response is subclass of HttpServletResponse.
 session :
session represents the HTTP session object
associated with the request.
Ex: session.setAttribute("firstname",firstName);
 Out :
out is an object of output stream and is used to
send any output to the client.
Ex: out.println(“This is java page”);
Conti.. variable JSP scriplet

 Other variable available to the scriptlets are


pageContext, application,config and exception.
Program for “scripting test:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.util.Calendar" %> //directive tag
<html>
<body>
<h2>This is scripting test</h2>
<%!Calendar c= Calendar.getInstance();%> //declaration tag
<% if ((c.DAY_OF_WEEK==Calendar.SATURDAY) //scriplet
|| (c.DAY_OF_WEEK==Calendar.SUNDAY))
{ out.println ("Its the weekend,I don't know what time it is");
}
else
{ out.println ("today is c.getTime()");}
%>
</body>
</html>
Output:
5) Action Tag
 Action element are request-time oriented
 Action element are the JSP element that are
directly involved in the processing of the
request.
 In most cases, action element enable you to
access data and manipulate or transform
data in the generation of the dynamic output.
 Action element can be either standard or
custom.
Conti… Action Tag
 Actions are broken down into the three
categories namely:- Control Tags , Bean
Tags & Custom Tags.
a) Bean Tags :- tags specifically for use with
JavaBeans.
b) Custom Tags :- tags that allow the definition
and use of custom JSP tags.
Conti…. Action Tag

c) Control Tags :- tags for flow and plugin


control, including <jsp:include>,<jsp:forward>
<jsp:param>, and <jsp:plugin>
• The<jsp:param> Action Tag
Define a parameter to be passed to an
included or forwarded page, or page that
uses the <jsp:plug-in>tag.
Conti.. Control Tag

The syntax is :-
<jsp:param name=“name” value=“value” />
 The value may be populated with an

expression, as in:
<jsp:param name=“today”
value=“<%=new java.util.Date() %>” />
 The receiving page obtains the parameter via

Request.getParameter(“name”) method.
Conti.. Control Tag
• The <jsp:include> Action Tag
The standard <jsp:include>action can be
coded as follows:
<jsp:include page: ”news.jsp” flush=“false” />
 The name of this tag is jsp:include, the
attributes are page and flush and this
<jsp:include> instance does not have a body.
Conti… Control Tag
• The <jsp:forward> Action Tag
The forward tags is similar to the include tag,
except control over the target page and then
never returned to the original page for this
request.
The syntax foe using<jsp:forward>is:
<jsp:forward page=“URL”>
Conti.. Control Tag

• The <jsp:plugin> Action Tag


 The Java plug-in is a software program, that
contains a JVM and bolts onto a web
browser.
 The plug-in registers with the browser to
receive the downloaded applets or beans.
 The browser hands the classes over to the
plug-in, which then execute them .
Program for “parameter test”

paramTest1.jsp
<html>
<body>
<jsp:include page="paramTest2.jsp">
<jsp:param name="firstname" value="mary" />
</jsp:include>
</body>
</html>
Conti…
paramTest2.jsp
First name is <%=request.getParameter("firstname") %>
Last name is <%= request.getParameter("lastname") %>
Looping through all the first name
<% String first[]=request.getParameterValues("firstname");
for (int i=0;i<first.length;i++)
{ out.println(first[i]);
}
%>
Output
Comments

 JSP comment are only intended to assist with


understanding the JSP file itself, so they get
stripped out before the response page is
sent.
 The syntax for a JSP comment is:
<%-- jsp comment --%>
 The syntax for an HTML comment is:
<!-- html comment -->
Conti..

 You can also include JSP expression in


HTML comment.
Ex:
<!-- html comment
<%=new java.util.Date() %>
-->
JSP implicit objects
Roadmap
• Request

• Response

• Out

• Session

• Config

• Application

• Page

• pageContext

• exception
What are Implicit Objects?

 When writing servlet, the available objects


parameter to methods, thus if you are
overriding the doPost method you will have a
signature.
public void doPost(HttpservletRequest request
,HttpServletResponse response).
 When writing JSP pages the implicit objects

are created automatically within the service


method.
Conti… Implicit Objects
 The implicit variable are only available within
the jspService method and thus are not
available within any declaration.
 Ex:

<%!
public void method()
{ out.print (“Hello”);}
%>
// where out. Is implicit object
•The request and response implicit
objects
 The request implicit object is an instance of
HttpServletRequest
 Response is an instance of
HttpServletResponse objects
 A typical use for the request is to get the
details of HTTP query string such as query
name and parameter
Ex:<%=request.getQueryString() %>
Conti… Implicit Objects
Ex:
<%
String remoteAddr=request.getRemoteAddr();
response.setContentType(“text/html;
charset=ISO-8859-1”);
%>
<html><body>
Hi ! Your IP address is <%=remoteAddr %>
</body></html>
• The out implicit object

 The out implicit object has a type of


jsp.JspWriter
 The out object can be used in a similar way

to System.out in Java programs


Ex:
<%
out.println(“Hello world”);
%>
• The Session implicit object

 The session implicit object is an instance of


javax.servlet.http.Httpsession
 The session concept is a way of allowing
multiple request from the same client to be
group together as part of a single
”conversation”
Conti… Implicit Objects

Ex:
<html>
<body>
<%@ page session=“false” %>
Session ID = <%=session.getId() %>
</body>
</html>
• The config implicit object

 The config implicit object is an instance of


javax.servlet.ServletConfig
 To set up within the configuration a servlet is

configured within web.xml


 But instead of creating containing a

<servlet-class> element a <jsp-file> element is


created containing the name of the jsp page.
Conti… Implicit Objects

Ex:
<html><body>
Servlet Name =<%=config.getServletName() %>
P region=<%=config.getInitParameter(“region”)%>
</body></html>
• The application implicit object
 The application implicit object is an instance
of javax.servlet.ServletContext.
 It refers to the overall web application
environment that the JSP belongs to.
 According to the API docs :-
“Define a set of methods that a servlet uses
to communicate with its servlet container to
get the MIME type of a file, dispatch request,
or write to a log file.
• The page implicit object
 The page implicit object is of type Object and it is
assigned a reference to the servlet that executing
the _jspService() method.
 Thus in the Servlet generated by tomcat, the page
object is created as
object page=this;
• To access any of the methods of the servlet through
page, it must be first cast to type servlet
<%this.log(“log message”); %>
<%((HttpServlet)page).log(“another message”) ; %>
are equivalent
•The pageContext implicit object

 The pageContext implicit object has a type of


javax.servlet.jsp.PageContext.
 According to API document

“A pageContext instance provide access to all


the namespaces associated with a jsp page.”
 A typical use of the pageContext is to include
another resources or forward to another
resources.
Ex: <%pageContext.forward(“menu.jsp”)%>
•The exception implicit object

 The exception implicit object is of type


java.lang.Throwable .
 This object is only available to pages that have
isErrorPage set to true with the directive.
Ex:
<html><body>
<%@ page isErrorPage=‘true’ %>
Msg:<%=exception.toString() %>
</body></html>
JSP and JavaBeans

 Definition of JavaBeans:
JavaBeans have become a popular technique
for building reusable Java component.
It represents a simple Java class with some
properties. The bean properties are accessed
by Getter and Setter method. They are used
to separate Business logic from the
Presentation logic. Internally, a bean is just
an instance of a class.
Beans Tags Syntax Overview

 There are three tags to support the use of


beans:
• <jsp:useBean>
• <jsp:setProperty>
• <jsp:getProperty>
Conti.. Java Beans
 Syntax :-
• <jsp:useBean id="bean name" class="bean class"
scope = "page | request | session |application "/>

• <jsp:setProperty name = "bean name"


property = "someProperty" value = "someValue" />

• <jsp:getProperty name = "bean name"


property = "someProperty"    />
<jsp:useBean>
 This action load in a JavaBean to be used in the
JSP page.
 The simplest syntax for specifying that a bean
should be used is:
<jsp:useBean id="name" class="package.class" />
This usually means "instantiate an object of the
class specified
<jsp:getProperty>

 This element retrieves the value of a bean


property, converts it to a string, and inserts it
into the output.
 The two required attributes are name, the
name of a bean previously referenced via
jsp:useBean, and property, the property
whose value should be inserted
<jsp:setProperty>

 This element set the value of a bean


property. Like emp.setName() in Java.
Syntax is :
<jsp:useBean id="myName" ... /> ...
<jsp:setProperty name="myName"
property="someProperty" ... />
Conti.. Java Beans

Where ,
• bean name = the name that refers to the
bean.
• Bean class = name of the java class that
defines the bean.
• property = name of the property to be passed
to the bean.
• value = value of that particular property.
explanation for the different scopes
 Page scope:-Visible only to the first Servlet
or JSP that the request is mapped
object:pageContext

 Request scope:-Visible within the Servlet or


JSP handling the request, as well as any
other Servlet or page that is forwarded or
included during the processing of that request
object:request
Conti.. Java Beans

 Session scope:-Visible to all handlers


(Servlet or JSPs) for request of the same
client session
object:session

 Application scope:-Visible to all handlers


and sessions of the same webapp
object:application
Program for useBean and setProperty
getName.jsp
<HTML>
<BODY>
<FORM METHOD=POST ACTION="SaveName.jsp">
What's your name? <input type=text name=username size=20><br>
What's your e-mail address? <input type=text name=email
size=20><br>
What's your age? <input type=text name=age size=4>
<- - After click on button submit the flow will go to SaveName.jsp“- ->

<P><input type=submit</FORM>
</BODY>
</HTML>
Conti…

saveName.jsp
<jsp:useBean id="user" class="kirtipackage.UserData"
scope="session"/>
<jsp:setProperty name="user" property="*"/>
<html>
<body>
<a href="NextPage.jsp">Continue</A>
</body>
</html>
Conti…

nextPage.jsp
<jsp:useBean id="user" class="kirtipackage.UserData"
scope="session"/>
<html>
<body>
You entered<br>
Name: <%= user.getUsername() %><br>
Email: <%= user.getEmail() %><br>
Age: <%= user.getAge() %><br>
</body>
</html>
Conti…
Program for useBean and
getProperty
<html>
<head>
<title> JavaBeans in JSP</title>
</head>
<body>
<jsp:useBean id="test" class="kirtipackage.EmpBean" />
<jsp:setProperty name="test"
property="name"
value="kirtikumar" />
Name is :
<jsp:getProperty name="test" property="name" />
</body>
</html>
Output:
Program for addition of 2 no

<%@page contentType="text/html"%>
<html>
<head><title>JSP Page</title></head>
<body> Programe for addition of nos.<br>
<%=2+5%> is the sum of 2 and 5
</body>
</html>
Output screen of add pgam
Program for “client info”
<html>
<body>
Client computer details: <br><br>
<b>Ip address</b>: <br>
<%=request.getRemoteAddr()%> <br><br>
<b>Computer name</b>:
<br>
<%=request.getRemoteHost()%>
<br><br>
</body>
</html>
Output
program for “Transfer value”
myform.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<title>Linking</title>
</head>
<body>
<form action="myformconfirm.jsp" method="post">
Enter in a website name:<br>
<input type="text" name="website"><br>
<input type="submit" name="submit">
</form>
</body>
</html>
Conti…
myformconfirm.jsp
<html> <head>
<title>Linking implements</title>
</head>
<body>
<font size=3>
Your info has been received:
<br><br>
<%
String sName = request.getParameter("website");
out.print(sName);
%>
</font>
</body>
</html>
Output
JSP- JDBC Connectivity

 Design the GUI for inserting login and


password into the table.
1)Create the table employee in MS Aces.
2)Write JSP program.
Program for login page
Login.jsp
<%@page contentType="text/html"%>
<%@page import="java.sql.*"%>
<html>
<head>
<title>Login Page</title>
<center><font size="5" color="blue">Login Form</font></center>
</head>
<body bgcolor="yellow"><font size="5" color="blue">
<form method=post action="login.jsp">
// ="login.jsp call the same program again and again
Username:<input type="text" name="u1"><br>
Password:<input type="password" name="p1"><br>
<input type="submit" value="submit">
Conti..
<%
try {
String user1=request.getParameter("u1");
String password1=request.getParameter("p1");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:mydsn");
PreparedStatement psmt=con.prepareStatement
("insert into employee values(?,?)");
psmt.setString(1,user1);
psmt.setString(2,password1);
psmt.executeUpdate();
con.close();
catch(Exception e)
{ out.println(e);
}
%>
</form>
</font>
</body>
</html>
Output screen for login form
Database output for login form
Registration form: To Store the Data into
the Database (Student) i.e Form creation
 This is a program in which user stored
information for registration.
 Complete Information are stored in database
by database connectivity
 Finally we can see where and which data
entry occur in database.
Registrations Form
Registration_page.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Registration Page</title>
</head>
<body bgcolor="pink">
<form>
First Name<input type="text" name="t1"> <br>
Middle Name <input type="text" name="t2"> <br>
Last Name<input type="text" name="t3"> <br>
Address<TextArea rows=2 cols=15 name="tx1"></TextArea><br>
Contact No <input type="text" name="t4"> <br>
Conti…
Gender <input type="radio" name="Gender">Male
<input type="radio" name="Gender">Female<br><br>
Password<input type="password" name="*****"><br>
Submit <input type="submit" name="s1"> <br>
Reset <input type="reset" name="r1"><br>
</Form>
<%
String FirstName=request.getParameter("t1");
String MiddleName=request.getParameter("t2");
String LastName=request.getParameter("t3");
String Address=request.getParameter("tx1");
String ContactNo =request.getParameter("t4");
String Gender =request.getParameter("Gender");
String password =request.getParameter("*****");
Conti…
try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:rani");
PreparedStatement pst=con.prepareStatement("insert into student
values(?,?,?,?,?,?,?)");
pst.setString(1,FirstName);
pst.setString(2,MiddleName);
pst.setString(3,LastName);
pst.setString(4,Address);
pst.setString(5,ContactNo);
pst.setString(6,Gender);
pst.setString(7,password) ;
pst.executeUpdate();
con.close();
} catch(Exception e)
{ System.out.println(""+e);
}
%>
</body>
</html>
Data Store
Case Study: Student Management
Input :Form
Output:Report for single record

 Student information is a program in which


information of student are stored
 We store username, password, branch etc
 When we want to retrieve or show
information, by using username and
password we can access student information
Home Page
Conti..
Conti…
Home Page
Slogin.jsp

<%@ page import="java.sql.*" errorPage="errormsg.jsp"


%>
<html>
<head>
<title>Student Management System</title>
</head>
<body bgcolor="pink"> <font color="purple"
size="5"><center>
<h1>Student Management System</h1></center><br><br>
<form action="Studentlogin.jsp">
<table border="1" align="center"><tr bgcolor="pink"><td>
Username<input type="text" name="u2"></td></tr>
<tr bgcolor="pink"><td>
Password <input type="password" name="p2"></td></tr>
<tr bgcolor="pink"><td><center>
<input type="submit" name="sub2" value="submit"></center></td></tr>
</table> <br><br>

<font color="red" size="4"> <center> New User:


<a href="Sregistration.jsp">Sign Up</a> </center></font>
</form>
</font>
</body>
</html>
Studentlogin.jsp

<%@ page import="java.sql.*" errorPage="error.jsp" %>


<html>
<body bgcolor="#CCAEEF"><form
action="ViewProfile.jsp" method="post">
<% try
{ String user1=request.getParameter("u2");
String password1=request.getParameter("p2");
session.setAttribute("u2",user1);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:kitti");
String sql1="select * from studentreg where UserName=? and
Password=?";
PreparedStatement psmt=con.prepareStatement(sql1);
psmt.setString(1,user1);
psmt.setString(2,password1);
ResultSet r=psmt.executeQuery();
if(r.next())
{ %>
<font color="purple" size="5"> <b>
<% out.println("Welcome " + (String)session.getAttribute("u2"));
%>
</b></font><br><br>
<input type="submit" name="sub" value="view profile">
<%
}
else
{
response.sendRedirect("Error.jsp");
}
con.close();
}
catch(Exception e)
{ out.println(e);
}
%>
</body>
</html>
ViewProfile.jsp( to get the report of the form)
<%@ page import="java.sql.*" errorPage="errormsg.jsp"
%>
<html>
<head>
<title>View Profile</title>
</head>
<body >

<% try
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=
DriverManager.getConnection("jdbc:odbc:kitti");
String sql1="select * from studentreg where UserName=?";
PreparedStatement psmt=con.prepareStatement(sql1);
psmt.setString (1,(String)session.getAttribute("u2")) ;
ResultSet r=psmt.executeQuery();
ResultSetMetaData rsmd=r.getMetaData();
%>
//this is report in row & column format
<table border="1" align="center" bgcolor="#CCAEEF">
<tr><th colspan="1">Student Information</th></tr>
<% while(r.next())
{ for(int i=1;i<=rsmd.getColumnCount();i++)
{ %>
<tr><td colspan="2"><%=rsmd.getColumnName(i) %>
:</td><td colspan="2"><%=r.getString(i)
%></td></tr>
<% } }
}
catch(Exception e)
{
out.println("e");
}
%>
</table>
</body>
</html>
Sregistration.jsp
<%@ page import="java.sql.*" errorPage="errormsg.jsp" %>
<html>
<body bgcolor="#CCAEEF">
<form action="registered.jsp" method=post>
<center><table cellpadding=4 cellspacing=2 border=1><th
colspan=2>
<font size=5>USER REGISTRATION</font><br>
<font size=1><sup>*</sup> Required Fields</font></th><tr>
<td valign=top> <b>First Name<sup>*</sup></b> <br>
<input type="text" name="firstName" value="" size=20
maxlength=20></td>
<td valign=top><b>Last Name<sup>*</sup></b><br>
<input type="text" name="lastName" size=20
maxlength=20></td></tr>
<tr> <td valign=top>
<b>E-Mail<sup>*</sup></b> <br>
<input type="text" name="email" size=20
maxlength=25><br></td>
<td valign=top> <b>Address<sup>*</sup></b> <br>
<input type="text" name="add" size=20
maxlength=125></td>
</tr>
<tr> <td valign=top colspan=2> <b>User
Name<sup>*</sup></b>
<input type="text" name="userName" size=20
maxlength=10> </td></tr>
<tr> <td valign=top> <b>Password<sup>*</sup></b> <br>
<input type="password" name="password" size=20
maxlength=10></td> </tr>
<tr> <td valign=top colspan=2> <b>Stream :
<select name="stream">
<option>Com Sci</option>
<option>Electronics</option>
<option>Mech</option>
<option>Civil</option></select></b> </td> </tr>
<tr>
<td align=center colspan=2>
<input type="submit" value="Submit"> <input type="reset"
value="Reset">
</td> </tr>
</table>
</center>
</form>
</body>
</html
Registrated.jsp
<%@ page import="java.sql.*" errorPage="error.jsp" %>
<html>
<body bgcolor="#CCAEEF">
<% try
{ String fname=request.getParameter("firstName");
String lname=request.getParameter("lastName");
String email=request.getParameter("email");
String address=request.getParameter("add");
String username=request.getParameter("userName");
String Password=request.getParameter("password");
String stream=request.getParameter("stream");
session.setAttribute("firstName",fname);
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:kitti");
String sql1="insert into studentreg
(FirstName,LastName,EMail,Address,UserName,Password,S
tream) values(?,?,?,?,?,?,?)";
PreparedStatement psmt=con.prepareStatement(sql1);
psmt=con.prepareStatement(sql1);
psmt.setString(1,fname);
psmt.setString(2,lname);
psmt.setString(3,email);
psmt.setString(4,address);
psmt.setString(5,username);
psmt.setString(6,password);
psmt.setString(7,stream);
psmt.executeUpdate();
%>
<font color="purple" size="5"> <b> <%
out.println("Welcome " +
(String)session.getAttribute("firstName"));%>
</b></font> <br><br><br>
<a href="SLogin.jsp"><b>Back to Login Page
</b></a><br><br>
<% con.close() :}
catch(Exception e)
{ out.println(e);
} %>
</body>
</html>
Errormsg.jsp
<%@ page isErrorPage="true" %>
<html>
<head>
<title>Error Page:</title>
</head>
<body>
<h2>Somethings has gone wrong..</h2>
<%=exception%>
</body>
</html>
Error.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP Page</title>
</head>
<body bgcolor="#CCAEEF">
<h1> No such user found<h1>
</body>
</html>
Library Management

 Problem Statement:
In this program we add books of
different subject with author name, price, year
of book etc.
After adding we can search any book by
its name, if this this book found it will show
details of book in table format.
Home Page:
Adding of Book:
Search Book:
Details of Book:
JSP Code for Library Management
Home.jsp
<html>
<head>
<title>Library Management Page</title>
</head>
<body bgcolor="#ABCEEF">
<h2><center>Library Management
System</center></h2><br><br>
<a href="Addbooks.jsp"><font color="black"><b>ADD
BOOKS </b></font></a><br><br>
<a href="Searchbooks.jsp"><font
color="black"><b>SEARCH BOOKS
</b></font></a><br><br>
</body>
</html>
Addbook.jsp
<html>
<head>
<title>ADD BOOKS JSP Page</title>
</head>
<body bgcolor="#ABCEEF">
<form action="Addbooksback.jsp"
method="post"><font color="black">
<br><br><br><center><h2>Book Details</h2>
<table border="1" cellspacing="%20">
<tr><td><b>Title:</b></td> <td><input type="text"
name="t1" size="15"></td></tr>
<tr><td><b>Author:</b></td> <td><input type="text"
name="t2" size="15"></td></tr>
<tr><td><b>Publication:</b></td> <td><input
type="text" name="t3" size="15"></td></tr>
<tr><td><b>Price:</b></td> <td><input type="text"
name="p1" size="15"></td></tr>
<tr><td><b>Year:</b></td><td><input type="text"
name="y2" size="15"></td></tr>
</table>
<br>
<b><input type="submit" name="sub" value="submit" >
</b>
</center>
</font>
</form>
AddBookback.jsp
<%@page import="java.sql.*"%>
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<% String title=request.getParameter("t1");
String author=request.getParameter("t2");
String publication=request.getParameter("t3");
int price=Integer.parseInt(request.getParameter("p1"));
String year=request.getParameter("y2");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection ("jdbc:odbc:lib");
PreparedStatement pstmt=con.prepareStatement("insert into
Book (title,author,publication,price,year)values(?,?,?,?,?)");
pstmt.setString(1,title);
pstmt.setString(2,author);
pstmt.setString(3,publication);
pstmt.setInt(4,price);
pstmt.setString(5,year);
pstmt.executeUpdate();
response.sendRedirect("information.jsp");
%>
</body>
</html>
Searchbook.jsp
<html>
<body bgcolor="#ABCEEF"><br><br><br>
<form action="Searchbooksback.jsp" method="post">
<font color="black">
<table border="1" align="center">
<tr><td><b>Title:<input type="text"
name="t1"></b></td></tr>
<tr><td><b><center><input type="submit"
name="sub" value="submit"></center</b></td></tr>
</table>
</font>
</form>
</body>
</html>
Searchbookback.jsp

<%@page import="java.sql.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP Page</title>
</head>
<body bgcolor="#ABCEEF">
<form action="LibraryManagement.jsp"
method="post">
<font color="black"><br><br>
<table border="1" align="left" cellpadding="10%">
<tr><td><b>Title</b></td><td><B>Author</B></td><td><B>
Publication</B></td><td><B>Price</td><td><B>Year
</B></td></tr>
<%
String title=request.getParameter("t1") ;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=
DriverManager.getConnection("jdbc:odbc:lib");
PreparedStatement pstmt=con.prepareStatement("select *
from book where title=?");
pstmt.setString(1,title);
ResultSet rs=pstmt.executeQuery();
while(rs.next()) { %>
<font color="black"><br><br>
<tr><td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getInt(4)%></td>
<td><%=rs.getString(5)%></td></tr>
</font> <%}%>
<tr><input type="submit" name="sub"
value="Back to HomePage"></tr>
</table>
</body>
</html>
Information.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="LibraryManagement.jsp"
method=post>
<h2> Book details updated successfully</h2>
<br> <input type="submit" name="back"
value="Back">
</form>
</body>
</html>
Employee Management System

 Problem Statement:
In this program add information about
Employee like emp_id, post, name , birth
date etc..
After storing information about Employee
we can search them by post .
Home Page
Add Page
Search Page
Details of Employee
Home page
Employeemanagement.jsp
<html>
<head>
<title>JSP Page</title>
</head>
<body bgcolor="pink">
<center><h2>Employee Management System
</h2></center><br><br><br>
<a href="AddEmployee.jsp"><font color="black">ADD
EMPLOYEE </font></a><br><br>
<a href="Searchemployee.jsp"><font
color="black">SEARCH EMPLOYEE
</font></a><br><br>
</body>
</html>
AddEmployee.jsp
<html>
<head>
<title>JSP Page</title>
</head>
<body bgcolor="pink"><br><br><br>
<form action="AddEmployeeback.jsp" method="post">
<table border="1" align="center">
<tr><td>Employee_ID:</td><td><input type="text"
name="w1"></td></tr>
<tr><td>Employee_Name:</td><td><input type="text"
name="w2"></td></tr>
<tr><td>National_ID:</td><td><input type="text"
name="w3"></td></tr>
<tr><td>Post:</td>
<td> <select name="w4">
<option>Engineer</option>
<option>Doctor</option>
<option>Mechanical</option>
<option>Proffessor</option>
</select>
</td><tr>
<tr><td>Birth_Date:</td><td><input type="text"
name="w5"></td></tr>
<tr><td>Marital:</td><td><input type="text"
name="w6"></td></tr>
<tr><td>Gender:</td><td><input type="text"
name="w7"></td></tr>
<tr><td>Highest Degree Earned:</td>
<td>
<select name="w8">
<option>HighSchool</option>
<option>Bachelor</option>
<option>Master</option>
<option>PhD</option>
</select>
</td></tr>
<tr><td colspan="2"><center><input type="submit"
name="sub" value="submit"></center></td></tr>

</table>
</form>
</body>
</html>
Addemployeeback.jsp
<%@page import="java.sql.*" %>
<html>
<body>
<% try
{ int
empid=Integer.parseInt(request.getParameter("w1"));
String empname=request.getParameter("w2");
int
natid=Integer.parseInt(request.getParameter("w3"));
String post=request.getParameter("w4");
String bdate=request.getParameter("w5");
String marital=request.getParameter("w6");
String gender=request.getParameter("w7");
String degree=request.getParameter("w8");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:empl");
PreparedStatement pstmt=con.prepareStatement ("insert
into Employee
(eid,ename,nid,post,bdate,marital,gender,degree)
values(?,?,?,?,?,?,?,?)");
pstmt.setInt(1,empid);
pstmt.setString(2,empname);
pstmt.setInt(3,natid);
pstmt.setString(4,post);
pstmt.setString(5,bdate);
pstmt.setString(6,marital);
pstmt.setString(7,gender);
pstmt.setString(8,degree);
pstmt.executeUpdate();
con.commit();
pstmt.close();
con.close();
response.sendRedirect("information.jsp");
} catch(Exception e)
{ out.println(e);
} %>
</body>
</html>
Information.jsp
<html>
<head>
<title>JSP Page</title>
</head>
<body>
<form action="Employeemanagement.jsp"
method="post">
<h2>Employee details updated successfully</h2>
<br>
<input type="submit" name="sub" value="Back">
</form>
</body>
</html>
Searchemployee.jsp
<html>
<head>
charset=UTF-8">
<title>JSP Page</title>
</head>
<body bgcolor="#ABCEEF"><br><br><br>
<form action="Searchemployeeback.jsp" method="post">
<font color="black"> <table border="1" align="center">
<tr><td><b>Post:</td> <td> <select name="w4">
<option>Engineer</option>
<option>Doctor</option>
<option>Mechanical</option>
<option>Proffessor</option>
</select> </td>
</table><br> <br>
<b><center><input type="submit" name="sub"
value="submit"></center></b>
</font>
</form>
</body>
</html>
Searchemployeeback.jsp
<%@page import="java.sql.*"%>
<html>
<title>JSP Page</title>
<body bgcolor="#ABCEEF">
<font color="black"><br><br>
<table border="1" align="center" cellpadding="10%">
<th colspan="8">Employee Details</th>

<tr><td><b>Employee_Id</b></td>
<td><b>Employee_Name</b></td>
<td><b>National_Id</td><td><b>Post</b></td><td><b>
Birth Date </b></td> <td> <b>Merital</b></td>
<td><b>Gender</b></td> <td> <b>Highest Degree</b>
</td></tr>
<%
String post=request.getParameter("w4");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection
con=DriverManager.getConnection("jdbc:odbc:empl");
PreparedStatement
pstmt=con.prepareStatement("select * from
Employee where post=?");
pstmt.setString(1,post);

ResultSet rs=pstmt.executeQuery();
while(rs.next()) { %>
<font color="black"><br><br>
<tr><td><%=rs.getInt(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getInt(3)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td>
<td><%=rs.getString(6)%></td>
<td><%=rs.getString(7)%></td>
<td><%=rs.getString(8)%></td>
</tr>
</font> <% } %>
</table><br><br>
<center> <a href="Employeemanagement.jsp"> <font
color="black">Back To Home Page </font></a></center>
</body>
</html>
Teaching Staff Information System

 This is Program in which we stored


information about staff in college like name,
salary, level, hire date, department etc.
 We can see all stored information at single
page, we can also delete, modify the stored
information
 we search record by choosing search by
department or search by level.
Home Page
Add Record
For Modify or Delete record
Search by Department
Information by Department
Search by Level
Information by Level
Home Page
Customermanagement.jsp
<html>
<body bgcolor="pink">
<center> <h2>Teaching Staff Management System</h2>
</center><br><br><br>
<a href="Addrecord.jsp"><font color="black"> 1.ADD RECORD
</font></a><br><br>
<a href="Listrecord.jsp"><font color="black">2.LIST RECORD
</font></a><br><br>
<a href="Modifyrecord.jsp"><font color="black">3.MODIFY RECORD
</font></a><br><br>
<a href="Deleterecord.jsp"><font color="black">4.DELETE RECORD
</font></a><br><br>
<a href="Searchrecord.jsp"><font color="black">5.SEARCH RECORD
</font></a><br><br>
</body>
</html>
Addrecord.jsp
<html>
<body bgcolor="pink">
<center><h2>Staff Information</h2><center>
<form action="Addrecordback.jsp" method="post">
<font color="black">
<br><br><br> <table border="1" align="center">
<tr><td>Staff Code:</td><td><input type=text maxlength="4"
name="c1"></td></tr>
<tr><td>Staff FirstName:</td><td><input type=text maxlength="25"
name="c2"></td></tr>
<tr><td>Staff Mid Initial:</td><td><input type=text maxlength="25"
name="c3"></td></tr>
<tr><td>Staff LastName:</td><td><input type=text maxlength="25"
name="c4"></td></tr>
<tr><td>Staff Work_Dept:</td><td><input type=text maxlength="25"
name="c5"></td></tr>
<tr><td>Staff PhonNo.:</td><td><input type=text maxlength="15"
name="c6"></td></tr>
<tr><td>Staff Hire Date:</td><td><input type=text maxlength="25"
name="c7"></td></tr>
<tr><td>Staff Level:</td><td><input type=text maxlength="25"
name="c8"></td></tr>
<tr><td>Staff Sex:</td><td><input type=text maxlength="25"
name="c9"></td></tr>
<tr><td>Staff BirthDate:</td><td><input type=text maxlength="25"
name="c10"></td></tr>
<tr><td>Staff Salary:</td><td><input type=text maxlength="25"
name="c11"></td></tr>
<tr><td colspan="2"><center><input type="submit" name="sub"
value="Submit"></center></td></tr>
</table>
</font>
</form>
</body>
</html>
Addrecordback.jsp
<%@page import="java.sql.*" %>
<html>
<body>
<%
try {
String scode=request.getParameter("c1");
String fname=request.getParameter("c2");
String minit=request.getParameter("c3");
String lname=request.getParameter("c4");
String wkdept=request.getParameter("c5");
String phnno=request.getParameter("c6");
String hdate=request.getParameter("c7");
String level=request.getParameter("c8");
String sex=request.getParameter("c9");
String bdate=request.getParameter("c10");
String salary=request.getParameter("c11");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:cust");
String sql1="insert into Customer
(Staff_Code,Staff_FName,Staff_MInit,Staff_LName,Staff_WkDept,S
taff_PNo,Staff_HDate,Staff_Level,Staff_Sex,Staff_BDate,Staff_Sala
ry) values (?,?,?,?,?,?,?,?,?,?,?)";
PreparedStatement psmt=con.prepareStatement(sql1);
psmt=con.prepareStatement(sql1);
psmt.setString(1,scode);
psmt.setString(2,fname);
psmt.setString(3,minit);
psmt.setString(4,lname);
psmt.setString(5,wkdept);
psmt.setString(6,phnno);
psmt.setString(7,hdate);
psmt.setString(8,level);
psmt.setString(9,sex);
psmt.setString(10,bdate);
psmt.setString(11,salary);
psmt.executeUpdate();
con.close();
response.sendRedirect("Back.jsp");
}
catch(Exception e)
{
out.println(e);
}
%>
</body>
</html>
Deleterecord.jsp
<html>
<body bgcolor="pink"><center><h4>Enter the Name and Code of Staff
to delete</h4> </center>
<form action="Deleterecordback.jsp" method="post">
<font color="black">
<br><br><br> <table border="1" align="center">
<tr><td>Staff Code:</td><td><input type=text maxlength="4"
name="c1"></td></tr>
<tr><td>Staff Name:</td><td><input type=text maxlength="25"
name="c2"></td></tr>
<tr><td colspan="2"><center><input type="submit" name="sub"
value="Submit"></center></td></tr>
</table>
</font>
</form>
</body>
</html>
Deleterecordback.jsp
<%@page import="java.sql.*"%>
<html>
<body>
<%
try {
String scode=request.getParameter("c1");
String fname=request.getParameter("c2");
String minit=request.getParameter("c3");
String lname=request.getParameter("c4");
String wkdept=request.getParameter("c5");
String phnno=request.getParameter("c6");
String hdate=request.getParameter("c7");
String level=request.getParameter("c8");
String sex=request.getParameter("c9");
String bdate=request.getParameter("c10");
String salary=request.getParameter("c11");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:cust");
String sql1="delete from Customer where Staff_Code=?
and Staff_FName=?";
PreparedStatement psmt=con.prepareStatement(sql1);
psmt=con.prepareStatement(sql1);
psmt.setString(1,scode);
psmt.setString(2,fname);
psmt.executeUpdate();
con.close();
response.sendRedirect("Backinformation.jsp");
}
catch(Exception e)
{
out.println(e);
}
%>
</body>
</html>
Listrecord.jsp
<%@page import="java.sql.*"%>
<html>
<body>
<table border="1" align="center" cellpadding="10%">
<th colspan="11"><h2>Staff Information</h2></th>
<tr bgcolor="yellow"><td><b>Staff Code</b></td><td><b>First
Name</b></td><td><b>MInit Name</td><td><b>Last Name
</b></td><td><b>Work Dept</b></td><td><b>Phone No
</b></td><td><b>HireDate </b></td><td><b> Level
</b></td><td><b>Sex</b></td><td><b>Birth Date
</b></td><td><b>Salary</b></td></tr>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:cust");
String sql1="select * from Customer";
PreparedStatement psmt=con.prepareStatement(sql1);
ResultSet r=psmt.executeQuery();
while(r.next()) { %>
<tr bgcolor="pink"><td><%=r.getString(1)%></td>
<td><%=r.getString(2)%></td>
<td><%=r.getString(3)%></td>
<td><%=r.getString(4)%></td>
<td><%=r.getString(5)%></td>
<td><%=r.getString(6)%></td>
<td><%=r.getString(7)%></td>
<td><%=r.getString(8)%></td>
<td><%=r.getString(9)%></td>
<td><%=r.getString(10)%></td>
<td><%=r.getString(11)%></td>
</tr>
<%}%>
</table><br><br>
<center><a href="Customermanagement.jsp"><font
color="black"><h3><b>Back To Home
Page</b></h3></font></a></center>
</body>
</html>
Modifyrecord.jsp
<html>
<body bgcolor="pink"> <center><h4>Enter the Name and Code of
customer to modify </h4></center>
<form action="Modifycustomerrec.jsp" method="post">
<font color="black">
<br><br><br> <table border="1" align="center">
<tr><td>Staff Code:</td><td><input type=text maxlength="4“
name="c1"></td></tr>
<tr><td>Staff Name:</td><td><input type=text maxlength="25"
name="c2"></td></tr>
<tr><td colspan="2"><center><input type="submit" name="sub"
value="Submit"></center></td></tr>

</table>
</font>
</form>
</body>
</html>
Modifyrecordback.jsp
<%@page import="java.sql.*"%>
<html>
<body bgcolor="pink">
<%
try {
String scode=request.getParameter("q1");
String fname=request.getParameter("q2");
String minit=request.getParameter("q3");
String lname=request.getParameter("q4");
String wkdept=request.getParameter("q5");
String phnno=request.getParameter("q6");
String hdate=request.getParameter("q7");
String level=request.getParameter("q8");
String sex=request.getParameter("q9");
String bdate=request.getParameter("q10");
String salary=request.getParameter("q11");
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:cust");
String sql1="update Customer set
Staff_Code=?,Staff_FName=?,Staff_MInit=?,Staff_LName=?,Staff_Wk
Dept=?,Staff_PNo=?,Staff_HDate=?,Staff_Level=?,Staff_Sex=?,Sta
ff_BDate=?,Staff_Salary=? where Staff_Code=? and
Staff_FName=?";
PreparedStatement psmt=con.prepareStatement(sql1);
psmt.setString(1,scode);
psmt.setString(2,fname);
psmt.setString(3,minit);
psmt.setString(4,lname);
psmt.setString(5,wkdept);
psmt.setString(6,phnno);
psmt.setString(7,hdate);
psmt.setString(8,level);
psmt.setString(9,sex);
psmt.setString(10,bdate);
psmt.setString(11,salary);
String staff_code=(String)session.getAttribute("c1");
String staff_fname=(String)session.getAttribute("c2");
psmt.setString(12,staff_code);
psmt.setString(13,staff_fname);
psmt.executeUpdate();
psmt.close();
con.close();
}
catch(Exception e)
{
out.println(e);
}
%>
<center>Staff Record Successfully<br><br>
<a href="Customermanagement.jsp">Back </a></center>
</body>
</html>
Searchrecord.jsp
<html>
<body bgcolor="#ABCEEF"><br><br><br>
<form action="Searchrecordback.jsp" method="post">
<font color="black">
<table border="1" align="center">
<tr><td><b>Search By:<select name="t1">
<option>Department</option>
<option>Level</option>
</select></b></td></tr>
<tr><td><b><center><input type="submit" name="sub"
value="submit"></center</b></td></tr>

</table>
</font>
</form>
</body>
</html>
Searchrecordback.jsp
<%@page import="java.sql.*"%>
<html>
<body bgcolor="#ABCEEF">
<form action="deptoption.jsp" method="post">
<%
String searchby=request.getParameter("t1");
if(searchby.equalsIgnoreCase("department"))
{%>
<h3>Select Department</h3>
Electronics<input type="radio" name="r1" value="Electronics"><br>
Mechanical <input type="radio" name="r1" value="Mechanical"><br>
Civil<input type="radio" name="r1" value="Civil"><br>
Electrical<input type="radio" name="r1" value="Electrical"><br>
Textile<input type="radio" name="r1" value="Textile"><br>
Computer<input type="radio" name="r1" value="Computer"><br><br>
<input type="submit" value="Submit">
<%}
else
{
%>
<h3>Select Level</h3>
Lecturer <input type="radio" name="r1" value="Lecturer"><br>
Teacher <input type="radio" name="r1" value="Teacher"><br>
Professor <input type="radio" name="r1" value="Professor">
<br><br>
<input type="submit" value="Submit">
<%
}%>
</form>
</body>
</html>
deoption.jsp
<%@page import="java.sql.*"%>
<html>
<body bgcolor="#ABCEEF">
<table border="1" align="center" cellpadding="10%">
<th colspan="11"><h2>Staff Information</h2></th>
<tr bgcolor="yellow"><td><b>Staff Code</b></td><td><b>First
Name</b></td><td><b>MInit Name</td><td><b>Last
Name</b></td><td><b>Work Dept</b></td><td><b>Phone
No</b></td><td><b>Hire
Date</b></td><td><b>Level</b></td><td><b>Sex</b></td><td><b>Bi
rth Date</b></td><td><b>Salary</b></td></tr>
<% try{
String deptname=request.getParameter("r1");
if((deptname.equalsIgnoreCase("Computer"))||
(deptname.equalsIgnoreCase("Civil")) ||
(deptname.equalsIgnoreCase("Mechanical"))||
(deptname.equalsIgnoreCase("Textile"))||
(deptname.equalsIgnoreCase("Electronic"))||
(deptname.equalsIgnoreCase("Electrical")))
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:cust");
String sql1="select * from Customer where Staff_WkDept=?";
PreparedStatement psmt=con.prepareStatement(sql1);
psmt.setString(1,deptname);
ResultSet rs=psmt.executeQuery();
while(rs.next())
{ %> <tr bgcolor="pink"><td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td>
<td><%=rs.getString(6)%></td>
<td><%=rs.getString(7)%></td>
<td><%=rs.getString(8)%></td>
<td><%=rs.getString(9)%></td>
<td><%=rs.getString(10)%></td>
<td><%=rs.getString(11)%></td>
</tr>
<% } psmt.close(); con.close();
}
else
{ String level=request.getParameter("r1");
if((deptname.equalsIgnoreCase("Professor"))||
(deptname.equalsIgnoreCase("Lecturer")) ||
(deptname.equalsIgnoreCase("Teacher")) )
{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:cust");
String sql1="select * from Customer where Staff_Level=?";
PreparedStatement psmt=con.prepareStatement(sql1);
psmt.setString(1,level);
ResultSet rs=psmt.executeQuery();
while(rs.next())
{ %>
<tr bgcolor="pink"><td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td>
<td><%=rs.getString(6)%></td>
<td><%=rs.getString(7)%></td>
<td><%=rs.getString(8)%></td>
<td><%=rs.getString(9)%></td>
<td><%=rs.getString(10)%></td>
<td><%=rs.getString(11)%></td>
</tr>
<% }
psmt.close();
con.close();
} }}
catch(Exception e)
{ out.println(e);
}
%>
</table>
<center><a href="Customermanagement.jsp">
<font color="black"><h3><b>Back To Home Page
</b></h3></font></a></center>
</body>
</html>
Program for “Baby Game”
Input Form
Conti…
Output Form:
Home Page
BabyGame.jsp
<html>
<head>
<title>Baby Game Page</title>
</head>
<body bgcolor="#FFFFFF">
<form method="post" action="BabyGame1.jsp"
name="">
<center>
<h3>Baby Game</h3>
</center>
<br>
<table border cols=5 width="75%" >
<caption>Please enter your own name:
<input type="text" name="guesser"></caption><tr><td>
<br><input type="radio" name="gender" value="Female"
checked>Female
<p><input type="radio" name="gender" value="Male"
checked> Male
</p></td>

<td><font size=-1>Please choose a Date:</font>


<p>Month:<select name="month">
<option value="January">January</option>
<option value="February">February</option>
<option value="December">December</option>
<br></select>
<p>Day:<select name="day">
<option value="1">1</option>
<option value="2">2</option>
<option value="31">31</option>
<br></select>
<td><font size=-1>Please choose a weight:</font>
<p>Pounds:<select name="pounds">
<option value="5">5</option>
<option value="6">6</option>
<option value="12">12</option>
<br></select>
<p>Ounces:<select name="ounces">
<option value="1">1</option>
<option value="2">2</option>
<option value="15">15</option>
<br></select>
<td><font size=-1>Please choose a length:</font>
<p>Inches:<select name="length">
<option value="14">14</option>
<option value="15">15</option>
<option value="26">26</option>
</select>
</table>
<center>
<p><input type="submit" name="submit" value="Make
Guess" >
<input type="reset" name="reset" value="reset">
</center>
<br></form>
</body>
</html>
BabyGame1.jsp
<html>
<head>
<title>Baby Game Page</title>
</head>
<body bgcolor="#FFFFFF">
<%@ page import="java.util.*,java.io.*" %>
<%
String guesser=request.getParameter("guesser");
String gender=request.getParameter("gender");
String pounds=request.getParameter("pounds");
String ounces=request.getParameter("ounces");
String month=request.getParameter("month");
String day=request.getParameter("day");
String length=request.getParameter("length");
if (guesser==null || gender==null || pounds==null || month==
null | day==null || length==null)
{ %>
<br>There were some choice that were not selected <br>
Sorry, but you must complete all selections to play.<br>
<font size=-1>(Please hit the browser 'back' button to
continue) </font><br>
<% }
else {
//store guess info and display ti user
Properties p=new Properties();
p.setProperty("guesser",guesser);
p.setProperty("gender",gender);
p.setProperty("pounds",pounds);
p.setProperty("ounces",ounces);
p.setProperty("month",month);
p.setProperty("day",day);
p.setProperty("length",length);
FileOutputStream outer= new FileOutputStream(guesser);
p.store(outer,"Baby Game--"+guesser+"'s guesses");
outer.flush();
outer.close();
%>
<br><%=guesser%>, your choices have been stored.<br>
Here they are:<br>
<table border cols=5 width="75%">
<caption></caption>
<tr>
<td><%=gender %></td>
<td><%=pounds %> lbs<%=ounces %> oz</td>
<td><%=month %><%=day%></td>
<td><%=length %>inches</td>
</tr>
</table> <br>
<%
} %>
</body>
</html>
Voter Registration
 In that we were using page-centric approach
to building this web application
 We have a form with input fields, required for
executing some business process such as
posting a message, placing an online order,
placing a stock trade etc in our case
registering and voting for a candidate
 The form is submitted to the workhorse JSP,
which execute the required JDBC logic to
process the action.
Voter Registration
Conti….
Conti…
Conti…
Detail Page
Refrences:

You might also like