服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

jbuilder4入门


  1. 前言

  经过对jbuilder 4 enterprise 版的几天使用,逐步了解了jbuilder 4一些让人向往的特性,如功能丰富的向导,对ejb/servlet/jsp的直接调试能力。深深的为其震撼,现将一些收获整理,希望能以此提高我们开发的速度!

  2. ejb

  开发环境的配置

   要进行ejb的开发,需要有jbuilder4 enterprise、borland application server 4.x.

  安装

   先安装jbuilder 4,假设你要安装到

   c:/jbuilder40 (default).

    安装 jbuilder foundations

    安装 jbuilder 4 enterprise.

   要安装bas(ias)4.x、请先下载jdk1.2.2以上版本,j2eesdk1.2.1以上版本。

    安装 j2eesdk 1.2.1 to c:/j2eesdk1.2.2 (default).

    安装 borland application server 4.5.

  这里假设你将bas安装到c:/bas45 (not default),osagent使用缺省的端口 (14000)

   bas与jbuilder4的集成

   将jbuilder open tool bas45.jar 从 c:/bas45/console/jbuilder/jb40 拷贝到c:/jbuilder40/lib/ext 目录下,重启jbuilder.

  1. 选择tools | enterprise setup 。

  2. 在 application server 页中选择bas 4.5,在bas installation directory点中选择输入 c:/bas45

  3. 点ok。系统将提示重启jbuilder,请确认。

  4. 选择tools | enterprise setup

  5. 选择corba页,在path for orb tools中输入c:/bas45/bin

  6. 点library for projects下的…按钮,选择bas 4.5 。
  实例操作步骤

   下面将在jbuilder里创建一个简单的ejb,请对照下面步骤执行

   第一步,创建工程:

    选择 file | new project…

    将工程目录设为 quickstart.

    将根目录设为 c:/project.

    按 finish 按钮.


  第二步,创建ejb group。

  ejb group将打包成jar文件时自动加入里面的包内的ejb。:

   选择 file | new… | enterprise tab | empty ejb group.

   设置组的名称为quickstart,jbuilder将在此工程的目录里生成一个quickstart.jar文件。.

   点 ok.

  第三步、建立bean

  接着使用向导建立bean:

   file | new… | enterprise tab | enterprise javabean.

   点 next to将这个bean加入刚刚创建的ejb组。

   将类名高为tellerbean 点next.

  不要改变接口的名称,但是要将bean home改为tellerhome。

  现在,可以看到工程中有三个类. tellerhome和teller 是ejb容器需要实现的。

  tellerbean是我们要加入的商业逻辑的类,双击tellerbean在其中加入以下代码:

public int gettellerno() {

return 1;

}

save all your work and make .

  第四步,发布方法

  现在我们需要将方法暴露给容器。双击tellerbean,我们可以在bean这一页中看到一个叫methods的页。步骤:

  双击tellerbean.java。

  选中 bean 页,再选methods 页。

  选中 gettellerno 方法的复选框(save all,这时如果你打开teller.java,你可以看到其中声明了一个新的方法的。)


  现在我们写完了ejb的代码。

  第五步,配置

  现在我们要完成及检查配置描述(deployment descriptor)。配置描述是ejb的重要部分,它将发布配置与发布代码分离开,以我们可以在不改变源代码的情况以分解与调配一个应用。

  jbuilder已经为我们生成了一部分配置描述,现在我们来作些调整:

   双击double-click quickstart.ejbgroup to view it.

   在左下的结构窗格中展开tellerbean ,点 container transactions.

  点 add 为我们的方法加入一个事务属性为 ‘required’的容器事务,点verify ,应该是0 warnings,0 errors. 点save all ,再点make 。


  第六步,写客户端测试代码。

  现在我们完成了服务端的编写,现在我们要写个java客户端来调用ejb内的gettellerno。使用向导来创建一个ejb test client。

   file | new… | enterprise tab | ejb test client

   将类名认为 client

   只选中 generate main function

   点ok.

  在client类中加入以下代码。注意是在第一个try语句块的最后。

