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();
}
Tuesday, July 29, 2008
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?
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?
Labels:
AJAX,
Java,
Javascript,
object from string,
servlet
Tuesday, May 27, 2008
SQL date manipulating in Oracle
Today I wrote a script that would take the values I had from my Calendar table, which contained all months with their start date and end date for the year of 2008, and insert them back for the year of 2007. My first solution didn't work so well and threw an error. I came to learn that the TO_YMINTERVAL function doesn't work because it doesn't handle leap year.
The function that did the job right was ADD_MONTH.
Here goes my script:
INSERT INTO calendar
(calendar_id,
month,
full_month,
begin_date,
end_date)
SELECT calendar_s1.nextval,
month,
full_month,
add_months(begin_date,-12),
add_months(end_date,-12)
FROM calendar;
Try that out and let me know if you have other simpler solutions for a similar problem.
The function that did the job right was ADD_MONTH.
Here goes my script:
INSERT INTO calendar
(calendar_id,
month,
full_month,
begin_date,
end_date)
SELECT calendar_s1.nextval,
month,
full_month,
add_months(begin_date,-12),
add_months(end_date,-12)
FROM calendar;
Try that out and let me know if you have other simpler solutions for a similar problem.
Labels:
ADD_MONTH,
date,
manipulation,
oracle,
SQL,
TO_YMINTERVAL
Wednesday, May 14, 2008
Introduction
Dear readers,
The purpose of this blog is to provide information, definitions, and ways of doing stuff with technology, mostly with programming, database, setting up different types of server - such as normal proxy servers, transparent proxy servers, web servers, etc...
The main purpose of this blog is really to keep my notes online so I can reference to them later on, if I go through the same problem.
I also want to go over some basic programming and database concepts to help beginners dive in programming without fear.
Let me know if you have any questions.
A brief introduction about me.
My name is Mathew Pretel. I work at Corda Technologies currently as a developer while going to school at BYU-Idaho. My major is Computer Information Technology with emphasis in development.
I'm married and we have a 6 months old little girl.
The purpose of this blog is to provide information, definitions, and ways of doing stuff with technology, mostly with programming, database, setting up different types of server - such as normal proxy servers, transparent proxy servers, web servers, etc...
The main purpose of this blog is really to keep my notes online so I can reference to them later on, if I go through the same problem.
I also want to go over some basic programming and database concepts to help beginners dive in programming without fear.
Let me know if you have any questions.
A brief introduction about me.
My name is Mathew Pretel. I work at Corda Technologies currently as a developer while going to school at BYU-Idaho. My major is Computer Information Technology with emphasis in development.
I'm married and we have a 6 months old little girl.
Subscribe to:
Posts (Atom)
