servelt中调用ejb的问题
本文导语: 请看一下下面两段代码,为什么在DinnerClient 调用DinnerDBEJB 成功,而ReceiptServlet调用DinnerDBEJB 不成功 import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import javax.naming.*; import javax.rmi.PortableRemote...
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;
/**
* An HTTP servlet that responds to the POST method of the HTTP protocol.
* The Receipt servlet updates the book database inventory, invalidates the user session,
* thanks the user for the order. */
public class ReceiptServlet extends HttpServlet {
private DinnerDBEJB myDinner;
public void init() throws ServletException {
try{
Context initial = new InitialContext();
Context myEnv = (Context)initial.lookup("java:comp/env");
Object objref = myEnv.lookup("ejb/DinnerDBEJBHome");
DinnerDBEJBHome home =
(DinnerDBEJBHome)PortableRemoteObject.narrow(objref,
DinnerDBEJBHome.class);
DinnerDBEJB myDinner = home.create();
}catch(Exception ex)
{System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
}
}
public void destroy() {}
public void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException
{
String EmpID = request.getParameter("EmpID");
String Name = request.getParameter("Name");
String STime = request.getParameter("Time");
String Type = request.getParameter("Type");
String PayType = request.getParameter("PayType");
String Price = request.getParameter("Price");
String Account = request.getParameter("Account");
response.setContentType("text/html");
response.setBufferSize(8192);
PrintWriter out = response.getWriter();
// then write the response
out.println("" +
"" + "error-uuu" + "" );
out.println("" + EmpID+"--"+Name+"--"+STime+"--"+Type+"--"+PayType+"--"+Price+"--"+Account);
// Update the inventory
try {out.println("" + "startbook");
myDinner.Book(EmpID,Name,STime,Type,PayType,Price,Account);
out.println("" + "endbook");
} catch (Exception ex) {
ex.printStackTrace();
return;
}
/// Locale locale=request.getLocale();
// messages = ResourceBundle.getBundle("messages.BookstoreMessages", locale);
out.println("" + "BookSuccess");
out.println("
" + "ContinueShopping"+ " " +
"");
}
public String getServletInfo() {
return "The Receipt servlet insert a bookdinner record into database dinner and display a success html page";
}
}
=============================================
============================================
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import java.math.BigDecimal;
//import Converter;
//import ConverterHome;
public class DinnerClient {
public static void main(String[] args) {
try {
Context initial = new InitialContext();
Context myEnv = (Context)initial.lookup("java:comp/env");
Object objref = myEnv.lookup("ejb/DinnerDBEJBHome");
DinnerDBEJBHome home =
(DinnerDBEJBHome)PortableRemoteObject.narrow(objref,
DinnerDBEJBHome.class);
DinnerDBEJB mydinner = home.create();
mydinner.Book("d","d","d","d","d","d","d");
System.exit(0);
} catch (Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
}
}
}
|