teller teller = tellerhome.create();

int t = teller.gettellerno();

system.out.println(“using teller no: “ + t);

  第七步,编译

  最后生成rmi/iiop的存根(stub)来调用远程方法。

   右击tellerhome.java 选中 properties.

   在 build | visibroker 页中, 选中 generate iiop.

   点 ok.

   save all 再 make .

  现在我们已经完成了开发。
  第八步,运行

  现在我们将在jbuilder4内运行容器来执行这个bean,选运行visibroker smart agent:

   tools | visibroker smart agent

  设置运行容器的参数

   run | configurations…

   编辑<default>的设置.

   选中 ejb 页.

   点 ok, ok.

   运行这个container 等待它初始化

   右击client.java,选中run ,运行客户端。

  在消息窗口可以看到这样的输出信息。

  c:/jbuilder40/jdk1.3/bin/javaw ?cclass… using teller no: 1
.

  如何调试

  1. 设定断点

  在tellerbean中找到这一行

   return 1;

  右击这一行,选择toggle breakpoint(或按快捷键f5),这一行将会加亮成红色。

  2. 装入容器

  点 ,如果在消息框内出现

borland application server customer


........ done


initializing jdb............. done


initializing ejbs............. done


container [ejbcontainer] is ready


ejb container statistics

========================

time fri jun 08 21:51:22 cst 2001

memory (used) 1304 kb (max 1304 kb)

memory (total) 2292 kb (max 2292 kb)

memory (free) 43.0%

------------------------

home tellerhome

total in memory 0

total in use 0

========================

  则可证明container运行成功。

  3. 运行客户端

  用右键点击client.java,选中run,在调用远程方法时,将跟踪到ejb的内部,想想这个,真是爽呀!jbuilder4开发ejb,真的历害。
  3. jsp

  开发环境的配置

  jbuilder提供了一个jsp开发的完整系统,包括一个用来创建jsp文件的jsp向导,及对jsp标记的codeinsight(加亮显示,自动完成)技术,集成的tomcat引擎(支java servlet 2.2 及jsp 1.1 ),可以使用户在集成环境下调试jsp代码。

  系统无需特别的设置,便可以用于开发jsp。

  实例操作步骤

   1. 生成一个jsp工程

   选择 file|close project关闭所有的打开的工程。

   选择 file|new。

   选中object gallery的new页中的 javaserver page. 这时将出现工程向导。

   把project name改成samplejsp.。

   单击finish按钮接受其缺省的配置。

   这时工程就创建了,系统将弹出jsp向导。

   在jsp向导的第一步中输入jspwithcountingbean,单击finish。

   jsp创建成功。

   工程的主类是生成的jsp文件,下面两个文件将加入工程中:

    jspwithcountingbean.jsp ?c 一个包含jsp标记的html文件。

    jspwithcountingbeanbean.java ?c 一个jsp文件调用的bean。

   双击在工程窗格中的jspwithcountingbeanbean.java,在源程序中增加以下黑体部分代码。

package samplejsp;


public class jspwithcountingbeanbean {

/**initialize variable here*/
private int mycount=0;

private string sample = "start value";
/**access sample property*/
public string getsample() {
return sample;
}

/**access sample property*/
public void setsample(string newvalue) {
if (newvalue!=null) {
sample = newvalue;
}
}

/**new method for counting number of hits*/
public int count() {
return ++mycount;
}

}

  双击在工程窗格中的jspwithcountingbean.jsp,在代吗中增加以下黑体部分代码。

<html>
<head>
<jsp:usebean id="jspwithcountingbeanid" scope="session"
class="samplejsp.jspwithcountingbeanbean" />
<jsp:setproperty name="jspwithcountingbeanid" property="*" />
<title>
jspwithcountingbean
</title>
</head>
<body>
<h1>
jbuilder generated jsp
</h1>
<form method="post">

enter new value: <input name="sample">




<input type="submit" name="submit" value="submit">
<input type="reset" value="reset">


