File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Servlets - JSP communication Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlets - JSP communication" Watch "Servlets - JSP communication" New topic

Servlets - JSP communication

Mohamed AshrafM

Joined: May 22, 2001
Posts: 5
I have decided to do EJB for my project. Here, I have decided the architecture like this. Jsp/html calls servlet then servlet interacts with EJB then the output should display in JSP...How do i implement this??Anyone of you have sample code share it with me..Think it, servlet gets the data from the database..
Thanks in advance..
with regards,
Mohamed Ashraf.M
Victor Boveda

Joined: May 22, 2001
Posts: 6
You can pass the Bean as an attribute to jsp page.
Something like this:
In your servlet:
try {
Context ctx = new InitialContext();
BeanHome home = ctx.lookup("bean");
Bean b = home.findByPrimaryKey("key");
request.setAttribute("myBean", b);
ServletContext context = getServletContext();
RequestDispatcher disp = context.getRequestDispatcher(page);
disp.forward(request, response);
} catch (.....
In your page
<% Bean b = (Bean) request.getAttribute("myBean"); %>
Code = <%= b.getCode() %>
Hope this helps

[This message has been edited by Victor Boveda (edited May 22, 2001).]
Poornima Shetty

Joined: Dec 12, 2000
Posts: 26
Poornima Shetty

Joined: Dec 12, 2000
Posts: 26
Your EJB that has all the methods defined in the remote interface.
import java.util.*;
public class FirstEJB() implements SessionBean or EntityBean for that matter
public String getString() throws RemoteException
return "Hello";
In Your Servlet:
Here in the servlet what you do is,get the InitialContext object to do a lookup for the EJB.
Once the lookup is done the reference to home object is assigend to "home" as in line 2.
Now using the home object you request for a remote object which is assigned to "remote" as in
line 3.Once you have the remote object with you ,you can invoke any methods that you have implemented
in your EJB.Now as you have invoked the getString method you have the string value with you
which you are sending to the JSP.

1.Context ctx = new InitialContext();
2.FirstEJBHome home = (FirstEJBHome)ctx.lookup("/FirstEJB");
3.FirstEJB remote = home.create();
4.String returnValue = remote.getString();
6.String sendTo = "/yourpackage/test.jsp";

In your JSP
Here basically in line by saying getAttribute,you are getting the string value that you had set in the servlet.
4. String strValue = (String)request.getAttribute("stringValue");
5. if (!strValue.equals(""))
6. {
9.<%= strValue %>

The above description is very much suitable for the architecture of
JSP----->Servlet------->EJB--------->Any Backend(Oracle,syabase,DB2,Ingris etc.,)
I hope this shall be of some use to you ,if not you are most welcome to ask .........

I agree. Here's the link:
subject: Servlets - JSP communication
jQuery in Action, 3rd edition