传值就是remote方式,参数必须实现序列化
传址就是local方式。
entitybean:
package com.manfred.entity;
import java.io.Serializable;
public class User implements Serializable {
private String username;
private int id;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
session bean:
package com.manfred.session.impl;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import com.manfred.entity.User;
import com.manfred.session.UserManagerLocal;
import com.manfred.session.UserManagerRemote;
@Stateless
@Remote({UserManagerRemote.class})
@Local({UserManagerLocal.class})
public class UserManagerBean implements UserManagerLocal, UserManagerRemote {
public void addUser(User user) {
user.setId(1000);
System.out.println("username = " + user.getUsername());
}
}
两个接口方式(remote和local)
package com.manfred.session;
import com.manfred.entity.User;
public interface UserManagerLocal {
public void addUser(User user);
}
package com.manfred.session;
import com.manfred.entity.User;
public interface UserManagerRemote {
public void addUser(User user);
}
web中的jsp文件的编码:
<%@page import="com.manfred.session.impl.UserManagerBean"%>
<%@ page language="java" import="java.util.*,javax.naming.*,com.manfred.session.*,com.manfred.entity.*" pageEncoding="GB18030"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
================================<br>
<%
final Properties jndiProperties = new Properties();
jndiProperties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
final Context context = new InitialContext(jndiProperties);
final String appName = "";
final String remoteModuleName = "EJB_03";
final String localModuleName = "EJB_03_web";
final String distinctName = "";
final String beanName = UserManagerBean.class.getSimpleName();
final String remoteViewClassName = UserManagerRemote.class.getName();
final String localVierClassName = UserManagerLocal.class.getName();
String remoteLookupStr = "ejb:" + appName + "/" + remoteModuleName + "/" + distinctName + "/" + beanName + "!" + remoteViewClassName;
out.println(remoteLookupStr +"<br>");
UserManagerRemote umr = (UserManagerRemote) context.lookup(remoteLookupStr);
//UserManagerRemote umr = (UserManagerRemote) context.lookup("java:global/EJB_03_web/UserManagerBean!com.manfred.session.UserManagerRemote");
User user1 = new User();
user1.setUsername("帅远");
umr.addUser(user1);
out.println("remote传值,user的id为:" + user1.getId());
%>
<br>
===============================================
<br>
<%
String localLookupStr = "java:module/" + beanName + "!" + localVierClassName;
out.println(localLookupStr+"<br>");
UserManagerLocal uml = (UserManagerLocal) context.lookup(localLookupStr);
//UserManagerLocal uml = (UserManagerLocal) context.lookup("java:global/EJB_03_web/UserManagerBean!com.manfred.session.UserManagerLocal");
User user2 = new User();
user2.setUsername("帅远");
uml.addUser(user2);
out.println("local传址,user的id为:" + user2.getId());
%>
</body>
</html>