JBoss-IDE 1.2.2 教程 3-4

JBoss-IDE 1.2.2 教程 3

Servlet 和 Web-App

只有 EJB 還是不足夠的, 我們要寫一個 Servlet 去使用 EJB 計算斐波納契數列.

 

在項目中新增一個 class, package 輸入 ‘tutorial.web’ 和名稱輸入為 ‘ComputeServlet’. 按下 ‘Browse…’ 在 superclass 新增 ‘HTTPServlet’. 選 ‘Constructors from superclass’ 和 ‘Inherited abstract methods’.

 

‘Finish’ 後應可看到以下畫面.

 

開啟 ‘ComputeServlet.java’ 檔案. 右擊右手方視窗選 ‘Source’ -> ‘Override/Implement Methods…’. ‘init’ ‘doPost’ 方法再按 ‘OK’, 這兩個方法會自動生成.

 

接著加上兩個 private 成員:

private FiboHome home;

private String value;

 

定義 ‘init’ 方法如下, 這是將 EJB Home 接口初始化:

public void init() throws ServletException {

try {

Context context = new InitialContext();

value = (String) context.lookup("java:/comp/env/Title");

Object ref = context.lookup("java:/comp/env/ejb/Fibo");

home = (FiboHome) PortableRemoteObject.narrow(ref, FiboHome.class);

} catch (Exception e) {

throw new ServletException("Lookup of java:/comp/env/ failed");

}

}

 

定義 ‘doPost’ 方法如下, 這是得到一個 ‘limit’ 要求後, 新增一個 EJB 實例計算後將結果輸出致 HTML:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<html><head><title>");

out.println(value);

out.println("</title></head>");

out.println("<body>");

out.println("<h1>");

out.println(value);

out.println("</h1>");

 

try {

Fibo bean = home.create();

int limit = 0;

String value = request.getParameter("limit");

 

if (value != null) {

try {

limit = Integer.parseInt(value);

} catch (Exception e) {}

}

 

double[] result = bean.compute(limit);

bean.remove();

out.println("<p>");

out.print("The ");

out.print(limit);

out.print(" first Fibonacci numbers ");

 

for (int i = 0; i < result.length; i++) {

out.println("<br>");

out.println(i);

out.println(" : ");

out.println(result[i]);

}

 

out.println("</p>");

}

catch (Exception e) {

out.println(e.getMessage());

e.printStackTrace(out);

}

finally {

out.println("</body></html>");

out.close();

}

}

 

下一步是插入 Servlet XDoclet javadoc 相關的標籤. javadoc 裏寫入 ‘@web’ 和按下 CTRL+Space. 應看到自動完成功能. 而其他的屬性亦一樣做法, 代碼如下:

*

* @web.servlet name = "ComputeServlet"

* display-name = "Computation Servlet"

* description = "Servlet that compute Fibonacci suite"

*

* @web.servlet-mapping url-pattern = "/Compute"

*

* @web.env-entry name = "Title"

* type = "java.lang.String"

* value = "Fibonacci computation"

* description = "Example of Env Entry"

*

* @web.ejb-ref name = "ejb/Fibo"

* type = "Session"

* home = "tutorial.interfaces.FiboHome"

* remote = "tutorial.interfaces.Fibo"

* description = "Reference to the Fibo EJB"

*

* @jboss.ejb-ref-jndi ref-name = "ejb/Fibo"

* jndi-name = "ejb/tutorial/Fibo"

*/

public class ComputeServlet extends HttpServlet {

JBoss-IDE 1.2.2 教程 4:

 

'ComputeServlet' 的完整的代碼如下:

package tutorial.web;

import java.io.IOException;

import java.io.PrintWriter;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.rmi.PortableRemoteObject;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import tutorial.interfaces.Fibo;

import tutorial.interfaces.FiboHome;

 

/**

* @author John Doe

*

* @web.servlet name = "ComputeServlet"

* display-name = "Computation Servlet"

* description = "Servlet that compute Fibonacci suite"

*

* @web.servlet-mapping url-pattern = "/Compute"

*

* @web.env-entry name = "Title"

* type = "java.lang.String"

* value = "Fibonacci computation"

* description = "Example of Env Entry"

*

* @web.ejb-ref name = "ejb/Fibo"

* type = "Session"

* home = "tutorial.interfaces.FiboHome"

* remote = "tutorial.interfaces.Fibo"

* description = "Reference to the Fibo EJB"

*

* @jboss.ejb-ref-jndi ref-name = "ejb/Fibo"

* jndi-name = "ejb/tutorial/Fibo"

*/

public class ComputeServlet extends HttpServlet {

private FiboHome home;

private String value;

 

public ComputeServlet() {

super();

}

       

public void init() throws ServletException {

try {

Context context = new InitialContext();

value = (String) context.lookup("java:/comp/env/Title");

Object ref = context.lookup("java:/comp/env/ejb/Fibo");

home = (FiboHome) PortableRemoteObject.narrow(ref, FiboHome.class);

} catch (Exception e) {

throw new ServletException("Lookup of java:/comp/env/ failed");

}

}

       

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

 

out.println("<html><head><title>");

out.println(value);

out.println("</title></head>");

out.println("<body>");

out.println("<h1>");

out.println(value);

out.println("</h1>");

 

try {

Fibo bean = home.create();

int limit = 0;

String value = request.getParameter("limit");

 

if (value != null) {

try {

limit = Integer.parseInt(value);

}

catch (Exception e) {

}

}

       

double[] result = bean.compute(limit);

bean.remove();

out.println("<p>");

out.print("The ");

out.print(limit);

out.print(" first Fibonacci numbers ");

       

for (int i = 0; i < result.length; i++) {

out.println("<br>");

out.println(i);

out.println(" : ");

out.println(result[i]);

}

       

out.println("</p>");

} catch (Exception e) {

out.println(e.getMessage());

e.printStackTrace(out);

} finally {

out.println("</body></html>");

out.close();

}

}

}

 

生成 Servlet 相關檔案:

要生成 Web 的配置文檔, 我們首先要設置一些 XDoclet 的設定. 就好像 EJB 一樣, 讓我們來定義 Web 自動生成的設定.

 

右擊選目選 ‘Properties’ -> ‘XDoclet configurations’, 在左上的視窗右擊選 ‘Add’, 填 ‘Web’ 然後按 ‘OK’, 這樣就成功新增一個 XDoclet ‘Web’ 的生成設定.


 

接著選 ‘Web’, 在左下方視窗右擊選 ‘Add Doclet’ -> ‘webdoclet’, 再按 ‘OK’. 左手方的視窗的 ‘webdoclet’ 設置中設定 ‘destDir’ 填入 ‘src/WEB-INF’. 現在 XDoclet ‘webdoclet’ 設定會在 ‘src/WEB-INF’ 資料夾中產生 Web 的檔案.

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值