EJB学习笔记-6-传值和传址

传值就是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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值