LAB MANUAL Advanced Java Programming Laboratory 16MCA46
1. Write a JAVA Servlet Program to implement a dynamic HTML using Servlet (user name and
Password should be accepted using HTML and displayed using a Servlet).
Project Structure
[Link]
<html>
<head><title>login</title> </head>
<body>
<form action="loginServlet" method="get">
<fieldset>
<legend>Login Form</legend>
Username:<input type="text" name="username"/><br/></br>
Password: <input type="password" name="pass"/><br/></br>
<input type="submit" value="send"/>
<input type="reset" value="clear"/>
</fieldset>
</form>
</body>
</html>
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class loginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = [Link]();
String name=[Link]("username");
String secretword=[Link]("pass");
[Link]("<html>");
[Link]("<head><title>Servlet</title></head>");
[Link]("<body>");
[Link]("<fieldset>");
[Link]("<h2>You have Entered</h2>");
[Link]("<p>Name: " + name + "</p>");
[Link]("<p> Password: " +secretword+"</p>");
[Link]("</fieldset>");
[Link]("</body>");
[Link]("</html>");
}
}
output:
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
2. Write a JAVA Servlet Program to Auto Web Page Refresh
Project Structure
[Link]
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link];
import [Link];
import [Link];
public class Refreshpage extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
[Link]("text/html;charset=UTF-8");
PrintWriter out = [Link]();
[Link]("Refresh", "3");
[Link]("<h1>Auto page Refresh using Servlet: </h1>");
[Link]("<br><h2>Today's date and time </h2>"+new Date());
}
}
output
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
3. Write a java servlet program to implement and demonstrate get() and post() methods
Project structure
[Link]
<html>
<head>
<title>TODO supply a title</title> </head>
<body>
<form action="loginServ" method="get">
<h2> Using Get method</h2>
<h3> UserName: <input type="text" name="uname"/></h3>
<h3> Password:<input type="password" name="pwd"/></h3>
<input type="submit" value="get()"/>
<input type="reset">
<hr>
</form>
<form action="loginServ" method="post">
<h2> Using Post method</h2>
<h3> UserName: <input type="text" name="uname1"/></h3>
<h3> Password:<input type="password" name="pwd1"/></h3>
<input type="submit" value="post()"/>
<input type="reset">
<hr>
</form>
</body>
</html>
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class loginServ extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String uname = [Link]("uname");
String pwd=[Link]("pwd");
PrintWriter out=[Link]();
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head><title>Servlet loginServ</title></head>");
[Link]("<body>");
[Link]("<h1>Details of Get()</h1>");
[Link]("<h1>username: " + uname+ "</h1>");
[Link]("<h1>password: " + pwd+ "</h1>");
[Link]("</body></html>");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String uname1 = [Link]("uname1");
String pwd1=[Link]("pwd1");
PrintWriter out=[Link]();
[Link]("<!DOCTYPE html>");
[Link]("<html>");
[Link]("<head>");
[Link]("<title>Servlet loginServ</title>");
[Link]("</head>");
[Link]("<body>");
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
[Link]("<h1>Details of Post()</h1>");
[Link]("<h1>username: " + uname1+ "</h1>");
[Link]("<h1>password: " + pwd1+ "</h1>");
[Link]("</body>");
[Link]("</html>");
}
Output
Get request
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
Post request
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
4. Write a JAVA Servlet Program using cookies to remember user preferences
[Link]
<html>
<head>
<title>cookie example</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<form method="post" action="Validate">
<fieldset>
<h2> Setting cookie </h2>
<h3>Name: <input type="text" name="user" /></h3>
<h3>Password:<input type="password" name="pass" /></h3>
<input type="submit" value="submit">
</fieldset>
</form>
</body>
</html>
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
public class Validate extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = [Link]("user");
String pass = [Link]("pass");
if([Link]("1234"))
{
Cookie ck = new Cookie("username",name);
[Link](ck);
[Link]("First");
}
}
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class First extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = [Link]();
//read cookie
Cookie[] cks = [Link]();
[Link]("Welcome "+cks[0].getValue());
}
}
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
Output:
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
5. Write a JAVA Servlet program to track HttpSession by accepting user name and password using
HTML and display the profile page on successful login.
[Link]
<html>
<head>
<title>setting session</title>
</head>
<body>
<form method="post" action="Validate">
<fieldset>
<h2> Setting session </h2>
<h3>Name: <input type="text" name="user" /></h3>
<h3>Password:<input type="password" name="pass" /></h3>
<input type="submit" value="submit">
</fieldset>
</form>
</body>
</html>
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
import [Link];
public class Validate extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = [Link]("user");
String pass = [Link]("pass");
if([Link]("1234"))
{
//creating a session
HttpSession session = [Link]();
[Link]("user", name);
[Link]("Welcome");
}
}
}
[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class Welcome extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = [Link]();
// getting session
HttpSession session = [Link]();
String user = (String)[Link]("user");
[Link]("Hello "+user);
}
}
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
Output
6. Write a JAVA JSP Program which uses jsp:include and jsp:forward action to display a Webpage.
Project Structure:
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
[Link]
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title></head>
<body>
<form action="[Link]" method="post">
<h2> Enter your birth date </h2>
Date: <input type="text" name="day"/>
<input type="submit" value="submit"/>
<input type="reset" value="clear"/>
</form>
</body>
</html>
[Link]
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<%@page import="[Link]"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Homepage</title>
</head>
<body>
<h1>Happy Birthday</h1>
<%
Date date = new Date();
int udate = [Link]([Link]("day"));
%>
<%if(udate==([Link]())){ %>
<jsp:include page="[Link]"/>
<%}else{ %>
<jsp:forward page="[Link]"/>
<%}%>
</body> </html>
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
[Link]
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>welcome</title> </head>
<body>
<h1>Welcome to home page</h1>
</body>
</html>
output
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
7. Write a JAVA JSP program which uses <jsp:pulgin> tag to run an applet.
Project Structure
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
[Link]
<%@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>Applet Demo</h1>
<jsp:plugin type="applet" code="[Link]" height="500" codebase="mypack" width="500">
<jsp:fallback>
unable to load
</jsp:fallback>
</jsp:plugin>
<h4><font color=red>
The above applet is loaded using the Java Plugin from a jsp page using the plugin tag
</font>
</body>
</html>
[Link]
package mypack;
import [Link];
import [Link];
import [Link];
public class MyApp extends Applet {
public void init() {
// TODO start asynchronous download of heavy resources
}
public void paint(Graphics g){
setBackground([Link]);
setForeground([Link]);
[Link]("hello from jsp", 100, 200);
}
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
Output:
8. Write a JAVA JSP Program to get student information through a HTML and create a JAVA Bean
class, populate Bean and display the same information through another JSP
[Link]
<!DOCTYPE html>
<html>
<head>
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
<title>Student Info</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<form action="[Link]" method="post">
<center><h2> Enter the student details </h2>
<p> Name: <input type="text" name="name"><br> </p>
<p>USN: <input type="text" name="usn"><br> </p>
<p>Branch: <input type="text" name="branch"><br></p>
<input type="submit" value="register"> </center>
</form>
</body>
</html>
[Link]
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<jsp:useBean id="userinfo" class="[Link]">
</jsp:useBean>
<jsp:setProperty property="*" name="userinfo" />
<strong> Entered Details are below: </strong>
<p>
<jsp:getProperty property="name" name="userinfo"/><br>
<jsp:getProperty property="usn" name="userinfo"/><br>
<jsp:getProperty property="branch" name="userinfo" /><br>
</p>
</body>
</html>
[Link]
package mypack;
import [Link];
public class Details implements Serializable{
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
private String name;
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public String getBranch() {
return branch;
}
public void setBranch(String branch) {
[Link] = branch;
}
public String getUsn() {
return usn;
}
public void setUsn(String usn) {
[Link] = usn;
}
private String branch;
private String usn;
}
Output
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
9. Write a JSP program to implement all the attributes of page directive tag.
[Link]
<html>
<head>
<title>page attribute</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<h2>Read two value to divide</h2>
<form action="[Link]">
Enter First Value:<input type="text" name="val1"/><br>
Enter Second Value:<input type="text" name="val2"/><br>
<input type="submit" value="Calculate"/>
</form>
</body>
</html>
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
[Link]
<%@page import="[Link]"
contentType="text/html"
pageEncoding="UTF-8"
session="true"
buffer="16kb"
autoFlush="true"
isThreadSafe="true"
isELIgnored="false"
extends="[Link]"
info="Lab 10: demo of all page directive" language="java"
errorPage="[Link]"
%>
<!DOCTYPE html>
<html>
<head>
<title>JSP Page</title> </head>
<%! int a, b;
Date d=new Date();%>
<body>
<h2>Welcome! Today is <%= [Link]()%></h2>
<%
String str1=[Link]("val1");
String str2=[Link]("val2");
a=[Link](str1);
b=[Link](str2);%>
<h2>Using Expression Language</h2>
A= ${param.val1}<br>
B= ${param.val2}<br>
<h3>Result: <%= a / b %></h3>
</body>
</html>
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
[Link]
<%@page isErrorPage="true"%>
<!DOCTYPE html> <html>
<head>
<title>JSP Page</title> </head>
<body>
<h3>Sorry an exception occured!</h3>
<h2>The exception is: <%= exception %> </h2> </body>
</html>
Output:
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
10. Write a JAVA Program to insert data into Student DATA BASE and retrieve info based on
particular queries(For example update, delete, search etc…)
import [Link].*;
import [Link];
public class Student
{
Connection con;
public void establishConnection()throws ClassNotFoundException,SQLException
{
[Link]("[Link]");
con=[Link]("jdbc:mysql://localhost:3306/studentlogin","root","rnsit");
}
public void sInsert(String usn, String name, String dept)throws ClassNotFoundException, SQLException
{
PreparedStatement pst=null;
establishConnection();
try
{
if(con!=null)
{
pst=[Link]("insert into student values(?,?,?)");
[Link](1, usn);
[Link](2,name);
[Link](3,dept);
int i=[Link]();
if(i==1)
{
[Link]("Record inserted successfully");
}}}
catch(SQLException e)
{
[Link]([Link]());
}
finally
{
[Link]();
[Link]();
}}
public void sSelect(String usn)throws ClassNotFoundException,SQLException
{
PreparedStatement pst=null;
ResultSet res;
establishConnection();
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
try
{
if(con!=null)
{
pst=[Link]("select * from student where usn=?");
[Link](1, usn);
res=[Link]();
if([Link]())
{
[Link]("USN= "+[Link](1)+"\tName="+ [Link](2)+ "\tDepartment=
"+[Link](3));
} }}
finally
{
[Link]();
[Link]();
} }
public void sUpdate(String usn, String name, String dept)throws ClassNotFoundException, SQLException
{
PreparedStatement pst=null;
establishConnection();
try
{
if(con!=null)
{
pst=[Link]("update student set name=?,dept=? where usn=?");
[Link](1, name);
[Link](2,dept);
[Link](3,usn);
int i=[Link]();
[Link](i);
if(i==1)
{
[Link]("Record updated successfully");
} } }
finally
{
[Link]();
[Link]();
}}
public void sDelete(String usn)throws ClassNotFoundException, SQLException
{
PreparedStatement pst=null;
establishConnection();
try
{
if(con!=null)
{
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
pst=[Link]("delete from student where usn=?");
[Link](1, usn);
int i=[Link]();
if(i==1)
{
[Link]("Record deleted successfully");
}}}
catch(SQLException e)
{
[Link]([Link]());
}
finally
{
[Link]();
[Link]();
}}
public void viewAll( )throws ClassNotFoundException, SQLException
{
PreparedStatement pst=null;
ResultSet res;
establishConnection();
try
{
if(con!=null)
{
pst=[Link]("select * from student");
res=[Link]();
while([Link]())
{
[Link]("USN= "+[Link](1)+"\tName="+[Link](2)+"\tDepartment=
"+[Link](3));
} } }
catch(SQLException e)
{
[Link]([Link]());
}
finally
{
[Link]();
[Link]();
} }
public static void main(String[] a) throws ClassNotFoundException, SQLException
{
Student std=new Student();
String usn,name,dept;
Scanner sc=new Scanner([Link]);
while(true)
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
{
[Link]("Operations on Student table");
[Link]("[Link]\[Link]\[Link]\[Link]\[Link] All\[Link]");
[Link]("select the operation");
switch([Link]())
{
case 1: [Link]("Enter USN to insert");
usn=[Link]();
[Link]("Enter Name to insert");
name=[Link]();
[Link]("Enter Deaprtment to insert");
dept=[Link]();
[Link](usn, name, dept);
break;
case 2: [Link]("Enter USN to select");
usn=[Link]();
[Link](usn);
break;
case 3: [Link]("Enter USN to update");
usn=[Link]();
[Link]("Enter Name to update");
name=[Link]();
[Link]("Enter department to update");
dept=[Link]();
[Link](usn, name, dept);
break;
case 4: [Link]("Enter USN to delete");
usn=[Link]();
[Link](usn);
break;
case 5: [Link]();
break;
case 6: [Link](0);
default: [Link]("Invalid operation");
break;
}
}
}
}
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
11. An EJB application that demonstrates Session bean
[Link]
package com;
import [Link];
@Stateless
public class SessionB implements SessionBLocal {
@Override
public int Square(int side) {
return side*side;
}
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
SessionBLocal
package com;
import [Link];
@Local
public interface SessionBLocal {
int Square(int side);
}
[Link]
<!DOCTYPE html>
<html>
<head>
<title>lab11</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<form action="SessionServlet">
Enter the number:
<input type="text" name="num"/><br>
<input type="submit"/>
</form>
</body>
</html>
[Link]
package com;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class SessionServlet extends HttpServlet {
@EJB
private SessionBLocal sessionB;
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=[Link]();
int i, result;
i=[Link]([Link]("num"));
result=[Link](i);
[Link]("the result "+result);
}
}
Output:
12. An EJB application that demonstrates MDB (with appropriate business
logic).
Project Structure
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
[Link]
package com;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@MessageDriven(mappedName = "jms/destlab12", activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue =
"[Link]")
})
public class MessageB implements MessageListener {
public MessageB() {
}
@Override
public void onMessage(Message message) {
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
TextMessage t=null;
t=(TextMessage) message;
try {
[Link]([Link]());
} catch (JMSException ex) {
[Link]([Link]()).log([Link], null, ex);
}
}
[Link]
<%@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>
<form action="MDB">
Type your message :<input type="text" name="msg"/><br>
<input type="submit"/>
</form>
</body>
</html>
[Link]
package com;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class MDB extends HttpServlet {
@Resource(mappedName = "jms/destlab12")
private Queue destlab12;
@Resource(mappedName = "jms/lab12")
private ConnectionFactory lab12;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=[Link]();
String m=[Link]("msg");
try {
sendJMSMessageToDestlab12(m);
} catch (JMSException ex) {
[Link]([Link]()).log([Link], null, ex);
}
[Link]("check output in server log");
private Message createJMSMessageForjmsDestlab12(Session session, Object messageData)
throws JMSException {
// TODO create and populate message to send
TextMessage tm = [Link]();
[Link]([Link]());
return tm;
}
private void sendJMSMessageToDestlab12(Object messageData) throws JMSException {
Connection connection = null;
Session session = null;
try {
connection = [Link]();
session = [Link](false, Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = [Link](destlab12);
[Link](createJMSMessageForjmsDestlab12(session, messageData));
} finally {
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
if (session != null) {
try {
[Link]();
} catch (JMSException e) {
[Link]([Link]().getName()).log([Link], "Cannot
close session", e);
}
}
if (connection != null) {
[Link]();
}
}
}
Output
1. Right Click on JMS
Select clean and build
2. Right Click on JMS
Deploy
3. Right Click on JMS
Run
Go back to Netbeans->GlassFish Server 4.0
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
13. An EJB application that demonstrates persistence (with appropriate business logic).
Project Structure
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
[Link]
package com;
import [Link];
import [Link];
import [Link];
@Stateless
public class Emp1Facade extends AbstractFacade<Emp1> implements Emp1FacadeLocal {
@PersistenceContext(unitName = "lab13-ejbPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public Emp1Facade() {
super([Link]);
}
public void addEmp(String eid, String ename)
{
Emp1 e=new Emp1();
[Link](eid);
[Link](ename);
create(e);
}
[Link]
package com;
import [Link];
import [Link];
@Local
public interface Emp1FacadeLocal {
void create(Emp1 emp1);
void edit(Emp1 emp1);
void remove(Emp1 emp1);
Emp1 find(Object id);
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
List<Emp1> findAll();
List<Emp1> findRange(int[] range);
int count();
void addEmp(String eid, String ename);
[Link]
<%@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>
<form action="EmpServlet" method="GET">
<b>Employee Form</b><br/><br/>
Emp Id: <input type="text" name="id"/><br/>
Name: <input type="text" name="name"/><br/>
<input type="submit" value="Add"/>
</form>
</body>
</html>
[Link]
package com;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class EmpServlet extends HttpServlet {
@EJB
private Emp1FacadeLocal emp1Facade;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT
LAB MANUAL Advanced Java Programming Laboratory 16MCA46
throws ServletException, IOException {
PrintWriter out=[Link]();
String eid=[Link]("id");
String ename=[Link]("name");
[Link](eid, ename);
[Link]("<b> Employee Id: " + eid + "</b><br/>");
[Link]("<b> Employee Ename: " + ename + "</b><br/><br/>
<b>Added to database</b>");
}
}
Output:
[Link] and Build
2. Deploy
3. Run
Prepared By: Rajatha S, Assistant Professor, Dept. of MCA, RNSIT