eclipse+jboss xdoclet ejb-sevlet综合应用(三)

5 servlet

package com.ibeidou.webgui.servlets;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.rmi.PortableRemoteObject;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.ibeidou.test01.ejbsource.HelloWorld1;

import com.ibeidou.test01.ejbsource.HelloWorld1Home;

/**

 * @web.servlet name="HelloWorld1Servlet" display-name="Hello Servlet"

 *              load-on-startup="1"

 * @web.servlet-init-param name="hello" value="hello this is xdoclet servlet"

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

 */

public class HelloWorld1Servlet extends HttpServlet {

public void init(ServletConfig config) throws ServletException {

super.init(config);

}

protected void doGet(HttpServletRequest request,

HttpServletResponse response) throws ServletException,

java.io.IOException {

// ServletConfig config = this.getServletConfig();

// String h = config.getInitParameter("hello");

try {

response.setContentType("text/html;charset=GBK");

java.io.PrintWriter out = response.getWriter();

out.println("<html><head><title>Welcome javamxj Blog!</title></head>");

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

String name = request.getParameter("name");

String h = this.sayHello(name);

out.println(" Hello:  " + h);

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

out.close();

} catch (Exception e) {

throw new ServletException(e);

}

}

private String sayHello(String name){

try{

Context ctx = new InitialContext();

Object objref = ctx.lookup("ejb/test01/helloworld1");

HelloWorld1Home helloHome = (HelloWorld1Home)PortableRemoteObject.narrow(

objref, HelloWorld1Home.class);

HelloWorld1 hello = (HelloWorld1)helloHome.create();

return hello.sayHello(name);

}catch(Exception e){

e.printStackTrace();

}

return null;

}

}

Build.xml

<project name="filtering" default="deploy" basedir=".">

<description>一个简单的XDoclet实例</description>

<property file="../configuration/conf/build.properties" />

<property name="src.dir" value="src"></property>

<property name="dist.dir" value="output"></property>

<!-- define classpath with jars needed -->

<path id="web.classpath">

<!-- customized jars needed -->

<pathelement location="${deploy_temp_dir}/${app.name}.jar"/>

<!-- system jars needed -->

<fileset dir="${jboss.home}/server/default/lib">

<include name="*.jar"/>

</fileset>

</path>

<path id="xdoclet.classpath">

<fileset dir="${xdoclet.home}">

<include name="**/*.jar" />

</fileset>

<!-- reference web.classpath, when generating web.xml the servlet api is needed -->

<path refid="web.classpath" />

</path>

<target name="init">

<mkdir dir="${dist.dir}" />

<mkdir dir="${dist.dir}/WEB-INF" />

<mkdir dir="${dist.dir}/WEB-INF/classes" />

</target>

<!-- webdoclet task only generate web.xml -->

<target name="webdoclet" depends="init">

<taskdef name="webdoclet" classpathref="xdoclet.classpath" classname="xdoclet.modules.web.WebDocletTask" />

<webdoclet destDir="${dist.dir}/WEB-INF" force="${xdoclet.force}">

<deploymentdescriptor Servletspec="2.4" xmlencoding="GBK" />

<fileset dir="${src.dir}" includes="**/*Servlet.java" />

</webdoclet>

</target>

<target name="deploy" depends="webdoclet">

<javac srcdir="${src.dir}" destdir="${dist.dir}/WEB-INF/classes">

<classpath refid="web.classpath" />

</javac>

<jar destfile="${jboss.home}/server/default/deploy/${app.name}.war" basedir="${dist.dir}" />

</target>

<target name="clean">

<delete dir="${dist.dir}"></delete>

</target>

</project>

6 测试运行

Dos下进入xdoclet_ejb01目录,输入ant命令,完成编译部署等一系列工作

Dos下再进入xdoclet_ejb01test目录,输入ant。

启动jboss服务器,打开浏览器

http://localhost:8080/xdoclet_ejb01/HelloWorld1?name=sam

即可输出

Hello: sam says hello world...

你可能感兴趣的:(eclipse+jboss xdoclet ejb-sevlet综合应用(三))