Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Friday, December 5, 2008

Configure JBoss for Oracle

  1. Oracle and JBoss use the same port by default, 8080. If you are using Oracle and JBoss on the same server, modify the http port for Oracle to something other than 8080 (i.e, 8081). Skip this step if you are running Oracle on a different server.
  2. Login to Oracle using SQLPLUS as the SYSTEM user and enter the following script command:


    BEGIN
    dbms_xdb.sethttpport(’8081′);
    END;
    /

    Locate the online_help file in the Oracle \server folder, right click, select properties, and modify the url to:

    http://127.0.0.1:8081/apex/wwv_flow_help.show_help?p_flow_id=4500&p_step_id=1000

    Locate the postDBCreation file in the Oracle server\config\log folder, open it and modify the following line:

    URL=http://127.0.0.1:8081/htmldb/wwv_flow_help.show_help

  3. JBoss needs to know where the Oracle jdbc driver classes are located. This can be done by either, copying the Oracle jdbc archive file to the server default lib folder. Search and locate the ojdbc14.jar file in the Oracle \jdbc\lib folder. Copy the ojdbc14.ja file to the JBoss \server\default\lib folder.
  4. Define a datasource in JBOSS to access a specific Oracle database instance. Copy the /docs/examples/jca/oracle-ds.xml file from the jboss directory to the /server/default/deploy folder in JBoss. Open a text editor (e.g., WordPad or xCode) and modify the file as follows.






  5. OracleDS
    jdbc:oracle:thin:@localhost:1521:xe
    oracle.jdbc.driver.OracleDriver
    homedirectbank
    bank

    Oracle9i



  6. Modify the standardjbosscmp-jdbc.xml configuration file in the JBoss /server/default/conf folder. Add the following and sub-tags to the tag. These tags map the datasource, OracleDS, to the data mapping, Oracle9i, defined further down in this file. The data mapping tag defines how the specific Oracle database types map to the corresponding Java data types.







  7. java:/OracleDS




Tuesday, July 29, 2008

Sending a java object over HTTP protocol

As you may know HTTP communication is based on strings that are sent back and forth, and if you want to send an object across the wire over HTTP just object serialization isn't enough. You will have to convert your object into an XML string, and stream it in a ByteArrayOutputStream.
Here goes a snippet of my client code:

MethodInvokerBean miBean = new MethodInvokerBean(); //Instantiate your java bean

ByteArrayOutputStream buffer = new ByteArrayOutputStream();

XMLEncoder encoder = new XMLEncoder(buffer); //Assign ByteArrayOutputStream object to your XMLEncoder object

encoder.writeObject(miBean); //Write object on XMLEncoder object
encoder.close(); //close stream

String url = "http://192.168.1.25/BeanDecoder"; //Servlet's URL

//The classes and methods used in the block below to establish connection with the servlet and pass my object as a parameter come from the HTTP apache client library called commons-httpclient

PostMethod post = new PostMethod(url);
NameValuePair[] nvp = new NameValuePair[1];
nvp[0] = new NameValuePair("bean",buffer.toString());
post.setRequestBody(nvp);
HttpClient httpClient = new HttpClient();

try
{
int result = httpClient.executeMethod(post);
System.out.println(post.getResponseBodyAsString());
System.out.println("HTTP Status: " + result);
}
...


Let's see the servlet's code now:

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
String beanXML= req.getParameter("bean");
ByteArrayInputStream bais = new ByteArrayInputStream(beanXML.getBytes());
XMLDecoder decoder = new XMLDecoder(bais);
MethodInvokerBean miBean = null;
miBean = (MethodInvokerBean)decoder.readObject();
}

Thursday, June 19, 2008

Creating an object out of a string

Something that I had to do in one of my projects was send a HTTP request from javascript to a Servlet and get some information back. But, what's better to hold data than an Object?
So I decided to make the servlet send an object back as a response to that javascript request. Here is what I did.

Javascript sends AJAX request
Servlet receives request
Servlet sends an object as a string (eg: {property1: "hello", property2: "World", property3: "!"})
Javascript reads the response and interprets it as a string

In order to turn that encapsulated object in a string to a real javascript object you'll need to use the eval() function and append parenthesis to both sides of the string.

//Since this entry is not to teach you how to do AJAX requests I'm just going to assign a string to a variable and call it "ajaxRequest"
var ajaxResponse = "{property1: "hello", property2: "World", property3: "!"}";

//Notice the parethesis before and after the string that has the object
var myObject = eval( "("+ajaxResponse+")");

alert(myObject.property1 + " " + myObject.property2 + " " + myObject.property3);

Pretty cool uh?