Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlets - JSP communication

 
Mohamed AshrafM
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Testing!!!
 
Poornima Shetty
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic