Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views

Advance Java

Uploaded by

samrity.5476
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Advance Java

Uploaded by

samrity.5476
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 53

Masters of Computer Application

Lab Manual

Subject: Advance Java Laboratory


Subject Code:PGCA1922

MCA 1st Year – 2nd Semester

CHANDIGARH ENGINEERING
COLLEGE
CGC-Landran, Mohali -140307
Advanced Java Laboratory (PGCA1922):C114
COURSE OUTCOMES

CO1 Outline the advanced features of Java and write the programs.
CO2 Make Use of API and implement Serialization concept of Java.
CO3 Determine Java Generics and develop Projects.
CO4 Make use of digital marketing for developing effective digital and social media
strategies

Advance Java Laboratory (PGCA1922) Year 2022-23

COURSE
OUTCOME PROGRAMME OUTCOMES (2022-23)
S
CO P P P P P P PO P P PO1 PO1 PO PS PSO PS
O O O O O O 7 O8 O9 0 1 12 O1 2 03
1 2 3 4 5 6

C114.1 3 2 2 2 1 2 2 1 3

C114.2 2 2 2 2 1 3 3 1 1

C114.3 3 2 3 2 1 1 1

C114.4 2 1 2 2 1 2 3 1 2

PGCA1922 3 2 2 2 1 2 2 1 1 2
Advance Java Laboratory (PGCA1922)

Internal Max. Marks:70

External Max. Marks:30 L TP

Total Marks: 100 004

Prac.No. Name of Practical Page No Relevanceto Cos

1. Create a Servlet to handle HTTP Requests and Responses. 1-3 CO1


2. Implementation of the concept of Cookies and Session Tracking. 4-6 CO1
3. Illustrate the concept of JavaServer Pages (JSP). 7-8 CO1
4. Create a JavaBean by using Bean Developer Kit (BDK). 9-10 C02
5. Implementation of various types of beans like Session Bean and Entity 11-15 CO2
Bean.
6. Introduction to Struts platform with basic connectivity. 16 CO3
7. Deploying first sample program using MVC architecture in struts. 17 CO3
8. Implementing database connectivity in struts. 18-19 CO3
9. Creating one sample application in struts. 20-21 CO2
10. Introduction to Hibernate framework. 22-24 CO4
11. Creating simple Hibernate application. 25-29 CO4

Content Beyond Syllabus


12. Configure Hibernate and strut on Eclipse 30-33 CO4
13. Write a program to implement the concept of RMI. 34-35 CO2

PREPARED BY: APPROVED BY:

LAB INCHARGE HOD(MCA)


IKGPTU Syllabus
Course Code: PGCA1922
Course Name: Advanced Java Laboratory

Program: MCA L: 0 T: 0 P: 4
Branch: Computer Applications Credits:2
Semester: 2nd Contact hours: 4 hours per week
Theory/Practical: Practical Percentage of numerical/design problems: -
-
Internal max. marks: 70 Duration of end semester exam (ESE): -
External max. marks: 30 Elective status: Core
Total marks: 100

Prerequisite: -NA-
Co requisite: -NA-
Additional material required in ESE: -NA-
Course Outcomes: Students will be able to:
CO# Course outcomes
CO1 Learn the advanced features of Java and write the programs.
CO2 Work with API and implement Serialization concept of Java.
CO3 Learn Java Generics and develop Projects.
CO4 Understand to use digital marketing for developing effective digital and
social media
strategies
S.N Practical Assignments (Java)
o.
1. Create a Servlet to handle HTTP Requests and Responses.
2. Implementation of the concept of Cookies and Session Tracking.
3. Illustrate the concept of JavaServer Pages (JSP).
4. Create a JavaBean by using Bean Developer Kit (BDK).
5. Implementation of various types of beans like Session Bean and Entity Bean.
6. Introduction to Struts platform with basic connectivity.
7. Deploying first sample program using MVC architecture in struts.
8. Implementing database connectivity in struts.
9. Creating one sample application in struts.
10. Introduction to Hibernate framework.
11. Creating simple Hibernate application.
Text Books:
1. Herbert Schildt , “The Complete Reference Java 2” , Tata McGraw -Hill.
2. H.M. Deital, P.J. Dietal and S.E. Santry, “Advanced Java 2 Platform How To
Program”,Prentice Hall.
3. Laudon and Traver, “E-Commerce: Business, Technology & Society”, PearsonEducation
1. Write a program to create Servlet to display Username & password using
doGet() method..
index.html
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="get" action="Servlet1">
<input type="text" placeholder="enter your name" name="name"/>
<input type="password" placeholder="enter your password" name="pass"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>

Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>pack.servlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<session-config>
<session-
timeout>
30
</session-timeout>
</session-config>
</web-app>
Servlet1
1
package pack;

