0% found this document useful (0 votes)
109 views

Practical File Advance Java

Uploaded by

Raghav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
109 views

Practical File Advance Java

Uploaded by

Raghav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 57

Chandigarh Group of Colleges, Landran

(Department of Computer Applications)


COURSE: MCA

SUBJECT:Advanced Java Laboratory


SUBJECT CODE: PGCA-1922

Submitted To: Submitted By:


NAME:
NAME: ROLL NO. :
DEPARTMENT :
DESIGNATION: AP SESSION:

DEPARTMENT:MCA

CGC, Landran, Mohali (Punjab)

(Pin- 140307)Phone: 0172- 3984200, 3984206


Advanced Java Laboratory-PGCA1922

INDEX
S.no Content Page no Signature
1. Write a program to create Servlet to display
3-6
Username & password using doGet() method.
2. Write a program to create Servlet to display
6-10
Username & password using doPost() method.
3. Demonstrate the use of SendRedirect() method 10-16
4. illustrate the function of cookies in servlet 16-20
5. Demonstrate the use of REQUEST DISPATCHER 20-22
6. Introduction of JSP. 22-24
7. Demonstrate the use of script let tag in JSP 24-26
8. Demonstrate the use of isError and error tag in
26-28
JSP
9. Demonstrate the use of HTML tag in JSP 29-33
10. Demonstrate the sum of two number using JSP 3-36
11. illustrate the use of Hibernate 36-42
12. Demonstrate the use of HQL 42-49
13. Write a program to implement the concept of
49-52
RMI.
Advanced Java Laboratory-PGCA1922

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">
Advanced Java Laboratory-PGCA1922

<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
package pack;
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
Advanced Java Laboratory-PGCA1922

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:
Advanced Java Laboratory-PGCA1922
Advanced Java Laboratory-PGCA1922

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>
Advanced Java Laboratory-PGCA1922

Servlet1.java

package pack;

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
Advanced Java Laboratory-PGCA1922

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>");


Advanced Java Laboratory-PGCA1922

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:
Advanced Java Laboratory-PGCA1922

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>
Advanced Java Laboratory-PGCA1922

</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>
Advanced Java Laboratory-PGCA1922
Advanced Java Laboratory-PGCA1922

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.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;
Advanced Java Laboratory-PGCA1922

importjavax.servlet.http.HttpServletResponse;

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

response.sendRedirect("index.html");

}
Advanced Java Laboratory-PGCA1922

SERVLET2

package pack;

importjava.io.IOException;

importjava.io.PrintWriter;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

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:
Advanced Java Laboratory-PGCA1922
Advanced Java Laboratory-PGCA1922

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>
Advanced Java Laboratory-PGCA1922

<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>
<!--
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">
Advanced Java Laboratory-PGCA1922

</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");
Advanced Java Laboratory-PGCA1922

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
{
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 {
Advanced Java Laboratory-PGCA1922

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
public class Servlet3 extends HttpServlet {
@Override
Advanced Java Laboratory-PGCA1922

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");
//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:
Advanced Java Laboratory-PGCA1922
Advanced Java Laboratory-PGCA1922

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");


Advanced Java Laboratory-PGCA1922

out.print("value of x is=" +x);

%>

</body>

</html>

Output:
Advanced Java Laboratory-PGCA1922

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>");


Advanced Java Laboratory-PGCA1922

out.print("value of x is=" +x);

%>

<%= new Date() %>

</body>

</html>

Output:
Advanced Java Laboratory-PGCA1922

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>

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%>
Advanced Java Laboratory-PGCA1922

<%

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:
Advanced Java Laboratory-PGCA1922
Advanced Java Laboratory-PGCA1922

9.Demonstate the 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%>
Advanced Java Laboratory-PGCA1922

<%

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"%>

<!DOCTYPE html>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">


Advanced Java Laboratory-PGCA1922

<title>JSP Page</title>

</head>

<body>

<h1 style="background: pink"> this is a header</h1>

</body>

</html>

Output:
Advanced Java Laboratory-PGCA1922

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>
Advanced Java Laboratory-PGCA1922

<%

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>

</attribute>

<attribute>

<name>num2</name>

<required>true</required>

<rtexprvalue>true</rtexprvalue>

</attribute>
Advanced Java Laboratory-PGCA1922

</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);

}
Advanced Java Laboratory-PGCA1922

return SKIP_BODY;

}}

Output:
Advanced Java Laboratory-PGCA1922

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.


Advanced Java Laboratory-PGCA1922

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>
Advanced Java Laboratory-PGCA1922

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>
Advanced Java Laboratory-PGCA1922

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>

</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;
Advanced Java Laboratory-PGCA1922

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(){
Advanced Java Laboratory-PGCA1922

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
package pack;

importantlr.collections.List;

importjava.util.Iterator;

importorg.hibernate.Query;

importorg.hibernate.Session;

importorg.hibernate.SessionFactory;

importorg.hibernate.Transaction;

importorg.hibernate.cfg.Configuration;

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();


Advanced Java Laboratory-PGCA1922

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() {


Advanced Java Laboratory-PGCA1922

return name;

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;

}
Advanced Java Laboratory-PGCA1922
Advanced Java Laboratory-PGCA1922

12. Demonstrate the use of HQL


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>


Advanced Java Laboratory-PGCA1922

<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"?>

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


"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
Advanced Java Laboratory-PGCA1922

<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.Configuration;

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();
Advanced Java Laboratory-PGCA1922

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;

public String getContact() {


Advanced Java Laboratory-PGCA1922

return contact;

public void setContact(String contact) {

this.contact = contact;

} public String getEmail() {

return email;

public void setEmail(String email) {

this.email = email;}}

Output
Advanced Java Laboratory-PGCA1922
Advanced Java Laboratory-PGCA1922

Q13. 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();
Advanced Java Laboratory-PGCA1922

}
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);
}

}
Advanced Java Laboratory-PGCA1922

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;
}
Advanced Java Laboratory-PGCA1922

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 number
55
Addition is 88

You might also like