当前位置: 技术问答>java相关
如何用SERVLET调用在本地或远程的EJB组件?
来源: 互联网 发布时间:2015-08-29
本文导语: 要用SERVLET调用EJB-SESSION BEAN,如何调用? 如果SERVLET和EJB不在同一台服务器又如何呢? 请高手给出源码谢谢 | servlet代码: package ne; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; impor...
要用SERVLET调用EJB-SESSION BEAN,如何调用?
如果SERVLET和EJB不在同一台服务器又如何呢?
请高手给出源码谢谢
如果SERVLET和EJB不在同一台服务器又如何呢?
请高手给出源码谢谢
|
servlet代码:
package ne;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
import java.text.*;
/**
*
*
*
*
* @author unascribed
* @version 1.0
*/
public class servletu extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
TestHome home;
TestDB testdb;
final String testURL = "ne/test.jsp";
final String test2URL = "ne/test2.jsp";
public void init() throws ServletException {
try{
InitialContext ic = new InitialContext();
Object objRef = ic.lookup("TestHome");
home = (TestHome)PortableRemoteObject.narrow(objRef, TestHome.class);
}catch(Exception a){
System.out.println("aaaaaaaaaaa"+a.toString());
}
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
//PrintWriter out = response.getWriter();
HttpSession session = request.getSession(true);
Test love1=null;
Iterator i=null;
int j=0;
String yztj=request.getParameter("yztj");
String name=request.getParameter("name");
try
{
if (yztj.equals("确认提交")){
String a=(String)request.getParameter("a");
String b=request.getParameter("b");
String c=request.getParameter("c");
String d=request.getParameter("d");
if (d==null){
d="0";
}
float pri=Float.parseFloat(d);
Float aaa=new Float(pri);
love1 = home.create(a,b,c,aaa);
}
if (yztj.equals("查询确认")){
int count=countfindname(name);
System.out.println("count=============================="+count);
testdb=new TestDB(count);
i=home.findname(name).iterator() ;
while(i.hasNext()){
Test t=(Test)PortableRemoteObject.narrow(i.next(),Test.class);
testdb.productid[j]=t.getProductid();
testdb.name[j]=t.getName();
testdb.desc[j]=t.getDesc();
testdb.price[j]=t.getPrice();
j++;
}
}
}
catch (Exception e)
{
System.out.println("2222222222222222222222222222222222222"+e.toString());
}
try{
testdb.i=j;
session.setAttribute("testdb",testdb);
//mLog.Out("转移" + HomeURL);
if (yztj.equals("确认提交")){
getServletContext().getRequestDispatcher(test2URL).forward(request, response);
return;
}else if(yztj.equals("查询确认")){
getServletContext().getRequestDispatcher(testURL).forward(request, response);
}
return;
}catch(Exception ee){
System.out.println("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww"+ee.toString());
}
}
public int countfindname(String name){
Iterator it=null;
int count=0;
try{
it=home.findname(name).iterator() ;
while(it.hasNext()){
it.next();
count++;
}
}catch(Exception e){
it=null;
System.out.println("Exception!");
}
it=null;
return count;
}
}
jsp代码:
Converter
Converter
package ne;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
import java.text.*;
/**
*
Title:
*
Description:
*
Copyright: Copyright (c) 2002
*
Company:
* @author unascribed
* @version 1.0
*/
public class servletu extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
TestHome home;
TestDB testdb;
final String testURL = "ne/test.jsp";
final String test2URL = "ne/test2.jsp";
public void init() throws ServletException {
try{
InitialContext ic = new InitialContext();
Object objRef = ic.lookup("TestHome");
home = (TestHome)PortableRemoteObject.narrow(objRef, TestHome.class);
}catch(Exception a){
System.out.println("aaaaaaaaaaa"+a.toString());
}
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
//PrintWriter out = response.getWriter();
HttpSession session = request.getSession(true);
Test love1=null;
Iterator i=null;
int j=0;
String yztj=request.getParameter("yztj");
String name=request.getParameter("name");
try
{
if (yztj.equals("确认提交")){
String a=(String)request.getParameter("a");
String b=request.getParameter("b");
String c=request.getParameter("c");
String d=request.getParameter("d");
if (d==null){
d="0";
}
float pri=Float.parseFloat(d);
Float aaa=new Float(pri);
love1 = home.create(a,b,c,aaa);
}
if (yztj.equals("查询确认")){
int count=countfindname(name);
System.out.println("count=============================="+count);
testdb=new TestDB(count);
i=home.findname(name).iterator() ;
while(i.hasNext()){
Test t=(Test)PortableRemoteObject.narrow(i.next(),Test.class);
testdb.productid[j]=t.getProductid();
testdb.name[j]=t.getName();
testdb.desc[j]=t.getDesc();
testdb.price[j]=t.getPrice();
j++;
}
}
}
catch (Exception e)
{
System.out.println("2222222222222222222222222222222222222"+e.toString());
}
try{
testdb.i=j;
session.setAttribute("testdb",testdb);
//mLog.Out("转移" + HomeURL);
if (yztj.equals("确认提交")){
getServletContext().getRequestDispatcher(test2URL).forward(request, response);
return;
}else if(yztj.equals("查询确认")){
getServletContext().getRequestDispatcher(testURL).forward(request, response);
}
return;
}catch(Exception ee){
System.out.println("wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww"+ee.toString());
}
}
public int countfindname(String name){
Iterator it=null;
int count=0;
try{
it=home.findname(name).iterator() ;
while(it.hasNext()){
it.next();
count++;
}
}catch(Exception e){
it=null;
System.out.println("Exception!");
}
it=null;
return count;
}
}
jsp代码:
Converter
Converter
Enter an amount to convert:
0 ) {
BigDecimal d = new BigDecimal (amount);
%>
dollars are Yen.
Yen are Euro.
|
和上面一样处理。你的Servlet所在的工程需要一个EJB客户端的jar包就行了。