Vous êtes sur la page 1sur 48

KCE Web Application Development using java ROLL NO:16L120

Ex no: 1
Basic of Servlet
Date :

Question:
Write a servlet to display “My First Servlet program” on browser.
Aim:
To Write a servlet program to display “My First Servlet program” on browser
Code:
Index.html:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="TestServlet " method="get">
name:<input type="text" name="name"><br>
password:<input type="password" name="pwd"><br>
<input type="submit" value="register">
</body>
</html>
</form>

TestServlet:
package com.ece.login.service;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class TestServlet
*/
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {
PrintWriter pw=response.getWriter();

ECE Page | 1
KCE Web Application Development using java ROLL NO:16L120

response.setContentType("text/html");
String iname=request.getParameter("name");
String ipassword=request.getParameter("pwd");
if((iname.equals("madhu"))&&(ipassword.equals("madhu")))
pw.println("My first servlet program");
else
pw.println("error");

Output:

ECE Page | 2
KCE Web Application Development using java ROLL NO:16L120

Result
Thus the web page to print “My Frist Servlet program “ has been successfully developed and the
output was verified.

Ex no: 2 Servlet request headers and its value

ECE Page | 3
KCE Web Application Development using java ROLL NO:16L120

Date :

Question:
Create a servlet that prints all the request headers it receives, along with their associated values.
INCLUDEPICTURE
"https://www.novell.com/documentation/novellaccessmanager312/policies/graphics/form_fill_test_a.png"
\* MERGEFORMATINET INCLUDEPICTURE
"https://www.novell.com/documentation/novellaccessmanager312/policies/graphics/form_fill_test_a.png"
\* MERGEFORMATINET
Aim
To Create a servlet that prints all the request headers it receives, along with their associated values.
Code
Index1.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="TestServlet" method="get">
name:
<input type="text" name="name"><br>
password:
<input type="password" name="pwd"><br>
dept:
<input type="checkbox" value="ece" name="dept">ece
<input type="checkbox" value="cse" name="dept">cse<br>
gender:
<input type="radio" value="male" name="gender">male
<input type="radio" value="female" name="gender">female<br>
<input type="submit" value="register">
</body>
</html>
</form>

package com.ece.login.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

ECE Page | 4
KCE Web Application Development using java ROLL NO:16L120

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class TestServlet
*/
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {
PrintWriter pw=response.getWriter();
response.setContentType("text/html");
pw.println("<body bgcolor=\"red\">");
Enumeration<String> reqHeaders = request.getHeaderNames();
while(reqHeaders.hasMoreElements()){
String name=(String)reqHeaders.nextElement();
String password=request.getHeader("pwd");
pw.println("<b> "+name + "</b>"+ " = " +password+"<br>");

}
}
}

OUTPUT:

ECE Page | 5
KCE Web Application Development using java ROLL NO:16L120

INCLUDEPICTURE
"https://www.novell.com/documentation/novellaccessmanager312/policies/graphics/form_fill_test_a.png"
\* MERGEFORMATINET INCLUDEPICTURE
"https://www.novell.com/documentation/novellaccessmanager312/policies/graphics/form_fill_test_a.png"
\* MERGEFORMATINET INCLUDEPICTURE
"https://www.novell.com/documentation/novellaccessmanager312/policies/graphics/form_fill_test_a.png"
\* MERGEFORMATINET

ECE Page | 6
KCE Web Application Development using java ROLL NO:16L120

Result
Thus the servlet program to print all the request headers it recieves and its value has been
successfully printed and the output was verified.

Question
Write a servlet to show all the parameters sent to the servlet via either GET or POST.consider all types of
form fields.

Aim
Write a servlet to show all the parameters sent to the servlet via either GET or POST.

Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="TestServlet" method="get">
name:
<input type="text" name="name"><br>
password:
<input type="password" name="pwd"><br>
dept:
<input type="checkbox" value="ece" name="dept">ece
<input type="checkbox" value="cse" name="dept">cse<br>
gender:
<input type="radio" value="male" name="gender">male
<input type="radio" value="female" name="gender">female<br>
<input type="submit" value="register">
</body>
</html>
</form>

