Sunday, October 26, 2008
Saturday, June 28, 2008
what is servlet container!!!!
Now, the question is, we have written a JSP or HTML page to raise a request and Servlet is placed there on server side,having the answer for raised request. Now there would be many servlets as such on the server.
Now who will map our request of that particular servlet and responds back in form of HTTP response.
The answer is : Servlet Container
It acts like a controller.
When container recieves a request for a servlet, through your HTTP GET or HTTP POST method, itcreates a response and request object first.
Then controller searches for the particular servlet requested, as mapped in your web.xml, and creates a thread for the same and assigns those request and response object to it.
Request and Response objects are of HttpServlet class, which your servlet extends.
Container calls the service() method of the servlet.
doGet() or doPost() method generates the dynamic page and attach to it the response object.
When thread completes, the container converts the response object into HTTP response and send it to client.
Followed by deletion of request and response object.
So this is the all story about our servlet CONTAINER.
Friday, June 27, 2008
The ServletContext
Servlet Context, in simple terms, as a shared memory segment for a web application.
when an object has been assigned to this shared memory, will be available to whole web application, until and unless it has been removed.
For every web application, there will be one of ServletContext.
Servlet context is defined in javax.servlet package.
Methods defined in this packages are used at server side to communicate with the servlet container.
They are as follows:
setAttribute()-- binds the object with the specified name, you give, in the current servlet context.
getAttribute()--returns the object assigned by the name provided.
removeAttribute()-- removes the particular object from servlet context.
getAttributeNames()--returns all the objects stored in the current servlet context.
Now, how to get a reference of servlet context,
ServletContext context=getServletContext(); //created context reference object
Now using context object set your object in servletcontext,
String name="JIIT";
context.setAttribute("USER",name);
So,now name object got set in the shared memory of web-application as USER and can be accessible throughout the web-application.
Sunday, June 8, 2008
Why Struts Framework ?
Well, the answer lies in the name "Strut" itself :)
Strut stands for " to provide a structure to impress other".
Four reasons of using Struts, as I think so,
a) easy to learn
b) easy to understand and handle the control flow of your web development
c) widely used, having many forum to discuss out the problem, in case, you face.
d) Many IDEs comes with struts tag.(e.g Jdeveloper)
Sunday, June 1, 2008
What is URL and URI?
Example : http://www.sun.com/
protocol --> http
domain name --> http://www.sun.com/
Here, Domain name(http://www.sun.com/) has been mapped to an IP address (this is the job of Domain Name Server[DNS] to map the entered URL to particular IP address )
URI is basically a part of full URL, it points to specific address.
For Example: http://www.sun.com/aboutsun/index.jsp
So here /aboutsun/index.jsp wil be the URI. Pointing towards the specific address of index.jsp page.
Friday, May 30, 2008
Session Death
- 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
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?
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)
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.