An EJB application that demonstrates Session Bean (with appropriate business logic)
1.overview
2.
create a javaEE web application
3.
create session bean
4.
create servlet
5.
create jsp
6.
deploy the web application
1.overview
It
covers creating a Session Bean and accessing it in a Web Application using
JSPand Servlet.Enterprise JavaBeans technology is the server-side component
architecturefor developing and deploying business applications in Java EE. The
latest release of the technology, JSR 318: Enterprise JavaBeans 3.1, which is
available in the Java EE 6platform, further simplifies the technology and makes
many improvements.
There
are two types of EJB components: Session beans and Message-driven beans. you will create a JEE 6 Web Application and
add the following components to it - Stateless Session Bean, a Servlet, and a
JSP. The JSP will contain a form
allowing you to specify a name. The form will submit to a Servlet which will
read the name entered as the form parameter. The Servlet will pass the name to
a sayHello method that is in the Session bean that you will create.
2. create a javaEE web application
To
create a Java EE Web Application,
perform the following steps in the NetBeans IDE.
Create
a new Web Application.
Select
File->New Project from the NetBeans menu.
Select
the Java Web category and a project type of
Web Application.
Click
Next.
3.
create session bean
To
create a stateless session bean that is accessed using the local client access
mode, perform the following steps in NetBeans IDE.Right-click on the prg11
project and select New->Other.
In
the New File window, select a category of Enterprise JavaBeans and a file type
of Session Bean.
Click
Next.
Specify
the Session Bean information as follows:
EJB
Name: hello
Package
Name: p11
Ensure
Stateless is selected as the Session Type
Select Local as the option for Create Interface.
Click
Finish.
Double-click
helloLocal.java in the Source Packages node to open in the code editor.
Add
the following line of code to the helloLocal interface to declare the sayHello
method.
String sayHello(String name) ;
Double-click
hello.java in the Source Packages node to open
in the code editor.
Implement sayHello method in the hello(SessionBean.)
public
String sayHello(String name) {
return
"Hello " + name;
}
4.create servlet
To
create a Servlet, perform the below steps in NetBeans IDE.
Right-click
on the prg11 project and select
New->Other.
In
the New File window, select a category of Web and a file type of Servlet.
Click
Next.-->Specify the Servlet
information as follows:
Class
Name: helloServlet
Package
Name: hs
Click
Finish.-->
Perform
the following changes to the helloServlet.
a. Import the following package.
import
javax.ejb.EJB;
b.
Add a field of type helloLocal named hello
@EJB
private
helloLocal hello;
---->
In
the ProcessRequest method of SayHelloServlet, make the following changes.
a. Add the below lines of code
String
str1=request.getParameter("name");
String
str2=hello.sayHello(str1);
b. Add the below line of code to modify the HTML
response produced by the
SayHelloServlet.
out.println("<h1>"
+ str2 + "</h1>");
5.
Create a jsp
-------------------
To
modify index.jsp, perform the following steps in NetBeans IDE.Open the existing
index.jsp file from the Web Pages portion of the prg11 project.
Modify
the title and heading of the page to Say Hello.
Add
a form to the body of the index.jsp page which contains one text box named
name and a submit button named OK.
<form method="post"
action="helloServlet">
Enter Your Name: <input
type="text" name="name">
<input
type="submit" value="OK">
</form>
6.Deploy
the web application
-------------------------------
To
deploy and run the application, perform the following steps in NetBeans
IDE.Right-click prg11 project in the projects window and select Build. In the Projects window, right-click
prg11 and select Run.
---------------------------------------------------------------------------
//index.jsp
<%@page
contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE
html>
<html>
<head>
<meta
http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>
Say Hello</title>
</head>
<body
bgcolor="orange">
<h1>Say
Hello!</h1>
<form method="post"
action="helloServlet">
Enter Your Name: <input type="text"
name="name">
<input
type="submit" value="OK">
</form>
</body>
</html>
//helloServlet.java
package
hs;
import
java.io.IOException;
import
java.io.PrintWriter;
import
javax.ejb.EJB;
import
javax.servlet.ServletException;
import
javax.servlet.annotation.WebServlet;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
p11.helloLocal;
@WebServlet(name
= "helloServlet", urlPatterns = {"/helloServlet"})
public
class helloServlet extends HttpServlet {
@EJB
private
helloLocal hello;
protected
void processRequest(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
String
str1=request.getParameter("name");
String
str2=hello.sayHello(str1);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try
{
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet
helloServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>"
+ str2 + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
@Override
protected
void doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
processRequest(request,
response);
}
@Override
protected
void doPost(HttpServletRequest request, HttpServletResponse response)
throws
ServletException, IOException {
processRequest(request,
response);
}
@Override
public
String getServletInfo() {
return
"Short description";
}
}
//hello.java
package
p11;
import
javax.ejb.Stateless;
@Stateless
public
class hello implements helloLocal
{
@Override
public
String sayHello(String name)
{
return
"Hello " + name;
}
}
//helloLocal.java
package
p11;
import
javax.ejb.Local;
@Local
public
interface helloLocal
{
String sayHello(String name);
}
No comments:
Post a Comment