Servlet.java:
package com.ece.login.service;
import java.io.IOException;

ECE Page | 7
KCE Web Application Development using java ROLL NO:16L120

import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {

response.setContentType("text/html");
PrintWriter pw=response.getWriter();
Enumeration<String>reqparams=request.getParameterNames();
while(reqparams.hasMoreElements()){
String name=(String)reqparams.nextElement();
String value=request.getParameter(name);
pw.println(value);

OUTPUT:

ECE Page | 8
KCE Web Application Development using java ROLL NO:16L120

Result:
Thus the program to show all the parameters sent to the servlet via either GET or POST is successfully
printed and output is verified successfully.

ECE Page | 9
KCE Web Application Development using java ROLL NO:16L120

Ex no: 4 Servlet Config and Servlet Context Parameters


Date :

Question
Write a Servlet Program that prints the Servlet Config and Servlet Context Parameters.
INCLUDEPICTURE
"https://www.novell.com/documentation/novellaccessmanager312/policies/graphics/form_fill_test_a.png"
\* MERGEFORMATINET INCLUDEPICTURE
"https://www.novell.com/documentation/novellaccessmanager312/policies/graphics/form_fill_test_a.png"
\* MERGEFORMATINET
Aim
To Write a Servlet Program that prints the Servlet Config and Servlet Context Parameters.

Code
Services.html
<form name="mylogin" action="#" method="post">
<table align="center" border="0" cellpadding="4" cellspacing="4">
<tr align="center" valign="top">
<td>
<table align="center" border="0">

<tr align="left">
<td>Username:</td>
<td><input type="text" name="username" size="30"></td>
</tr>

<tr align="left">
<td>Password:</td>
<td><input type="password" name="password" size="30">
</td>
</tr>

<tr align="left">
<td>City of<br>Employment:</td>
<td><input type="text" name="city" size="30"></td>
</tr>

<tr align="left">
<td>Web server:</td>
<td>
<select name="webserv" size="1">
<option value="default" selected>
--- Choose a server ---
</option>
<option value="Human Resources">
Human Resources
</option>
<option value="Development">

ECE Page | 10
KCE Web Application Development using java ROLL NO:16L120

Development
</option>
<option value="Accounting">
Accounting
</option>
<option value="Sales">
Sales
</option>
</select>
</td>
</tr>
<tr>
<td colspan="2" align="left" height="25" valign="top">
<p></p>
</td>
</tr>

<tr align="left">
<td>Please specify<br>your role:</td>
<td>
<input name="role" value="admin" type="radio">
Admin<br>
<input name="role" value="engineer" type="radio">
Engineer<br>
<input name="role" value="manager" type="radio">
Manager<br>
<input name="role" value="guest" type="radio">Guest
</td>
</tr>

<tr>
<td colspan="2" align="left" height="25" valign="top"
width="121">
<p></p>
</td>
</tr>

<tr align="left">
<td>Single Sign-on<br>to the following:</td>
<td>
<input name="mail" type="checkbox">Mail<br>
<input name="payroll" type="checkbox">Payroll<br>
<input name="selfservice" type="checkbox">
Self-service<br>
</td>
</tr>
</table>
</td>
</tr>

ECE Page | 11
KCE Web Application Development using java ROLL NO:16L120

<tr>
<td colspan="2" align="center">
<input value="Login" type="submit">
<input type="reset">
</td>
</tr>
</table>
</form>
</body>
</html>

Output:

ECE Page | 12
KCE Web Application Development using java ROLL NO:16L120

Result

ECE Page | 13
KCE Web Application Development using java ROLL NO:16L120

Thus the progarm to prints the Servlet Config and Servlet Context Parameters was printed and output is
verified successfully.

Ex no: 5
Servlet cookies -I
Date :

Question
Create a Servlet that recognizes the first time visitor to a web application and responds by
saying "Welcome, you are visiting for the first time". When the page is visited for the second time, it
should say 'Welcome Back'.

INCLUDEPICTURE
"https://www.novell.com/documentation/novellaccessmanager312/policies/graphics/form_fill_test_a.png"
\* MERGEFORMATINET INCLUDEPICTURE
"https://www.novell.com/documentation/novellaccessmanager312/policies/graphics/form_fill_test_a.png"
\* MERGEFORMATINET
Aim
To Create a Servlet that recognizes the first time visitor to a web application and responds by saying
"Welcome, you are visiting for the first time".When the page is visited for the second time, it should say
'Welcome Back'.

Code
Session.java

package com.ece.login.service;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
* Servlet implementation class Session
*/
@WebServlet("/Session")
public class Session extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws


ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());

ECE Page | 14
KCE Web Application Development using java ROLL NO:16L120

HttpSession session = request.getSession(true);


Date createTime = new Date(session.getCreationTime());

Date lastAccessTime = new Date(session.getLastAccessedTime());

String title = "Welcome Back";


Integer visitCount = new Integer(0);
String visitCountKey = new String("visitCount");

if (session.isNew()) {
title = "Welcome to my site";

} else {
visitCount = (Integer)session.getAttribute(visitCountKey);
visitCount = visitCount + 1;

}
session.setAttribute(visitCountKey, visitCount);
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";

out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n"+
"<body> "+visitCount+"</body></html>\n");
}

