EX - NO: 10 Date: An Ejb Application That Demonstrates Session Bean - Stateless Bean Aim
EX - NO: 10 Date: An Ejb Application That Demonstrates Session Bean - Stateless Bean Aim
NO: 10
DATE:
AIM:
To develop an EJB application that demonstrates Stateless Session Bean.
PROCEDURE:
1. Start Netbeans IDE, Go to file menu and select new project, the new project dialog box
opens. From the Categories select the option JavaEE and from projects select Enterprise
Application and click Next, type the name as “Myfirst”, select location click next and
finish.
2. Right click the source packages in “Myfirst-ejb” select new and select “session bean” and
give a name for it as “calcbean” and package name as “SessionBean” select session as
“stateless” and select checkbox “local” and click Finish.
3. Inside the code of the class rightclick and select insertcode and select Add Business
Method. A dialog box opens give the name of the method as “addition” and for return type
click browse and select integer (java.lang) and click ok. Click the add button and enter the
parameter name as “a” and type as int and click add button and enter parameter name as
“b” and type as int and click ok. Now change the return value in the code.
4. Right Click the “Myfirst-war” file and select the option new and select the option servlet
and give name “calcservlet” and package as “SessionBean” click Next. Select the Check
box “Add information to deployment descriptor (web.xml) and click Finish.
5. Right click in the next line of the class of the servlet code and select the option insert code
and then select the option “call Enterprise Bean” and select “calcbean” from “myfirst-ejb”
file.
6. Type the code which are given in bolded case.
7. Right Click the “myfirst-war” file and select the option new and select the option jsp and
give name “calcjsp” click Finish. Type the code of the jsp file.
8. Right click the Application from workspace and select the option deploy. Right click the
application and select the option Run.
PROGRAM:
clacbeanLocal.java
package SessionBean;
import javax.ejb.Local;
@Local
public interface calcbeanLocal {
Integer addition(int a, int b);
}
clacbean.java
package SessionBean;
import javax.ejb.Stateless;
@Stateless
public class calcbean implements calcbeanLocal {
@Override
public Integer addition(int a, int b) {
return (a+b);
}
}
clacservlet.java
package SessionBean;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class calcservlet extends HttpServlet {
@EJB
private calcbeanLocal calcbean;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet calcservlet</title>");
out.println("</head>");
out.println("<body>");
int a=Integer.parseInt(request.getParameter("t1"));
int b=Integer.parseInt(request.getParameter("t2"));
out.println("<h1>Sum = " + calcbean.addition(a, b) + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
calcjsp.java
OUTPUT:
RESULT:
Thus the program has been executed successfully and output verified.
EX.NO: 11
DATE:
AIM:
To develop an EJB application that demonstrates Stateful Session Bean.
PROCEDURE:
1. Start Netbeans IDE, Go to file menu and select new project, the new project
dialog box opens. From the Categories select the option JavaEE and from projects
select Enterprise Application and click Next, type the name as “MySecond”,
select location click next and finish.
2. Right click the source packages in “MySecond-ejb” select new and select
“session bean” and give a name for it as “hellobean” and package name as “Hello”
select session as “stateful” and select checkbox “local” and click Finish.
3. Inside the code of the class rightclick and select insertcode and select Add
Business Method. A dialog box opens give the name of the method as
“Helloname” and for return type click browse and select String (java.lang) and
click ok. Click the add button and enter the parameter name as “name” and type
as “String” and click ok. Now type the return value in the code.
4. Right Click the “MySecond-war” file and select the option new and select the
option servlet and give name “Helloservlet” and package as “Hello” click Next.
Select the Check box “Add information to deployment descriptor (web.xml) and
click Finish.
5. Right click in the next line of the class of the servlet code and select the option
insert code and then select the option “call Enterprise Bean” and select
“hellobean” from “MySecond-ejb” file.
6. Type the code which are given in bolded case. Type the code of the “index.html”
file.
7. Right click the Application from workspace and select the option deploy. Right
click the application and select the option Run.
PROGRAM:
hellobean.java
package Hello;
import javax.ejb.Stateful;
@Stateful
public class hellobean implements hellobeanLocal {
@Override
public String Helloname(String name) {
return "name";
}}
hellobeanLocal.java
package Hello;
import javax.ejb.Local;
@Local
public interface hellobeanLocal {
String Helloname(String name);
}
Helloservlet.java
package Hello;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Helloservlet extends HttpServlet {
hellobeanLocal hellobean = lookuphellobeanLocal();
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String n=request.getParameter("name");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Helloservlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello " + n + "</h1>");
out.println("</body>");
out.println("</html>");
}
}
private hellobeanLocal lookuphellobeanLocal() {
try {
Context c = new InitialContext();
return (hellobeanLocal) c.lookup("java:global/MySecond/MySecond-
ejb/hellobean!Hello.hellobeanLocal");
} catch (NamingException ne) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception
caught", ne);
throw new RuntimeException(ne);
} } }
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>
<form action="Helloservlet" method="POST">
ENTER YOUR NAME: <input type="text" name ="name">
<input type="submit" value="SUBMIT">
</form>
</body>
</html>
OUTPUT:
RESULT:
Thus the program has been executed successfully and output verified.