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

EJB Example With JSP and Servlet

This document provides steps to create a simple EJB example with JSP and Servlet: 1. Create an EJB module project and generate a stateless session bean with a remote business method. 2. Create a web application project and add a servlet. The servlet calls the business method on the session bean. 3. Add an index.jsp with a form that submits to the servlet. The servlet gets the form parameter and passes it to the session bean method. 4. The session bean processes the parameter and returns a response that is displayed on the JSP page.

Uploaded by

suganya004
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
444 views

EJB Example With JSP and Servlet

This document provides steps to create a simple EJB example with JSP and Servlet: 1. Create an EJB module project and generate a stateless session bean with a remote business method. 2. Create a web application project and add a servlet. The servlet calls the business method on the session bean. 3. Add an index.jsp with a form that submits to the servlet. The servlet gets the form parameter and passes it to the session bean method. 4. The session bean processes the parameter and returns a response that is displayed on the JSP page.

Uploaded by

suganya004
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

A Simple EJB Example With JSP and Servlet

Stateless Session Bean program to combine with JSP and Servlet.

File -> New Project -> Java EE -> EJB Module

Next, provide the Project Name

Select Webserver and J2EE version, Finish.


Right click on the Project -> New -> Session Bean..

Provide a Bean Name


Assign a Package Name
Select Session Type as Stateless
And set the Interface as Remote, Finish
Open SessionBean.java
Press <alt + insert>, From the list select Add Business Method

Provide Menthod Name


Define a Return Type
Add Parameter by clicking on Add Button
Change the given method body to following code.

@Stateless
public class SessionBean implements SessionBeanRemote {

public String businessMethod(String name) {


return "Welcome " + name;
}
}

Right Click on project and click on Build


Now the componenct is created and we have to use that component in Web Container.
Here in this example we are going to use the EJB component via Servlet.

File -> New Project -> Java Web -> Web Application

Next, Provide Project Name


Next, Select Web Server and J2EE version, Finish.

Right Click on Web Pages -> New -> Servlet..


Provide Servlet Name and Package location

Next, Provide a URL pattern for the Servlet, Finish.


In Servlet Page, remove comment from processRequest method body.
Press <alt + insert>, From the list Select Call Interprise Bean

Select SessionBean, Ok.


Go to index.jsp and create a form with a textbox and button and action to be
use_ejb.do (servlet url pattern)

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

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

<title>EJB Example</title>

</head>

<body>
<form action="use_ejb.do" method="post">

<input type="text" name="nm">

<input type="submit" value="OK">

</form>

</body>

</html>

Then go to Servlet and send the received value from textbox to the RemoteBean
method to get result from the RemoteBean.

protected void processRequest(HttpServletRequest request, HttpServletResponse


response)

throws ServletException, IOException {

String str1=request.getParameter("nm");

String str2=sessionBean.businessMethod(str1);

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

try {

out.println("<html>");

out.println("<head>");

out.println("<title>Servlet UseBean</title>");

out.println("</head>");

out.println("<body>");

out.println("<h1>" + str2 + "</h1>");

out.println("</body>");

out.println("</html>");
} finally {

out.close();

Output :

You might also like