ECE Page | 15
KCE Web Application Development using java ROLL NO:16L120

Output

If you run the servlet again

‘WELCOME BACK’

ECE Page | 16
KCE Web Application Development using java ROLL NO:16L120

Result
Thus the program to recognizes the usage of cookies is exicuted and the output is verified
successfully.

Ex no: 6
Servlet cookies -II
Date :

Question
Display all the cookies available for the given application in a tabular format.
If there are no cookies available with the application, display “No Cookies”.
INCLUDEPICTURE
"https://www.novell.com/documentation/novellaccessmanager312/policies/graphics/form_fill_test_a.png"
\* MERGEFORMATINET INCLUDEPICTURE
"https://www.novell.com/documentation/novellaccessmanager312/policies/graphics/form_fill_test_a.png"
\* MERGEFORMATINET
Aim
To Display all the cookies available for the given application in a tabular format.
If there are no cookies available with the application, display “No Cookies”.

Code
Cookies.html
package com.ece.login.service;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class Cookies
*/
@WebServlet("/Cookies")
public class Cookies extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Cookies() {
super();
// TODO Auto-generated constructor stub

ECE Page | 17
KCE Web Application Development using java ROLL NO:16L120

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());

Cookie cookie = null;


Cookie[] cookies = null;

cookies = request.getCookies();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String firstname=request.getParameter("first_name");
String lastname=request.getParameter("last_name");
Cookie firstName = null;
response.addCookie( firstName );
Cookie lastName = null;
response.addCookie( lastName );

String title = "Reading cookies";


String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";

out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body>" );

if( cookies != null ) {


out.println("<h2> Found Cookies Name and Value</h2>");

for (int i = 0; i < cookies.length; i++) {


cookie = cookies[i];

out.print("<table> <tr> founded cookies </tr>");


out.print("<tr><td>First name : " + cookie.getName( ) + ", </td>");
out.print("<td>last name: " + cookie.getValue( ) + " </td></tr></table");
}
} else {
out.println("<h2>No cookies founds</h2>");
}
out.println("</body>");
out.println("</html>");

ECE Page | 18
KCE Web Application Development using java ROLL NO:16L120

ECE Page | 19
KCE Web Application Development using java ROLL NO:16L120

Result
Thus the program display all the cookies available for the given application in a tabular format is
printed and output is verified successfully.

8.

Question
Create a HTML Page, which asks the user to enter a number in a text box. On clicking the submit
button, it places the request to a Servlet. The Servlet generates all Prime numbers which are less than the
given number and adds them to an Array List and forwards the control to a JSP page. The JSP page
iterates through the Array List and prints them in a Tabular format.

Aim
To Write a Servlet Program that prints the prime number using HTML and forward into jsp page and
print the prime numbers in array list.

Code

Prime.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form method="get" action="Primenum">
Enter the number:<input type="text" name="num"/><br>
<input type="submit" value="submit"/>
</body>
</html>

Primenumber.jsp:

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

