Friday, May 30, 2008

Session Death

This is one of the most not obvious trigger in Web components. Because protocol doesn't demand to have continuous connection between the client making request and server keep on serving those request. And so most of the application contains a "Log out" button. True, on some event we can force session to die and here we are doing that on a button click. Time out value can be controlled in 3 ways:

- Global default - App Server has its own session timeout depending on the length of the session. Can't do anything with this.
- Webapps default - Here is the time to play with DD file. We can change the web.xml file to define the timeout and here is the small change:



Value here(40) goes in minute in session-timeout tag. But the most important is individual session setting

- Individual session setting: For this, we can write one line servlet code :

HttpSession.setMaxInactiveInterval(2400); // here time is in seconds.

Interesting part is defining 0 or negative in webapps default, leads to a never expired session whereas 0 means Immediate expire and negative means never, in Individual session setting. In HttpSession API, we can always kill the session by calling invalidate() method. So, on log-out button, just call this simple code.

In the next blog, I will try a simple code to see the session life cycle.

Wednesday, May 28, 2008

Error : log4j:WARN Please initialize the log4j system properly

When you compile and run your JSP and encounter an error as such:

log4j:WARN No appenders could be found for logger(org.apache.struts.util.PropertyMessageResources).
log4j:WARN Please initialize the log4j system properly.

Nothing to worry about, simply add the log4j.jar file in your WEB-INF/lib folder.
At times, your lib folder might be missing in your WEB-INF folder.

Download Log4J.jar file from http://logging.apache.org/log4j/1.2/download.html

How does a client and a server interacts?

Client is an end user and server is a computer program that provides services to client or user.
In order to interact in day to day life, we need a commom language.
In a similar way, a client and server interact using HTTP(Hyper Text Transfer Protocol).

HTTP is a Protocol(a standard way of communicating over a network), which runs above TCP/IP.

TCP/IP is responsible for routing your message properly from source to destination.

Now here the source and destination would be client/server.

Now a client sends request to server on HTTP request and the server responds to it on HTTP response.


What happens on entering a URL in your browser and click submit?

A HTTP request is being sent to web server, which is designed to handle HTTP request, to retrieve the particular web page.

Web-server responds with HTTP response containing the required web page.
HTTP request stream contains method name like GET,POST,etc, the page to access and form paraneters.

GET is mostly used to request for webpage, whereas POST in case of submitting form details(which need to be hidden).


Lets see the anatomy of GET,
Enter http://www.sun.com/ in your web browser, the generated HTTP request header would be as follows:


GET / HTTP/1.1Host: www.sun.com

Connection: closeUser-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)Accept-Encoding: gzipAccept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7Cache-Control: noAccept-Language: de,en;q=0.7,en-us;q=0.3


Here GET refer to request for a page.Host is your requested URLUser-Agent is your web browser detailsAccept-encoding refers to kind of Encoding your browser supportsAccept-Language refers to kind of language your brower supports

The server responds through HTTP response for the requested web page.

HTTP response header details as follows:


HTTP Status Code: HTTP/1.1 200 OK

Server: Sun-Java-System-Web-Server/6.1

Date: Tue, 27 May 2008 09:07:10 GMT

Content-type: text/html;charset=UTF-8 P3p: policyref="http://www.sun.com/p3p/Sun_P3P_Policy.xml", CP="CAO DSP COR CUR ADMa DEVa TAIa PSAa PSDa CONi TELi OUR SAMi PUBi IND PHY ONL PUR COM NAV INT DEM"

X-powered-by: Servlet/2.4,JSP/2.0

Connection: close

Here HTTP Status code specifies version type of HTTP.Content type specifies the type of data format being sent from server.


Now in order to see all Request and Response Header details. Visit http://web-sniffer.net/
Enter the requested URL and see the header level details.

For URL, https://69.46.17.169/servlet/redirect.srv/sruj/saorsbzq/snop/p1/"


Generated HTTP request line would be as follows:
GET /select/getDetails.jsp?name="hero"&dob="17061986" HTTP/1.1

so here,
HTTP method --> GET

The page to access--> /select/getDetails.jsp
Form paraneters--> name="hero"&dob="17061986"

Now for POST,
POST is having a message body.The form parameter mentioned above goes into a message body.
In GET, the data has been encoded in URL, whereas in POST, it has been encoded in message body.
Sometimes we need to hide the parameter passed to server due to security reasons. So make use of POST there.

GET is idempotent, means returning same webpage on each request.
GET is used for retrieving data and POST is used for storing, updation,etc of data.

Sunday, May 25, 2008

Simple Application - Checks the machine status(Lab use)

So, here goes the first post. Not anything that deals with the concept of Web Component but the first place where I have used JSP which initially was written in CGI and you can't believe JSP is so fast than CGI. Here goes the scenario:

If you are working in a product oriented company, you must be using lot of machines including your PC. There is a chance that there is a lab which is maintaining your all types of Computer... Windows, Solaris, Linux and so on. Most of the time(for me its most of the time :( ), when some machines are down(and that was the time, you want to work on those machines :D). Now knowing the machine is down or not is itself a tedious job and you keep on pinging all machine to check this. Why not, write a JSP page and let everyone in your team to watch which machines are up and which are down. Here goes my code:

MachineDetails.jsp (place it in /webapps folder)
(I am sick of placing HTML code here, so I have uploaded the file somewhere and given you the link, blogspot is frustrating me :-( )
This use something called machine.txt which is not but just a plain file with all machine names like:

machine.txt:

MachineName1
MachineName2
WinBox_15
LinuxBox_20
SolarixBox_1
MachineName11
..
..
and so on.

So simple and so useful :). There is a very small flaw in the code, which I like reader to find out :D.

Feel free to use this code for your lab condition.

Promise

Hello,
We promise to have something creative out here, nothing boring to learn :)

init()

Hi WCDevelopers,

I am going to write soon about web components.

Thanks !