value of bean property is: <jsp:getproperty name="jspwithcountingbeanid"
property="sample" />
<p>this page has been visited :<%= jspwithcountingbeanid.count() %> times.

</form>
</body>
</html>

  2. 运行jsp

   要运行jsp

   右击jsp文件

   选择 web run.

  如果没有错误,tomcat将启动,在内容面板中将出现web view与web view source 页,web view相当于一个浏览器,返回jsp运行的结果。如果运行成功将可以看到以下内容:

    jsp in web view

可以点击web view 页中的submit按钮,可以看到page counter在增加。tomcat所有的输出信息/日志都显示在message窗格中。

  3. 调试jsp

  要调试jsp,先设置一个断点,具体步骤如下:

  在jspwithcountingbean.js中的源文件窗格中,找到下面这一行:

   <p>this page has been visited :<%= jspwithcountingbeanid.count() %> times.
.

  点右键,在弹出菜单中选择toggle breakpoint。

  右击jsp文件,选择web debug即可调试。

  如何调试

  如上节所提,上图就是调试的界面。

  4. servlet

   开发环境的配置

   无需特别配置,jbuilder4已经为我们配好了开发环境。

   例操作步骤

    1. 生成一个servlet

     在jbuilder内创建一个新工程

     选择 file|close 关闭其他工程

     选择 file|new, 在object gallery.双击servlet图标

     这时将出现工程向导,将目录、工程名设为sampleservlet。

     点finish.

     这时出现servlet向导

     创建一个新的servlet:

     类名设为firstservlet。

     选中dopost()方法(通常doget()方法已被缺省选中了 )。

     点next.

     让shtml文件保持缺省设置,点next。

     点add按钮加入一个servlet parameter,这样将在shtml文件内生成一些这些参数的引用代码。请对应下表将参数设为下表的值:


name* username
type*string
descname of user
variable*username
defaulthey you

  点finish,这时工程内将有一个firstservlet.java文件和一个firstservlet.shtml 文件。servlet库也将自动载入工程的library。

  在firstservlet.java内加入以下代码,新再入的代码也被标为黑体。

package firstservlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class firstservlet extends httpservlet {
private static final string content_type = "text/html";

/**initialize global variables*/
int connections;
public void init(servletconfig config) throws servletexception {
super.init(config);
connections=0;
}

/**process the http get request*/
public void doget(httpservletrequest request, httpservletresponse
response) throws servletexception, ioexception {
response.setcontenttype(content_type);
printwriter out = response.getwriter();
out.println("<font color=/"green/">");
out.println("<p>the servlet has received a get. this is
the reply.</p>");
out.println("</font>");
}

/**process the http post request*/
public void dopost(httpservletrequest request, httpservletresponse
response) throws servletexception, ioexception {
file://name of user
string username = "hey you";
try {
username = request.getparameter("username");
}
catch(exception e) {
e.printstacktrace();
}
response.setcontenttype(content_type);
printwriter out = response.getwriter();
out.println("<html>");
out.println("<head><title>firstservlet
</title></head>");
out.println("<body>");
out.println("<p>the servlet has received a post.
this is the reply.</p>");
out.println("<p>thanks for visiting, ");
out.println(request.getparameter("username"));
out.println("<p>");
out.println("hello world - my first java servlets program!");
out.println("<p>you are visitor number ");
connections++;
out.println(integer.tostring(connections));
out.println("</body></html>");
}
}

  2. 编译

   要编译serlet,先到设置运行时属性。

   单击 project|project properties。选择run页,再选jsp/servlet 页。

   单击set按钮,浏览并选中创建的shtml文件。

   改变web server的属性,端口是web server运行时使用的端口,缺省的端口是8080,如果8080被占用的,jbuilder会自动选择一个空闲的端口。

   点ok。

   现在可以编译了

   右击firstservlet.shtml.

   点make。

  3. 运行

   与运行jsp类似,右击firstservlet.shtml,选中web run.

  4. 调试

   与运行jsp类似,右击firstservlet.shtml,选中web debug.

   如何调试

   已作说明。

扫描关注微信公众号