ECE Page | 20
KCE Web Application Development using java ROLL NO:16L120

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"


"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
ArrayList<Integer> list = new ArrayList<Integer>();
list=(ArrayList<Integer>)request.getAttribute( "Primenum");
out.println(list);
%>

</body>
</html>

Primenumber.java:

package com.ece.login.service;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.ArrayList;

@WebServlet("/Primenumber")
public class Primenumber extends HttpServlet {
private static final long serialVersionUID = 1L;

public Primenumber() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// TODO Auto-generated method stub
//response.getWriter().append("Served at: ").append(request.getContextPath());
String prim=request.getParameter("num");
int primeno=Integer.parseInt(prim);
ArrayList<Integer> al = new ArrayList<Integer>();
for (int i=0;i<=primeno;i++) {
if (isPrime(i)) {
al.add(i);
}
}
request.setAttribute("Primenum", al);

getServletConfig().getServletContext().getRequestDispatcher("/Primenumber.jsp").forward(reque
st,response);
}
public static boolean isPrime(int n){
if (n <= 1) {

ECE Page | 21
KCE Web Application Development using java ROLL NO:16L120

return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}

OUTPUT:

Result:
Thus the Program that prints the prime number usig HTML and forward into jsp page and print the prime
numbers in array list is executed and output is verified successfully.

ECE Page | 22
KCE Web Application Development using java ROLL NO:16L120

9.
Question
Write a JSP program which when invoked will print today‟s date and time.

Aim
To Write a JSP program which when invoked will print today‟s date and time.

Code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import = "java.io.*,java.util.*, javax.servlet.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Date date=new Date();
out.print(date.toString());
%>

</body>
</html>

ECE Page | 23
KCE Web Application Development using java ROLL NO:16L120

Output:

Result:

Thus the program to write a JSP program which when invoked will print todays date and time is printed
successfully.

ECE Page | 24
KCE Web Application Development using java ROLL NO:16L120

10.

Question:
Write a jsp program that prints a table with numbers and their corresponding factorial values.

Aim
To Write a jsp program that prints a table with numbers and their corresponding factorial values

Code:

ECE Page | 25
KCE Web Application Development using java ROLL NO:16L120

Ex no 12 Forwarding the files


Date :

Question
Create a servlet that forwards the request to one of three different JSP pages, depending on the
value of the operation request parameter. Say if input Is <10 then page 1 or greater than 10 and less than
99 then page 2 otherwise error page
Aim
To Create a servlet that forwards the request to one of three different JSP pages, depending on the
value of the operation request parameter. Say if input Is <10 then page 1 or greater than 10 and less than
99 then page 2 otherwise error page
Code

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="/JspDemo16f101/processJSP.jsp" method="post">
Enter Num: <input type="text" name="num" id="num"/>

<input type="submit" name="submit" value="submit"/>


</form>
</body>
</html>

Jsp file
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"


"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

ECE Page | 26
KCE Web Application Development using java ROLL NO:16L120

<%
String num=Integer.parseInt(request.getParameter("num"));

%>

<%
if(num>=9)
{
%>
<jsp:forward page="/successPage.jsp"/>

<%
}
else if(num<=10 && num>=99)
{
%>
<jsp:include page="/failPage.jsp"/>
<%} %>

</body>
</html>

Success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
this is success page
<%="Sucess" %>

</body>
</html>

Failiure page
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

ECE Page | 27
KCE Web Application Development using java ROLL NO:16L120

this is fail page


<%="failure" %>
</body>
</html>

OUTPUT

ECE Page | 28
KCE Web Application Development using java ROLL NO:16L120

Result
Thus the progarm to Create a servlet that forwards the request to one of three different JSP pages,
depending on the value of the operation request parameter. Say if input Is <10 then page 1 or greater than 10 and
less than 99 then page 2 otherwise error page was printed and output is verified successfully.

Ex no: 19
Hibernate database
Date :

Question
Using Hibernate create a Product table in the backend and insert 5 Product objects into the database.
The following are the details of the Product table. ProductName varchar2 ProductId Number Price
Number The value of the ProductId should be taken from a sequence which is created at the back end
Aim
To create a hibernate Product table in the backend and insert 5 Product objects into the database. The
following are the details of the Product table. ProductName varchar2 ProductId Number Price Number
The value of the ProductId should be taken from a sequence which is created at the back end

Code
HibernateUtil.java
package com.wipro;

import org.hibernate.cfg.Configuration;
import org.hibernate.SessionFactory;

public class HibernateUtil {


private static final SessionFactory sessionFactory;

static {
try {
sessionFactory = new
Configuration().configure("hiber.cfg1.xml").buildSessionFactory();
}
catch(Exception ex) {
System.out.println("Initial SessionFactory creation failed :"+ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;

}
}
Pricenum.java
package com.wipro;
import java.io.IOException;
import javax.servlet.ServletException;

ECE Page | 29
KCE Web Application Development using java ROLL NO:16L120

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.Transaction;
@WebServlet("/Price_num")
public class Price_num extends HttpServlet
{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = null;
try {
String no1=request.getParameter("no");
int no=Integer.parseInt(no1);
String name=request.getParameter("name");
String colour=request.getParameter("colour");
tx = session.beginTransaction();
Price s1 = new Price();
s1.setFlowerid(no);
s1.setFlowerName(name);
s1.setFlowercolor(colour);
session.save(s1);
tx.commit();
}
catch(Exception ex){
System.out.println(ex);
}
finally {
session.close();
}
}

}
Price.java
package com.wipro;

public class Price


{
private int Flowerid;
private String FlowerName;
private String Flowercolor;

public Price(int flowerid, String flowerName, String flowercolor) {


Flowerid = flowerid;
FlowerName = flowerName;
Flowercolor = flowercolor;

ECE Page | 30
KCE Web Application Development using java ROLL NO:16L120

}
public Price() {
// TODO Auto-generated constructor stub
}
public int getFlowerid() {
return Flowerid;
}
public void setFlowerid(int flowerid) {
Flowerid = flowerid;
}
public String getFlowerName() {
return FlowerName;
}
public void setFlowerName(String flowerName) {
FlowerName = flowerName;
}
public String getFlowercolor() {
return Flowercolor;
}
public void setFlowercolor(String flowercolor) {
Flowercolor = flowercolor;
}

}
Hibernate.cfg1.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" -->
<!DOCTYPE hibernate-configuration SYSTEM "classpath://org/hibernate/hibernate-configuration-
3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Oracle dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.connection.password">system</property>
<!-- Echo all executed SQL to stdout -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="Price.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Price.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>

ECE Page | 31
KCE Web Application Development using java ROLL NO:16L120

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"


"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.wipro.Price" table="Flower38">
<id name="Flowerid" column="Flowerid" type="int">
<generator class="assigned" >
<!-- param name="sequence">xmlseq</param -->
</generator>
</id>
<property name="FlowerName" column="florname" type="string" length="25"/>
<property name="Flowercolor" column="Florcolor" type="string" length="25"/>
</class>
</hibernate-mapping>
Hy.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="Price_num" method="get">
Number:<input type="number" name="no"/><br>
Flower Name:<input type="text" name="name"/><br>
Flower color:<input type="text" name="colour"/><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Output

ECE Page | 32
KCE Web Application Development using java ROLL NO:16L120

Result
Thus the Program to store the values in database is stored and executed and output is verified successfully.
Ex no: 21
Hibernate database
Date :

Question
Write a Hibernate Program that will ask the user to enter an id and price. Using that the corresponding
Flower's price need to be updated. After updating, show that the value is updated, by displaying that
record
Aim
To Write a Hibernate Program that will ask the user to enter an id and price. Using that the
corresponding Flower's price need to be updated. After updating, show that the value is updated, by
displaying that record

Code
package com.wipro;
import java.util.Scanner;
public class Price1 {
public static void main(String[] args) {
Price_num price=new Price_num();
Scanner sin=new Scanner(System.in);
System.out.println("enter the flowerid:");
int flowerid=sin.nextInt();
System.out.println("enter the flowercolour:");
String flowercolor=sin.next();
System.out.println("enter the flowername :");
String flowerName=sin.next();
Price pri=new Price();
pri.setFlowercolor(flowercolor);
pri.setFlowerid(flowerid);
pri.setFlowerName(flowerName);
price.insert(pri);
price.display();
System.out.println("enter flowerid to be updated");
int flowerid1=sin.nextInt();
System.out.println("enter flowername to be updated");
String flowername=sin.next();
price.update(flowerid1,flowername);
price.display();
}
}
Pricenum.java
package com.wipro;

import java.io.IOException;
import java.util.List;

ECE Page | 33
KCE Web Application Development using java ROLL NO:16L120

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
@WebServlet("/Price_num")
public class Price_num extends HttpServlet
{
SessionFactory sеssionFactory;
Session session;
Transaction tx;

public Price_num() {
sеssionFactory = new Configuration().configure("hiber.cfg3.xml").buildSessionFactory();

public void insert(Price pri)


{
session = sеssionFactory.openSession();
tx = session.beginTransaction();
try
{
session.save(pri);
tx.commit();
session.close();
System.out.println("value inserted");

}
catch(Exception e)
{
System.out.println("error at insert"+e);
}
}

public void update(int flowerid,String flowername)


{
try
{
session = sеssionFactory.openSession();
tx = session.beginTransaction();
Price pri = (Price)session.get(Price.class,flowerid);
pri.setFlowerName(flowername);
session.update(pri);
tx.commit();
session.close();

ECE Page | 34
KCE Web Application Development using java ROLL NO:16L120

System.out.println("value updated");
}
catch(Exception e)
{
System.out.println("error at update"+e);
}
}

public void display()


{
session = sеssionFactory.openSession();
tx = session.beginTransaction();
try
{
List<Price> pricelist= session.createQuery("FROM Price").list();
for(Price result:pricelist)
{
System.out.println(result);
}
session.close();
}
catch(Exception e)
{
System.out.println("error at display"+e);
}
}

}
}
Price.java
package com.wipro;

public class Price


{
private int Flowerid;
private String FlowerName;
private String Flowercolor;

public Price(int flowerid, String flowerName, String flowercolor) {


Flowerid = flowerid;
FlowerName = flowerName;
Flowercolor = flowercolor;
}
public Price() {
// TODO Auto-generated constructor stub
}
public int getFlowerid() {
return Flowerid;
}
public void setFlowerid(int flowerid) {
Flowerid = flowerid;
}
public String getFlowerName() {

ECE Page | 35
KCE Web Application Development using java ROLL NO:16L120

return FlowerName;
}
public void setFlowerName(String flowerName) {
FlowerName = flowerName;
}
public String getFlowercolor() {
return Flowercolor;
}
public void setFlowercolor(String flowercolor) {
Flowercolor = flowercolor;
}

}
Hibernate.cfg1.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" -->
<!DOCTYPE hibernate-configuration SYSTEM "classpath://org/hibernate/hibernate-configuration-
3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.connection.password">system</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="Price.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Price.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.wipro.Price" table="Price">
<id name="Flowerid" column="Flowerid" type="int">
<generator class="assigned" >

</generator>
</id>
<property name="FlowerName" column="florname" type="string" length="25"/>
<property name="Flowercolor" column="Florcolor" type="string" length="25"/>
</class>
</hibernate-mapping>
Hy.html

ECE Page | 36
KCE Web Application Development using java ROLL NO:16L120

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="Price_num" method="get">
Number:<input type="number" name="no"/><br>
Flower Name:<input type="text" name="name"/><br>
Flower color:<input type="text" name="colour"/><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Output

INCLUDEPICTURE
"https://www.novell.com/documentation/novellaccessmanager312/policies/graphics/form_fill_test_a.png"
\* MERGEFORMATINET INCLUDEPICTURE
"https://www.novell.com/documentation/novellaccessmanager312/policies/graphics/form_fill_test_a.png"
\* MERGEFORMATINET INCLUDEPICTURE
"https://www.novell.com/documentation/novellaccessmanager312/policies/graphics/form_fill_test_a.png"
\* MERGEFORMATINET

ECE Page | 37
KCE Web Application Development using java ROLL NO:16L120

Result
Thus the Program to store the values in database is stored and executed and output is verified
successfully.

Ex no: 20
Hibernate with database
Date :

Question:
Write a Hibernate Program to Delete a Flower with its Id from the table that you have created earlier. If
there is no Flower with that id exists, then an appropriate error message needs to be stored to the user.
Aim:
Write a Hibernate Program to Delete a Flower with its Id from the table that you have created earlier. If
there is no Flower with that id exists, then an appropriate error message needs to be stored to the user.
Code:
Price_num.java
package com.wipro;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
@WebServlet("/Price_num")
public class Price_num extends HttpServlet
{
SessionFactory sеssionFactory;
Session session;
Transaction tx;

public Price_num() {
sеssionFactory = new
Configuration().configure("hiber.cfg2.xml").buildSessionFactory();
}
public void insert(Price pri)
{

ECE Page | 38
KCE Web Application Development using java ROLL NO:16L120

session = sеssionFactory.openSession();
tx = session.beginTransaction();
try
{
session.save(pri);
tx.commit();
session.close();
System.out.println("value inserted");

}
catch(Exception e)
{
System.out.println("error at insert"+e);
}
}
public void delete(int flowerid)
{
session = sеssionFactory.openSession();
tx = session.beginTransaction();
try
{
Price pri= (Price)session.get(Price.class, flowerid);
session.delete(pri);
tx.commit();
session.close();
System.out.println("value deleted");
}
catch(Exception e)
{
System.out.println("error at display"+e);
}
}

Price.java
package com.wipro;
public class Price
{
private int Flowerid;
private String FlowerName;
private String Flowercolor;

public Price(int flowerid, String flowerName, String flowercolor) {


Flowerid = flowerid;
FlowerName = flowerName;
Flowercolor = flowercolor;
}
public Price() {
// TODO Auto-generated constructor stub

ECE Page | 39
KCE Web Application Development using java ROLL NO:16L120

}
public int getFlowerid() {
return Flowerid;
}
public void setFlowerid(int flowerid) {
Flowerid = flowerid;
}
public String getFlowerName() {
return FlowerName;
}
public void setFlowerName(String flowerName) {
FlowerName = flowerName;
}
public String getFlowercolor() {
return Flowercolor;
}
public void setFlowercolor(String flowercolor) {
Flowercolor = flowercolor;
}

}
Price1.java
package com.wipro;
import java.util.Scanner;
public class Price1 {
public static void main(String[] args) {
Price_num price=new Price_num();
Scanner sin=new Scanner(System.in);
System.out.println("enter the flowerid:");
int flowerid=sin.nextInt();
System.out.println("enter the flowercolour:");
String flowercolor=sin.next();
System.out.println("enter the flowername :");
String flowerName=sin.next();
Price pri=new Price();
pri.setFlowercolor(flowercolor);
pri.setFlowerid(flowerid);
pri.setFlowerName(flowerName);
price.insert(pri);
System.out.println("enter flowerid to be deleted");
int flowerid1=sin.nextInt();
price.delete(flowerid1);
}
}
Hibernate.cfg2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" -->
<!DOCTYPE hibernate-configuration SYSTEM "classpath://org/hibernate/hibernate-configuration-
3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Oracle dialect -->

ECE Page | 40
KCE Web Application Development using java ROLL NO:16L120

<property
name="hibernate.dialect">org.hibernate.dial ect.OracleDialect</pr
operty>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.connection.password">system</property>
<!-- Echo all executed SQL to stdout -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="Price1.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Price1.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.wipro.Price" table="Flower25">
<id name="Flowerid" column="Flowerid" type="int">
<generator class="assigned" >
<!-- param name="sequence">xmlseq</param -->
</generator>
</id>
<property name="FlowerName" column="florname" type="string" length="25"/>
<property name="Flowercolor" column="Florcolor" type="string" length="25"/>
</class>
</hibernate-mapping>

Output:

ECE Page | 41
KCE Web Application Development using java ROLL NO:16L120

ECE Page | 42
KCE Web Application Development using java ROLL NO:16L120

Result
Thus the Program to store the values in database is stored and executed and output is verified
successfully.

ECE Page | 43
KCE Web Application Development using java ROLL NO:16L120

Ex no: 20
Hibernate with database
Date :

Question:
Write a Hibernate Program to Delete a Flower with its Id from the table that you have created earlier. If
there is no Flower with that id exists, then an appropriate error message needs to be stored to the user.
Aim:
Write a Hibernate Program to Delete a Flower with its Id from the table that you have created earlier. If
there is no Flower with that id exists, then an appropriate error message needs to be stored to the user.
Code:
Price_num.java
package com.wipro;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
@WebServlet("/Price_num")
public class Price_num extends HttpServlet
{
SessionFactory sеssionFactory;
Session session;
Transaction tx;

public Price_num() {
sеssionFactory = new
Configuration().configure("hiber.cfg2.xml").buildSessionFactory();
}
public void insert(Price pri)
{
session = sеssionFactory.openSession();
tx = session.beginTransaction();
try
{
session.save(pri);

ECE Page | 44
KCE Web Application Development using java ROLL NO:16L120

tx.commit();
session.close();
System.out.println("value inserted");

}
catch(Exception e)
{
System.out.println("error at insert"+e);
}
}
public void delete(int flowerid)
{
session = sеssionFactory.openSession();
tx = session.beginTransaction();
try
{
Price pri= (Price)session.get(Price.class, flowerid);
session.delete(pri);
tx.commit();
session.close();
System.out.println("value deleted");
}
catch(Exception e)
{
System.out.println("error at display"+e);
}
}

Price.java
package com.wipro;
public class Price
{
private int Flowerid;
private String FlowerName;
private String Flowercolor;

public Price(int flowerid, String flowerName, String flowercolor) {


Flowerid = flowerid;
FlowerName = flowerName;
Flowercolor = flowercolor;
}
public Price() {
// TODO Auto-generated constructor stub
}
public int getFlowerid() {
return Flowerid;
}
public void setFlowerid(int flowerid) {
Flowerid = flowerid;

ECE Page | 45
KCE Web Application Development using java ROLL NO:16L120

}
public String getFlowerName() {
return FlowerName;
}
public void setFlowerName(String flowerName) {
FlowerName = flowerName;
}
public String getFlowercolor() {
return Flowercolor;
}
public void setFlowercolor(String flowercolor) {
Flowercolor = flowercolor;
}

}
Price1.java
package com.wipro;
import java.util.Scanner;
public class Price1 {
public static void main(String[] args) {
Price_num price=new Price_num();
Scanner sin=new Scanner(System.in);
System.out.println("enter the flowerid:");
int flowerid=sin.nextInt();
System.out.println("enter the flowercolour:");
String flowercolor=sin.next();
System.out.println("enter the flowername :");
String flowerName=sin.next();
Price pri=new Price();
pri.setFlowercolor(flowercolor);
pri.setFlowerid(flowerid);
pri.setFlowerName(flowerName);
price.insert(pri);
System.out.println("enter flowerid to be deleted");
int flowerid1=sin.nextInt();
price.delete(flowerid1);
}
}
Hibernate.cfg2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" -->
<!DOCTYPE hibernate-configuration SYSTEM "classpath://org/hibernate/hibernate-configuration-
3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Oracle dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.connection.password">system</property>

ECE Page | 46
KCE Web Application Development using java ROLL NO:16L120

<!-- Echo all executed SQL to stdout -->


<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="hibernate.current_session_context_class">thread</property>
<mapping resource="Price1.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Price1.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping
DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-
3.0.dtd">
<hibernate-mapping>
<class name="com.wipro.Price" table="Flower25">
<id name="Flowerid" column="Flowerid" type="int">
<generator class="assigned" >
<!-- param name="sequence">xmlseq</param -->
</generator>
</id>
<property name="FlowerName" column="florname" type="string"
length="25"/>
<property name="Flowercolor" column="Florcolor" type="string"
length="25"/>
</class>
</hibernate-mapping>

OUTPUT:

ECE Page | 47
KCE Web Application Development using java ROLL NO:16L120

RESULT:

ECE Page | 48