📄 ch13s21.html
字号:
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>How to use Applets to access EJBs in JBoss</title><link rel="stylesheet" href="styles.css" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/styles.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets Vimages/callouts/"><link rel="home" href="index.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/index.html" title="JBoss 3.0 Documentation"><link rel="up" href="ch13.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13.html" title="Chapter 13. HOWTO"><link rel="previous" href="ch13s16.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s16.html" title="How to Integrate a Web Container into JBoss"><link rel="next" href="ch13s26.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s26.html" title="How to Integrate Custom Services via MBeans"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table border="0" cellpadding="0" cellspacing="0" height="65"><tr height="65"><td rowspan="2"><img src="jboss.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/jboss.gif" border="0"></td><td rowspan="2" background="gbar.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/gbar.gif" width="100%" align="right" valign="top"><a href="index.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/index.html"><img src="doc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/doc.gif" border="0"></a><a href="ch13.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13.html"><img src="toc.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/toc.gif" border="0"></a><a href="ch13s16.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s16.html"><img src="prev.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/prev.gif" border="0"></a><a href="ch13s26.html" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s26.html"><img src="next.gif" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/next.gif" border="0"></a></td></tr><tr></tr></table><div class="section"><a name="howtoclientapplet"></a><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="howtoclientapplet"></a>How to use Applets to access EJBs in JBoss</h2></div></div><p>Author: <span class="author">Sacha Labourey</span>
<tt><<a href="mailto:sacha.labourey@cogito-info.ch">sacha.labourey@cogito-info.ch</a>></tt>
</p><p>
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><a name="d0e9424"></a>Note</h3><p>This document applies to JBoss release 2.4 and later. If you are
using an earlier version of JBoss upgrade to the 2.4 version or later.</p></div>
</p><div class="section"><a name="ClientAppletIntroduction"></a><div class="titlepage"><div><h3 class="title"><a name="ClientAppletIntroduction"></a>Introduction</h3></div></div><p>This chapter describes how to access EJBs running in JBoss from an Applet running in a browser.</p><p>When a JVM runs an Applet, it verifies its conformity to a set of security rules which have been established in order to protect the user from malicious code. Some of these rules are:</p><div class="itemizedlist"><ul><li><p><a name="d0e9436"></a>No access to the local filesystem</p></li><li><p><a name="d0e9439"></a>No access to any host on the Internet but the one from which the Applet was downloaded</p></li><li><p><a name="d0e9442"></a>Restricted access to system properties</p></li></ul></div><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title"><a name="d0e9445"></a>Note</h3><p>More information on the Applet security model can be found here: <a href="javascript:if(confirm('http://java.sun.com/sfaq/ \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://java.sun.com/sfaq/'" tppabs="http://java.sun.com/sfaq/" target="_top">http://java.sun.com/sfaq/</a>
</p></div><p>These restrictions do have consequences on what can be done from a client Applet that want to access EJBs over the network.</p><div class="itemizedlist"><ul><li><p><a name="d0e9454"></a>
<a href="ch13s21.html#clientappletunsigned" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s21.html#clientappletunsigned" title="Restricted (unsigned) Applet">Unsigned client applet</a>
</p></li><li><p><a name="d0e9460"></a>
<a href="ch13s21.html#clientappletsigned" tppabs="http://www.huihoo.org/jboss/online_manual/3.0/ch13s21.html#clientappletsigned" title="Signed client Applet">Signed client applet</a>
</p></li></ul></div></div><div class="section"><a name="clientappletunsigned"></a><div class="titlepage"><div><h3 class="title"><a name="clientappletunsigned"></a>Restricted (unsigned) Applet</h3></div></div><p>The main consequence of the sandbox on EJB access from an Applet is that we will need to download the Applet from a Web server that has the same IP address as the J2EE server running the EJB we want to access. Nevertheless, it is still possible, thanks to IP traffic load-balancers for example, to have more than one server hidden behind a single IP address.</p><p>In our example, we will build a stateless session bean that simply returns the server date and hostname in a friendly message. To try this example, you’d better install the Java plug-in: support for Java in Web browsers in somewhat hazardous.</p><p>We first build our remote interface:</p><pre class="programlisting">public interface TestApplet extends EJBObject
{
public String getMessage() throws RemoteException;
}</pre><p>And the Home interface:</p><pre class="programlisting">public interface TestAppletHome extends EJBHome
{
public TestApplet create() throws RemoteException, CreateException;
}</pre><p>Now the simple EJB implementation:</p><pre class="programlisting">public class TestAppletBean implements SessionBean
{
private SessionContext sessionContext;
public void ejbCreate() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate(){}
public void setSessionContext(SessionContext context)
{
sessionContext = context;
}
public String getMessage()
{
String hostname = "Unknown host";
try
{
hostname = java.net.InetAddress.getLocalHost ().getHostName ();
} catch (java.net.UnknownHostException uhe) { }
String now = java.text.DateFormat.getDateInstance().format (new java.util.Date());
System.out.println ("getMessage has been called from Applet!");
return "Hello from " + hostname + ", here it is: " + now + ".";
}
}</pre><p>And its associated ejb-jar.xml descriptor:</p><pre class="programlisting"><?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN' 'http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd'>
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>TestAppletBean</ejb-name>
<home>org.jboss.docs.appletclient.interfaces.TestAppletHome</home>
<remote>org.jboss.docs.appletclient.interfaces.TestApplet</remote>
<ejb-class>org.jboss.docs.appletclient.ejb.TestAppletBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>TestAppletBean</ejb-name>
<method-intf>Remote</method-intf>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar></pre><p>We now implements a simple Applet that will have a single button. When pressed, the Applet will call the EJB and display its message.</p><pre class="programlisting">public class AppletEjbCaller extends JApplet
{
JPanel buttonPanel = new JPanel();
JButton callEjbButton = new JButton();
JPanel labelPanel = new JPanel();
JLabel ejbMessageLabel = new JLabel();
// Construct the Applet
//
public AppletEjbCaller () {}
// Initialize the applet
//
public void init()
{
try
{
this.setSize(new Dimension(400, 65));
callEjbButton.setText("Get server message");
callEjbButton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
callEjbButton_actionPerformed(e);
}
});
ejbMessageLabel.setText("no call performed yet.");
this.getContentPane().add(buttonPanel, BorderLayout.CENTER);
buttonPanel.add(callEjbButton, null);
this.getContentPane().add(labelPanel, BorderLayout.SOUTH);
labelPanel.add(ejbMessageLabel, null);
}
catch(Exception e)
{
e.printStackTrace();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -