Thursday, August 19, 2010

How to test servlet using Telnet

Essentially what I need to do is just make an HTTP Call using Telnet.

$> telnet 127.0.0.1 9086
Ok, I'm connected, and now what??
I know which servlet I want to test, I know which parameters is expecting, then "let's do it":

Which method should I use? POST or GET?? let's use POST (due to well known limitations with GET)

This is my URL:
/MyWebApp/proxyServlet?operation=doSomethingNice&consumerName=myActiveMQConsumers&server=192.168.5.122:9080&targetServlet=/MyOtherWebApp/utilsServlet
I'll finish my command with the HTTP version I want to use: HTTP/1.0 (if I use 1.1 it'll keep you connected)

And finally let's put all together:

Important: Once you have copied this full line in your telnet body, please type ENTER twice.

How do I know if this works? You should see this on your telnet console:
HTTP/1.0 200 OK
Content-Type: text/xml;charset=ISO-8859-1
Cache-Control: no-cache
Content-Language: en-US
Content-Length: 21
Date: Thu, 19 Aug 2010 18:38:45 GMT
Server: WebSphere Application Server/6.1

true

Connection to host lost.

Tuesday, August 17, 2010

Small Proxy App based on Apache Mina 2.0.0-RC1

What I'm going to do is develop a small app which basically is going to broadcast a message received to another host in a defined port.
My small app is just a standalone app based on Apache Mina, is going to be listening on "X" port and it's going to broadcast message received to another app which is a blackbox. So simple, Isn't it?


What do you need?
  • mina-filter-compression-2.0.0-RC1.jar
  • mina-integration-xbean-2.0.0-RC1.jar
  • slf4j-log4j12-1.5.0.jar
  • mina-integration-beans-2.0.0-RC1.jar
  • jzlib-1.0.7.jar
  • jaxen-1.1.1.jar
  • mina-transport-apr-2.0.0-RC1.jar
  • log4j-1.2.15.jar
  • slf4j-api-1.5.0.jar
  • mina-core-2.0.0-RC1.jar
Or simple, you can get the full distribution and get them all in one single file.
This jar is just a luxury, but it's necesary, because I don't want to reinvent the wheel: commons-lang-2.3.jar

Well, time to work, the first thing you should code is the main class, my ProxyServer and 2 IoHandlers (ProxyHandlerAdapter & ClientHandlerAdapter).


And this is my code







How can I test it??? Just by create a client using java.net.Socket or just by using Telnet to that particular port

I didn't include other classes because they're not important

Log4J tips

Add thread name to my log file; sometimes we want to know which thread is being executed, well, try this:
ConversionPattern=%d [%5p] [%t] %c{1}.%m%n

That's right just by adding "%t", you can see which thread is doing what.


Friday, August 13, 2010

Spring MVC - Binding VO to a Form

Problema:
I have a VO and I wanted to populate it on my JSP and on my controller just process the VO already populated.

Solution:

On my POST method I'll handle my VO already populated.
This entry will do 50% of the job @ModelAttribute(value="LoggerVO") LoggerVO loggerVO
On my JSP I'll map using spring's tag library
By doing this modelAttribute="LoggerVO" commandName="LoggerVO"
I'm matching what I have on my form and what I have on my Controller
I hope this will help you!
Salu2

Wednesday, August 11, 2010

Cross Domain issue - Ajax

Who say yes???
I was working in a GUI for controlling components in different servers from one server - stopping, starting, purging queues & consumers; finally beans
The firSt thing it came to my mind was: "let's do it with AJAX to avoid multiple calls and decrease the traffic!!!"
Which AJAX framework should I choose??? Let's learn jQuery, because that one has a bunch of cool features which will be really helpfull to develop my GUI (check this site: flowplayer.org they have a bunch of demos which are really easy to follow and all based on jQuery)
My first issue was learnign that Ajax call, but after a few hours I could do my first ajax call using jQuery (I have a servlet which returns XML or JSON)

Now what??? If I'm accessing the servlet in the same context - domain, everything works fine, but... If I try to access my servlet but in another server

Tons tuve que eccharle coco
My solution was to create a ProxyServlet which basically will take my request and pass it to the remote server & servlet using an HttpClient and in the same way, it will get the response coming from the remote servlet, in other words, my ProxyServlet (in the same domain - context) will be the only one talking to my Ajax call, eventhough ProxyServlet will be talking to the remote server & servlet. Then I did it again  

This is my code:

This is my main servlet (the one which returns XML or JSON using json-simple project)

And this now, these are portions of my jQuery code:


You can get the additional JavaScript libraries from: JSON2 and my version of jQuery Tools

Monday, August 9, 2010

Adding Spring Web MVC part 2

Now I'll show you a couple of examples how it looks like a Controller & a JSP using Spring MVC.

From my personal perspective Defining a controller gives you a lot of power, because you can define a method to handle RequestMethod.GET, just to show a Form, and another method to handle the RequestMethod.POST to handle the "submit", cool isn't it?

By adding @Controller you already told Spring, "hey this dude is going to be a Controller".

@RequestMapping(value="/configuration/*") means: "If my URL is WebContext/configuration/ this Controller is going to handle the Request"

Then on each method I can define my path below /configuration/ @RequestMapping(value = "showConsumersConfiguration.do"), something like this WebContext/configuration/showConsumersConfiguration.do

I almost forgot @Autowired which is for inject implementation of that Bean


If you want to handle an object like a form (bind an object to a form), check this out

Important, include these tag libraries!

By using this attribute modelAttribute="UserVO" of form:form tag, I'm binding this line of my controller: modelMap.put("UserVO", userVO); with my form

If you see carefully, each attribute of my VO, has a reference on my JSP, at least as hidden field, but all of them are there

Now my form has been binded to my VO @ModelAttribute UserVO paramUserVO

Now you're ready to play around with this thing!!!

Always go to the official documentation to get more details!!!

Adding Spring Web MVC

I'll try to be brief.

Basically, you need to add a new Servlet on you web.xml, a configuration file (let's call it webApplicationContext.xml) and some Annotations.

This is my example:

My web.xml

The framework will, on initialization of a DispatcherServlet, look for a file named [servlet-name]-servlet.xml (webApplicationContext-servlet.xml) in WEB-INF by default, but if you provide parameter "contextConfigLocation" you can define a location and name for your configuration file.

My webApplicationContext.xml

Which basically is divided in 3 sections:

  • We're going to use configuration based onn annotations
  • Location of my Controllers
  • And where are located my JSP's

In my next entry I'll cover how it looks like a Controller & JSP.