2
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
public class servlet1 extends HttpServlet {
@Override
public void init() throws ServletException {
super.init(); //To change body of generated methods, choose Tools | Templates.
}
@Override
protected void doGet(HttpServletRequestreq, HttpServletResponseresp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out= resp.getWriter();
out.println("<h1>Hello world</h1>");
out.println("<h1>This is my doGet() method </h1>");
}@Override
protected void doPost(HttpServletRequestreq, HttpServletResponseresp)
throws ServletException, IOException {
PrintWriter out= resp.getWriter();
out.println("<h1>Hello world</h1>");
out.println("<h1>This is my doPost() method </h1>");
}
@Override
public void destroy() {
super.destroy(); //To change body of generated methods, choose Tools | Templates.
}}
OUTPUT:

3
4
2. Write a program to create Servlet to display Username & password
using doPost() method.
Index.html
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" action="Servlet1">
<input type="text" placeholder="enter your name" name="name"/>
<input type="password" placeholder="enter your password" name="pass"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>

Servlet1.java
package pack;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletExceptio
n;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletRespons
e;
5
public class Servlet1 extends HttpServlet {

@Override
public void init() throws ServletException {
super.init(); //To change body of generated methods, choose Tools | Templates.
}

@Override
protected void service(HttpServletRequestreq, HttpServletResponseresp) throws
ServletException, IOException {

resp.setContentType("Text/
html"); PrintWriter out=
resp.getWriter();

String name= req.getParameter("name");


String password = req.getParameter("pass");
out.println("user name is"+name);
out.println("user password is"+password);

out.println("hello
world");
out.println("<html>");
out.println("<head>");
out.println("<title>welcome to
servlet</title>"); out.println("<body>");
out.println("<h1>welcome to servlet1</h1>");
out.println("<h1>get content type "
+req.getContextPath()+"</h1>"); out.println("<h1>get content type
" +req.getContentType()+"</h1>");

out.println("<h1>get local address" +req.getLocalAddr()+"</h1>");


out.println("<h1>get path info " +req.getPathInfo()+"</h1>");
out.println("<h1>get protocol " +req.getProtocol()+"</h1>");
out.println("<h1>get server name "
+req.getServerName()+"</h1>");

out.println("</
body>");
out.println("</head>")
;
out.println("</html>")
;
}}
OUTPUT
:

6
7
3. Demonstrate the use of SEND REDIRECT
METHOD WEB.XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web- app_3_1.xsd">
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-class>pack.Servlet1</servlet-class>
</servlet>
<servlet>
<servlet-name>Servlet2</servlet-name>
<servlet-class>pack.Servlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/Servlet1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Servlet2</servlet-name>
<url-pattern>/Servlet2</url-pattern>
</servlet-mapping>
<session-config>
<session-
timeout>
30
</session-timeout>
</session-config>
</web-app>
INDEX.HTML
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="Servlet1" method="get">
<input type="text" placeholder="enter your name" name="name"/>
<input type="text" placeholder="enter your password" name="pass"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>

8
9
4. Illustrate the function of cookies in servlet

index.html
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="get" action="Servlet1">
<input type="text" placeholder="enter your name" name="name"/>
<input type="password" placeholder="enter your password" name="pass"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>

SERVLET1
package pack;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletExceptio
n;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletRespons
e; public class Servlet1 extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throwsServletException, IOException
{
response.setContentType("text/html;charset=UTF-
8"); try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code.
*/ out.println("<THIS IS SERVLET1>");

String name= request.getParameter("name");


String password =
request.getParameter("pass");

if(name.equals("admin") &&password.equals("admin"))
{
response.sendRedirect("Servlet2?nm="+name+"&psw="+password+"");
}
else
{
10
response.sendRedirect("index.html");
}

11
}
}
}

SERVLET2
package pack;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletExceptio
n;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletRespons
e; public class Servlet2 extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throwsServletException, IOException
{
response.setContentType("text/html;charset=UTF-
8"); PrintWriter out = response.getWriter();
String name= request.getParameter("nm");
String password=
request.getParameter("psw");
out.println("name is "+name);
out.println("password is
"+password); out.println("this is
servlet2");
}
}
Output:

12
5. Demonstrate the use of REQUEST DISPATCHER
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
eb-app version="3.1"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-class>pack.Servlet1</servlet-class>
</servlet>
<servlet>
<servlet-name>Servlet2</servlet-name>
<servlet-class>pack.Servlet2</servlet-class>
</servlet>
<servlet>
<servlet-name>Servlet3</servlet-name>
<servlet-class>pack.Servlet3</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/Servlet1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Servlet2</servlet-name>
<url-pattern>/Servlet2</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Servlet3</servlet-name>
<url-pattern>/Servlet3</url-pattern>
</servlet-mapping>
<session-config>
<session-
timeout>
30
</session-timeout>
</session-config>
</web-app>
Index.html
<!DOCTYPE html>
13
<!--
To change this license header, choose License Headers in Project Properties.

14
To change this template file, choose Tools |
Templates and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="get" action="Servlet1">
<input type="text" placeholder="enter your name" name="name"/>
<input type="password" placeholder="enter your password" name="pass"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>

Servlet1
package pack;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.RequestDispatcher
;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
public class Servlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequestreq, HttpServletResponseresp)
throws ServletException, IOException{
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
String name= req.getParameter("name");
String password= req.getParameter("pass"); req.setAttribute("database","MySql");
if(name.equals("admin")&&password.equals("admin"))
{
RequestDispatcherrd= req.getRequestDispatcher("Servlet2");
rd.include(req,resp) }
else
15
{

16
resp.sendRedirect("index.html");
}
Servlet2
package pack;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.RequestDispatcher
;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
public class Servlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequestreq, HttpServletResponseresp)
throws ServletException, IOException {
resp.setContentType("text/
html"); PrintWriter out =
resp.getWriter(); out.println("this
is servlet2");
String name= req.getParameter("name");
String password
=req.getParameter("pass");
String database = (String)req.getAttribute("database");
out.println("Name is "+name);
out.println("Password is
"+password); out.println("Database
is "+database);
out.println("<a href='Servlet3'>click here to move Servlet3</a>");
}

}
Servlet3
package pack;
importjava.io.IOException;
importjava.io.PrintWriter;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
@author admin
17
public class Servlet3 extends HttpServlet
{ @Override
protected void doGet(HttpServletRequestreq, HttpServletResponseresp)
throws ServletException, IOException

18
resp.setContentType("text/html");

PrintWriter out = resp.getWriter();

String name = req.getParameter("name");


String password = req.getParameter("pass");
//String database=
(String)req.getAttribute("database"); out.println("username
is"+name+"<br>"); out.println("password
is"+password+"<br>");
// out.println("database is "+database+"<br>");
out.println("<a href='index.html'>click here to move on Home Page</a>");
}}}
Output:

19
20
6. Introduction of JSP.
index.html
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>TODO write content</div>
</body>
</html>
welcome.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%! int x=10;%>
<%= x%>
<%
out.println("This is scriplet
tag"); out.print("value of x is="
+x);
%>
</body>
</html>
Output:

21
7. Demonstrate the use of scriplet tag in
JSP index.html
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>TODO write content</div>
</body>
</html>
welcome.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page language="java" import="java.util.Date" autoFlush="true" isThreadSafe="true" buffer="16kb"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%! int x=10;%>
<%= x%>
<%
out.println("This is scriplet tag
<br>"); out.print("value of x is="
+x);
%>
<%= new Date() %>
</body>
</html>

Output:

22
8. Demonstrate the use of iserror and error tag in
JSP index.html
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>TODO write content</div>
</body>
</
html>
newpag
e
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page language="java" import="java.util.Date" autoFlush="true" isThreadSafe="true" buffer="16kb"%>
<%@page errorPage="Errorjsp.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%! int x=10; int y;%>
<%= x%>
<%
out.println("This is scriplet tag
<br>"); out.print("value of x is="
+x);
y = x/0;
%>
<%= new Date() %>
</body>
</html>
ERROR PAGE
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page isErrorPage="true" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Sorry! something went wrong</h1>
</body>
</html>
Output:
23
24
9. Demonstatethe use of HTML tag in
JSP index.html
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>TODO write content</div>
</body>
</html>
newpage
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page language="java" import="java.util.Date" autoFlush="true" isThreadSafe="true" buffer="16kb"%>
<%@page errorPage="Errorjsp.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<%! int x=10; int y;%>
<%= x%>
<%
out.println("This is scriplet tag
<br>"); out.print("value of x is="
+x);
y = x/0;
%>
<%= new Date() %>
</body>
</html>
ERROR PAGE
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page isErrorPage="true" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Sorry! something went wrong</h1>
</body>
</html>
header
<%@page contentType="text/html" pageEncoding="UTF-8"%>
25
<!DOCTYPE html>

26
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1 style="background: pink"> this is a header</h1>
</body>
</html>

Output:

27
10. Demonstrate the sum of two number using
JSP index
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="welcome.jsp" method="post">
<input type="text" placeholder="enter name" name="name"/>
<input type="text" placeholder="enter name" name="name"/>
<input type="text" value="submit"/>
</form>
</body>
</html>
welcome
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@tagliburi="/WEB-INF/tlds/math.tld " prefix="math" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String
n1=request.getParameter("num1");
String
n2=request.getParameter("num2");
%>
<math:sumnum="<%=n1 %>" num2="<%=n2 %>"
</body>
</html>
math
<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-
jsptaglibrary_2_1.xsd">
<tlib-version>1.0</tlib-version>
<short-name>math</short-name>
<uri>/WEB-INF/tlds/math</uri>
<tag>
<name>num</name>
<tag-class>pack.taghandler</tag-class>
<attribute>
<name>num1</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
28
</attribute>
<attribute>
<name>num2</name>

29
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
taghandler
package pack;
importjavax.servlet.jsp.JspException;
importjavax.servlet.jsp.JspWriter;
importjavax.servlet.jsp.tagext.TagSupport;
public class taghandler extends
TagSupport{
private String num1,num2;
public void setNum1(String num1)
{ this.num1 = num1;
}
public void setNum2(String num2)
{ this.num2 = num2;
}
@Override
publicintdoStartTag() throws JspException
{ try{
int
n1=Integer.parseInt(num1);
int
n2=Integer.parseInt(num2);
int sum =n1+n2;
JspWriter
out=pageContext.getOut();
out.println("sum is"+sum);
}
catch(Exception e){
System.out.println("exception"+e
);
}
return SKIP_BODY;
}}
Output:

30
31
32
11. Illustrate the use of Hibernate
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web- app_3_1.xsd">
<servlet>
<servlet-name>CheckServlet</servlet-name>
<servlet-class>pack.CheckServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CheckServlet</servlet-name>
<url-pattern>/CheckServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-
timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
index.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools |
Templates and open the template in the editor.
-->
<html>
<head>
<title> TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="CheckServlet" method="post">
<input type ="text" placeholder="Enter roll number" name="roll_number"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>

33
hibernate.cfg.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">
<hibernate-configuration>
<session-factory>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/demo8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">12345</property>
<mapping resource="hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>
hibernate.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 table="student" name="pojo.Userpojo">
<id name="roll">
<generator class="assigned"></generator>
</id>
<property name="name" type="string"></property>
<property name="email" type="string"></property>
<property name="contact" type="string"></property>

34
</class>
</hibernate-mapping>

checkservlet.java
package pack;
importjava.io.IOException;
importjava.io.PrintWriter;
importjava.util.Iterator;
importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.hibernate.Query;
importorg.hibernate.Session;
importorg.hibernate.SessionFactory;
importorg.hibernate.Transaction;
importorg.hibernate.cfg.Configuration;
importpojo.Userpojo;
public class CheckServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Configuration cfg= new Configuration();
cfg.configure(); // read both cogiguration file and mapping file

SessionFactory factory = cfg.buildSessionFactory();

Session session = factory.openSession();

Transaction tx= session.beginTransaction();


// HQL
String roll0=request.getParameter("roll_number");

String hql = "from Userpojo U where U.roll=:R";//only for 1 record use SELECT Userpojo for all record

Query query = session.createQuery(hql);


query.setParameter("R",Integer.parseInt(roll0));
java.util.List list = query.list();
Iterator iterator = list.iterator();
while(iterator.hasNext(){
Userpojoobj = (Userpojo)iterator.next();//for all record
out.println("Roll Number "+obj);
out.println("Name is "+obj.getName());
out.println("Email is "+obj.getEmail());
out.println(" Contact is "+obj.getContact());

} tx.commit();
}}
controller.java
35
package pack;
importantlr.collections.List;
importjava.util.Iterator;
importorg.hibernate.Query;
importorg.hibernate.Session;
importorg.hibernate.SessionFactory;
importorg.hibernate.Transaction;
importorg.hibernate.cfg.Configuratio
n; importpojo.Userpojo;

public class Controller


{
public static void main(String[] args)
{
Configuration cfg= new Configuration();
cfg.configure(); // read both cogiguration file and mapping
file SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();
Transaction tx= session.beginTransaction();
String hql = "SELECT E1.roll from Userpojo E1";//only for 1 record use SELECT Userpojo for all
record Query query = session.createQuery(hql);
java.util.List list = query.list();
Iterator iterator =
list.iterator();
while(iterator.hasNext())
{ System.out.println("Roll Number "+iterator.next());
tx.commit();
}}

Userpojo.java
public class Userpojo
{
privateint roll;
private String
name,contact,email;
publicintgetRoll() {
return roll;
}public void setRoll(int roll)
{ this.roll = roll;
} public String getName()
36
{ return name;
}

37
public void setName(String name)
{ this.name = name;
}

public String getContact()


{ return contact;
}

public void setContact(String contact)


{ this.contact = contact;
}

public String getEmail()


{ return email;
}

public void setEmail(String email)


{ this.email = email;
}

38
39
12. Demonstrate the use of HQL in hibernate TO Eclipse
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web- app_3_1.xsd">
<session-config>
<session-
timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
index.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools |
Templates and open the template in the editor.
-->
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>TODO write content</div>
</body>
</html>
Hibernate.cfg.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">
<hibernate-configuration>
<session-factory>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/demo7</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">12345</property>
<mapping resource="hibernate.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Hibernate.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
40
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD
3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class dynamic-insert="false" table="student" dynamic-update="false" mutable="true"
name="pojo.Userpojo" optimistic-lock="version" polymorphism="implicit" select-before-update="false">
<id name="roll">
<generator class="assigned"></generator>
</id>
<property name="name" type="string"></property>
<property name="email" type="string"></property>
<property name="contact" type="string"></property>
</class>
</hibernate-mapping>
Controller.java
package pack;
importorg.hibernate.Session;
importorg.hibernate.SessionFactory;
importorg.hibernate.Transaction;
importorg.hibernate.cfg.Configuratio
n; importpojo.Userpojo;
public class Controller {
public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure();//read both configuration gile and mapping
file SessionFactory Factory =cfg.buildSessionFactory();
Session session= Factory.openSession();
Transaction tx =session.beginTransaction();
Userpojoobj = new Userpojo();
obj.setRoll(2);
obj.setName("anchal");
obj.setEmail("anch.gmail.com"
); obj.setContact("97467838");
session.save(obj);
tx.commit();
}}
Userpojo.java
packagepojo;
public class Userpojo
{ privateint roll;
private String name,contact,email;
publicintgetRoll() { return
roll;
}
public void setRoll(int roll)
{ this.roll = roll;
}
public String getName()
{ return name;
}
public void setName(String name)
{ this.name = name;
}
41
public String getContact() {

42
return contact;
}
public void setContact(String contact)
{ this.contact = contact;
} public String getEmail()
{ return email;
}

public void setEmail(String email)


{ this.email = email;}}
Output

43
13. Write a program to implement the concept of RMI.

packagermi;
importjava.rmi.Remote;
importjava.rmi.RemoteException
;
public interface Adder extends Remote
{
publicint add(int a, int b) throws RemoteException;

packagermi;
importjava.rmi.NotBoundException;
importjava.rmi.RemoteException;
importjava.rmi.registry.LocateRegistry
; importjava.rmi.registry.Registry;
importjava.util.Scanner;
public class Client
{
public static void main(String args[]) throws RemoteException, NotBoundException
{
Client c = new
Client();
c.connectRemote();
}
private void connectRemote() throws RemoteException, NotBoundException
{
try
{ Scanner sc = new Scanner(System.in);
Registry reg =
LocateRegistry.getRegistry("localhost",1099); Adder ad =
(Adder)reg.lookup("hello");

System.out.println("Enter any two number ");


int a = sc.nextInt();
int b = sc.nextInt();
System.out.println("Addition is "+ad.add(a, b));
}
catch(RemoteException e)
{
System.out.println("Exception is ctrated in Client "+e);
}

44
}

45
packagermi;
importjava.rmi.RemoteException;
importjava.rmi.registry.LocateRegistry;
importjava.rmi.registry.Registry;
importjava.rmi.server.UnicastRemoteObject
;
public class Server extends UnicastRemoteObject implements Adder
{
public Server() throws RemoteException
{
}
@Override
publicint add(int a, int b) throws RemoteException
{
int c=
a+b;
return c;
}

public static void main(String[] args) throws RemoteException


{
try
{
Registry reg = LocateRegistry.createRegistry(1099);
reg.rebind("hello",new Server());
System.out.println("Server is Ready ");

}
catch (RemoteException e)
{
System.out.println("Exception "+e);
}

}
}

Output:-

Enter the first integer


number 33
Enter the second integer
46
number 55
Addition is 88

47

You might also like