wood burning stoves 2.0*
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
Author

Servlets - JSP communication

Mohamed AshrafM
Greenhorn

Joined: May 22, 2001
Posts: 5
Hi,
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
Greenhorn

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
Victor.

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

Joined: Dec 12, 2000
Posts: 26
Hi,
Testing!!!
Poornima Shetty
Greenhorn

Joined: Dec 12, 2000
Posts: 26
Your EJB that has all the methods defined in the remote interface.
import java.util.*;
import java.io.*;
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();
5.request.setAttribute("stringValue",returnValue);
6.String sendTo = "/yourpackage/test.jsp";
7.getServletContext().getRequestDispatcher(sendTo).include(request,response);

In your JSP
Here basically in line by saying getAttribute,you are getting the string value that you had set in the servlet.
1.<table>
2.<tr>
3.<%
4. String strValue = (String)request.getAttribute("stringValue");
5. if (!strValue.equals(""))
6. {
7.%>
8.<td>
9.<%= strValue %>
10.</td>
11.</tr>
12.</table>

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 .........

....P...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlets - JSP communication