jbuilder2005+jboss-4.0.2rc1+j2sdk1.5+log4j Development Session Bean Ii. (4)

Source: Internet
Author: User
Tags date command line end http post tostring xmlns jboss log4j
Session
Author: Junsan Jin

Date: 2005-3-30

Version: 1.0

Mailbox: junsan21@126.com; Junnef21@sohu.com

Disclaimer: I reserve all the rights of this article.







Part Four: test session Bean



The previous three sections already complete the process of establishing a session bean. Here's how to build a test project.







To add a test class:



In the right tree menu select Com.inspiresky.trader Right 〉new〉package, the dialog box in the input box at the end of the add user, new Com.inspiresky.trader.user package.



Select the Com.inspiresky.trader.user right key 〉new〉class, create the Userinfo.java class, the code is as follows.



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



Userinfo.java



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



Package com.inspiresky.trader.user;







Import Javax.naming.InitialContext;



Import Trader. Userinfolocalhome;



Import Trader. userinfolocal;



Import javax.naming.NamingException;



Import java.rmi.RemoteException;



Import javax.ejb.CreateException;



Import Java.util.HashMap;



Import Java.util.Date;







/**



* <p>title: </p>



*



* <p>description: </p>



*



* <p>copyright:copyright (c) 2005</p>



*



* <p>company: </p>



*



* @author not attributable



* @version 1.0



*/



public class UserInfo {







Public UserInfo () {



}







Public HashMap GetUserInfo () {



HashMap map = new HashMap ();







try {



Userinfolocal local = (userinfolocal) findejb ();



Map = Local.showuserinfo ();



catch (CreateException ex) {



System.out.println ("Error 1");



catch (RemoteException ex) {



System.out.println ("Error 2");



catch (Namingexception ex) {



System.out.println ("Error 3");



}







return map;



}







Private Object FINDEJB () throws Namingexception, RemoteException,



createexception {



Javax.naming.Context context = new Javax.naming.InitialContext ();







Object obj = context.lookup ("userinfolocal");







Userinfolocalhome home = (userinfolocalhome) javax.rmi.PortableRemoteObject.narrow (obj,



Userinfolocalhome.class);







Userinfolocal local = (userinfolocal) home.create ("Jak", "a", New Date (). toString ());







return to local;



}



}







Add Customer Class:



The same method creates the class Test.java in the same package, and the code is as follows.



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



Test.java



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



Package com.inspiresky.trader.user;







Import javax.servlet.*;



Import javax.servlet.http.*;



Import java.io.*;



Import java.util.*;



Import Com.inspiresky.trader.user.UserInfo;







public class Test extends HttpServlet {



private static final String Content_Type = "text/html;" CHARSET=GBK ";



Private HASHMAP map;







Initialize Global Variables



public void Init () throws Servletexception {



UserInfo user = new UserInfo ();



Map = User.getuserinfo ();



}







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 ("


Out.println ("


Out.println ("<body bgcolor=\" #ffffff \ ">");



Out.println (Map.tostring ());



Out.println ("</body>");



Out.println ("


Out.close ();



}







Process the HTTP Post request



public void DoPost (HttpServletRequest request, httpservletresponse response) throws



Servletexception, IOException {



Doget (request, response);



}







Process the HTTP put request



public void DoPut (HttpServletRequest request, httpservletresponse response) throws



Servletexception, IOException {



}







Process the HTTP Delete request



public void Dodelete (HttpServletRequest request,



HttpServletResponse response) throws Servletexception,



IOException {



}







Clean up resources



public void Destroy () {



}



}







Modify Web.xml:



In the second part we have modified the Web. XML, the same method, we then add the following sections:



<servlet>



<servlet-name>test</servlet-name>



<servlet-class>com.inspiresky.trader.user.Test</servlet-class>



</servlet>



<servlet-mapping>



<servlet-name>test</servlet-name>



<url-pattern>/test</url-pattern>



</servlet-mapping>







The full contents of the modified Web.xml file are:



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



Xml



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



<?xml version= "1.0" encoding= "UTF-8"?>



<web-app xmlns= "Http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version= "2.4" >



<display-name>WebTrader</display-name>



<servlet>



<servlet-name>log4j-init</servlet-name>



<servlet-class>com.inspiresky.trader.init.Log4jInit</servlet-class>



<init-param>



<param-name>log4j-init-file</param-name>



<param-value>WEB-INF/log4j.properties</param-value>



</init-param>



<load-on-startup>1</load-on-startup>



</servlet>



<servlet>



<servlet-name>test</servlet-name>



<servlet-class>com.inspiresky.trader.user.Test</servlet-class>



</servlet>



<servlet-mapping>



<servlet-name>test</servlet-name>



<url-pattern>/test</url-pattern>



</servlet-mapping>



</web-app>







Create the Java EE application subordinate package:



All the development work for the project has been done before, and for the sake of deployment, we are now creating an application deployment package.



Select menu: New〉enterprise〉application Module (EAR) 〉ok〉create empty application module〉next〉 input name etrader〉next〉 Select Trader 〉next〉 Choose Webtrader〉finish.







Compile Project:



Right-click Etader〉rebuilder in the Tree menu. The compilation succeeds without the error message.







Start JBoss:



Run D:\jboss-4.0.2RC1\bin\run.bat, start JBoss. Because we set paths in path, you can also run the Run command directly under the command line.







To deploy the application:



Right-click etader〉deploy option for "Etrader.ear" 〉deploy in the tree menu.



The following two commands are used when redeploying and uninstalling applications respectively.







To run the program:



Open IE Browser, enter in the address bar



Http://localhost:8080/WebTrader/test



At this point, you will see



{Username=jak, logintime=wed Mar 11:26:18 CST, userage=12}



Indicates that our program is running properly.







OK, a simple session bean application is developed to this end. There is less theory here, just hope to use examples to give you some inspiration, to better understand and apply some of the theories we have seen about Java, better familiar with the tools we use.



In the written haste, it is inevitable that there are mistakes to be overlooked.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.