Saturday, June 28, 2008

what is servlet container!!!!

As client sends a request to a server, server receives HTTP request and have to respond in HTTP response.Well the server way of communicating over a network is through HTTP, i.e it understands HTTP.
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 ?

Once I encountered with question, "Why are you using Struts Framework in your project?", in an interview.
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?

URL refers to the protocol(like http:// or ftp:// ) and domain name (http://www.sun.com/) of the requested resource.

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.