request.getSession (false) always returning a session

Recently I found that request.getSession (false) was *always* returning a session instance even when it is not created earlier by my application. After spending 2-3 hours on it, I figured out the cause. Read on …….

I am working on adding a rather unique jobs functionality for chahiye.info which required an authentication mechanism. For this purpose, I wanted to add a check at various pages to ensure that if a session did not exist a request to these pages would be re-directed to loign page. This was done using the following code

HttpSession session = request.getSession (false);
if (session == null)
{
// forward request to login page
// return;
}

However to my surprise this api was always giving me a session instance even when I had not explicitly created one (after the login request). I searched my entire code base & confirmed that I was not creating a session anywhere. Looked up the JavaDoc for this method in class javax.servlet.HttpServletRequest, which clearly stated that if the supplied boolean is false and if the request has no valid HttpSession, this method would return null.

After spending some more time, I realized the problem. Basically if the application is using a JSP, the container (in my case Tomcat 5.5) by default ends up creating a session. In my case, the user’s landing page itself was a JSP as a result the session gets created at that time itself. To avoid this, simply add the following directive at your JSP, which tells the JSP compiler not to use session variable in the compiled class.

< % @page session="false" % >

Note – You will have to get rid of extra spaces between “<“, “%” & “@” symbols.

Have you also had a similar situation and figured out another cause for this? If yes, please share your experience here.

Advertisements

Send/Receive SMS using Java

In the last couple of months for some reason or the other I have faced a requirement to send/receive SMS for the applications that I have been working on. This post talks about how I achieved the same.

There are plenty of SMS vendors that provide simple APIs (typically HTTP based) that allow one to send an SMS. Once you register with any vendor (typically after paying), then all it takes to send an SMS is to invoke a URL programmatically. I have been using http://sms.sms2india.info for my last project and have been pretty happy with their service. The service gives me 500 credits (500 SMS) for Rs 300/- (60 paisa an SMS – which I’m okay to spend). To know more about the API format, visit their website and click on Developer API Link.

If for any reason, you don’t like this option; There are other ways of sending an SMS. I had earlier written a simple application that could interact with a handset connected with the computer over serial cable through AT Commands and instruct it to send an SMS. It could also read the inbox in similar manner. Other options include professional services that provide interface with an SMSC over simpler APIs. Check out this product from Simple Wire. Though I haven’t really used it, but it looks fairly neat.

I’m pretty sure there are plenty of other options that exist and might probably be better for one reason or the other. If you know of some, please feel free to add a comment.

To receive an SMS, you would need to set up a short-code. There are plenty of vendors that allow one to do this. Searching Google with keywords like “SMS Shortcode India” for example will expose you a list of vendors. Once you get a short code registered for yourself (again by paying some amount), for a particular keyword, your vendor would allow you to configure handling of this keyword. If your application requirements are static in nature, it will provide tools to send you notifications (email or sms) on every sms that comes to your short-code. If your needs are dynamic, then you can set up the key-word so that it hits a URL (of your application) passing on the incoming sms along with the sender’s mobile number and whatever you return (in plain text) is sent back to the sender.

Adding a WYSIWYG Editor on a HTML Text Area

One of the applications that I’m currently working on required me to write a small Java program that allowed users to send email from a web page. [Yeah – Nothing so great about it but sending emails is not what this post is about]. The page allowed user with basic email facilities and also supported Email Attachments (Thanks to Apache commons File upload). It allowed users to compose text emails through a regular HTML Text Area.

On Completing the application, one of the first enhancement that came my way was to enable rich text & spell checker in the body of the email. This initially looked quite a challenge to me as I had no clue how to go about building this. However After spending some time with my dear friend – Google, I found a couple of Tools that could help me out. I ended up using FCKEditor, which enabled writing/editing rich text through Java Script.

The product was not very costly (license details can be found here) & was fairly easy to set up. After a couple of hours, I managed to set it up properly and the HTML text area in my form got modified to that shown below (click to en-large). Notice the tool bar on top of the email body text area. One of the greatest feature of this editor was that it supported copy/paste from MS Office applications (Word, Outlook) without breaking down the formatting.

The reset of this post provides brief instructions on how to set this up. For more details, please refer to their online documentation here.

1) Download FCK Editor from here [I used version 2.6].

2) Unzip the downloaded compressed file into your web-app directory. With Tomcat for example, the un-compressed folder (fckeditor by default) should be placed into %TOMCAT_HOME%\webapps\%YOUR_WEBAPP_NAME% directory.

3) Point your web browser to the following linke in order to test the installation. If correctly installed you would see a text area with a tool bar similar to the one shown above.

http://yourip:port/your-webapp/fckeditor/_samples/default.html

4) Add the following lines to your web page in the section

window.onload = function()

{

var oFCKeditor = new FCKeditor( ‘my_text_area’, ‘90%’, ‘400’, ‘Default’, ” ) ;

oFCKeditor.BasePath = “fckeditor/” ;

oFCKeditor.ReplaceTextarea() ;

}

window.onload = function()

{

var oFCKeditor = new FCKeditor( ‘my_text_area’, ‘90%’, ‘400’, ‘Default’, ” ) ;

oFCKeditor.BasePath = “fckeditor/” ;

oFCKeditor.ReplaceTextarea() ;

}

Note(s)

1. This example adds rich text toolbar to a text area with the name ‘my_text_area’. You might want to replace this with the actual name that is being used in your HTML form.

2. The Base Path refers to the directory where the editor is installed. It defaults to fckeditor which is the default directory in which fckeditor is unzipped.

3. For more details, please refer online documentation here.

Web Load Simulator

As promised in my earlier post, here’s the description of my first completed project

The project’s goal were

a) To find the maximum number of concurrent users a web portal will be able to handle.
b) If the figure found above turned out to be less than the estimated load then also work out ways to resolve the situation.

I wrote a simple JAVA application to handle part a). Details for the same follow –

The application allowed setting up any number of page flows that users would hit [A page flow essentially defined a list of GET/POST requests to be sent to the server along with delays in between] These page-flows were defined with the help of their marketing people who were aware of typical user actions (based on analytics reports). Once the page flows are defined, the program allowed setting up the number of users that would be going through a page flow. This allowed them to simulate the actual usage scenario of their portal.

On starting the program, a thread would be created for each user and a configured page flow assigned to it. This thread on starting would hit send the HTTP GET/POST request, from the received HTML response would parse the resources (images, java scripts, style sheets etc) and then send GET requests for them – just the way a web browser would behave.

The application captured the response received for each URL for each thread and later at the end of the program generate a report that would contain the response time for each URL hit and a higher level summary.