2013-10-08 116 views
0

嘗試使用jsp與bean進行交互,但是在運行時會出現一些錯誤。java ejb jsp未報告異常

下面是我在瀏覽器

生成的servlet錯誤出現的錯誤: [javac的] C:\太陽\ AppServerNew \域\ DOMAIN1 \生成的\ JSP \ J2EE的APPS \ ConverterApp \戰爭ic_war \ org \ apache \ jsp \ index_jsp.java:21:未報告的異常javax.naming.NamingException;必須被捕獲或聲明爲拋出 [javac] InitialContext ic = new InitialContext(); ^

生成的servlet錯誤: [javac的] C:\太陽\ AppServerNew \域\ domain1的\生成\ JSP \ J2EE-APPS \ ConverterApp \戰爭ic_war \組織\阿帕奇\ JSP \ index_jsp.java:22 :未報告的異常javax.naming.NamingException;必須被捕獲或聲明爲拋出 [javac] Object objRef = ic.lookup(「java:comp/env/ejb/Converter」); ^

生成的servlet錯誤: [javac的] C:\太陽\ AppServerNew \域\ domain1的\生成\ JSP \ J2EE-APPS \ ConverterApp \戰爭ic_war \組織\阿帕奇\ JSP \ index_jsp.java:24 :未報告的異常javax.ejb.CreateException;必須被捕獲或聲明爲拋出 [javac] converter = home.create();

的index.jsp

<%@ page import="converter.Converter, converter.ConverterHome, java.math.*, javax.ejb.*, javax.naming.*, 
javax.rmi.PortableRemoteObject, java.rmi.RemoteException" %> 
<%! 
    private Converter converter = null; 
    public void jspInit() { 
    try { 
     InitialContext ic = new InitialContext(); 
     Object objRef = ic.lookup("java:comp/env/ejb/Converter"); 
     ConverterHome home = (ConverterHome)PortableRemoteObject.narrow(objRef, ConverterHome.class); 
    converter = home.create(); 

    } catch (RemoteException ex) { 
    } 
    } 
%> 
<html> 
<head> 
    <title>Converter</title> 
</head> 

<body bgcolor="white"> 
<h1><center>Converter</center></h1> 
<hr> 
<p>Enter an amount to convert:</p> 
<form method="get"> 
<input type="text" name="amount" size="25"> 
<br> 
<p> 
<input type="submit" value="Submit"> 
<input type="reset" value="Reset"> 
</form> 
<% 
    String amount = request.getParameter("amount"); 
    if (amount != null && amount.length() > 0) { 
    BigDecimal d = new BigDecimal (amount); 
%> 
    <p><%= amount %> dollars are 
    <%= converter.dollarToYen(d) %> Yen. 
    <p><%= amount %> Yen are 
    <%= converter.yenToEuro(d) %> Euro. 
<% 
    } 
%> 
</body> 
</html> 

ConverterBean

package converter; 
import javax.ejb.*; 
import java.math.*; 
import java.rmi.*; 

public class ConverterBean implements SessionBean { 

BigDecimal yenRate = new BigDecimal("122.00"); 

BigDecimal euroRate = new BigDecimal("0.0077"); 


public BigDecimal dollarToYen(BigDecimal dollars) { 
    BigDecimal result = dollars.multiply(yenRate); 
    return result.setScale(2, BigDecimal.ROUND_UP); 
} 

public BigDecimal yenToEuro(BigDecimal yen) { 
    BigDecimal result = yen.multiply(euroRate); 
    return result.setScale(2, BigDecimal.ROUND_UP); 
} 

    public void ejbActivate(){} 

    public void ejbPassivate(){} 

    public void ejbRemove(){} 

    public void ejbCreate(){} 

    public void setSessionContext(SessionContext ctx){} 



} 

ConverterHome

package converter; 
import java.rmi.RemoteException; 
import javax.ejb.*; 

public interface ConverterHome extends EJBHome { 

Converter create() throws CreateException, RemoteException; 



} 

轉換

package converter; 
import java.rmi.RemoteException; 
import javax.ejb.*; 
import java.math.*; 

public interface Converter extends EJBObject{ 

public BigDecimal dollarToYen(BigDecimal dollars) 
throws RemoteException; 

public BigDecimal yenToEuro(BigDecimal dollars) 
throws RemoteException; 


} 

回答

1

更改jspInit()方法在index.jsp到:

public void jspInit() { 
    try { 
     InitialContext ic = new InitialContext(); 
     Object objRef = ic.lookup("java:comp/env/ejb/Converter"); 
     ConverterHome home = (ConverterHome) PortableRemoteObject.narrow(objRef, ConverterHome.class); 
     converter = home.create(); 

    }catch(NamingException ne){ 
    }catch (RemoteException ex) { 
    }catch(CreateException ce){ 
    } 
} 
0

您需要登錄NamingExceptionCreateException您的.jsp文件,在RemoteException之後。