【问题标题】:java ejb jsp unreported exceptionjava ejb jsp 未报告的异常
【发布时间】:2013-10-08 21:42:47
【问题描述】:

尝试使用 jsp 与 bean 交互但是,在运行它时会出现一些错误。

这是我在浏览器中看到的错误

生成的 servlet 错误: [javac] C:\Sun\AppServerNew\domains\domain1\generated\jsp\j2ee-apps\ConverterApp\war-ic_war\org\apache\jsp\index_jsp.java:21: 未报告的异常 javax.naming.NamingException;必须被抓住或宣布被扔掉 [javac] InitialContext ic = new InitialContext(); ^

生成的 servlet 错误: [javac] C:\Sun\AppServerNew\domains\domain1\generated\jsp\j2ee-apps\ConverterApp\war-ic_war\org\apache\jsp\index_jsp.java:22: 未报告的异常 javax.naming.NamingException;必须被抓住或宣布被扔掉 [javac] 对象 objRef = ic.lookup("java:comp/env/ejb/Converter"); ^

生成的 servlet 错误: [javac] C:\Sun\AppServerNew\domains\domain1\generated\jsp\j2ee-apps\ConverterApp\war-ic_war\org\apache\jsp\index_jsp.java:24: 未报告的异常 javax.ejb.CreateException;必须被抓住或宣布被扔掉 [javac] 转换器 = 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){}



}

转换器主页

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;


}

【问题讨论】:

    标签: java jsp ejb javabeans


    【解决方案1】:

    index.jsp中的jspInit()方法改为:

    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){
        }
    }
    

    【讨论】:

      【解决方案2】:

      您需要在.jsp 文件上的RemoteException 之后输入NamingExceptionCreateException

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-05
        • 1970-01-01
        相关资源
        最近更新 更多