Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

August 26, 2010

Important points : java.util.Logger

The Logger class implements Factory patter to return logger instance.
The getLogger() method will either create a new logger instance or return a suitable(having same name as specified) existing instance. Each logger has a level associated with it, which specifies the minimum level the logger takes care of. If a  logger level is null, then its level is inherited from its parent.So, if the log level is changed for a class, the change would effect its child loggers which have the log level set as null.

Also, all methods in a logger are thread-safe.

RequestDispatcher : forward()

The argument to forward method is the name of the resource we want the request to be forwarded to. This can be a jsp file or a Servlet. In case it is a servlet, the argument should be whatever we have specified in the url-pattern for that servlet declaration in web.xml.

So, the point to be noted here is that in case we want to forward the request to some other servlet, the argument to forward method should be the url-pattern of that servlet.

Also, as we (probably) already know, the URL in the browser is not changed in case of forward. i.e. the client does not get to know that internally the request has been forwarded to some other resource.

August 2, 2010

Response : sendRedirect()

sendRedirect() method takes String as an argument. This string can start with a forward slash.

E.g.

Lets say the current URL we are accessing is : http://localhost:8080/MyApp/MyServlet/welcome.do

Now in our Servlet, we can do either :
response.sendRedirect("thanks.jsp");
OR
response.sendRedirect("/thanks.jsp");

Both of these would hit differnet URLs

In first case, i.e. response.sendRedirect("thanks.jsp");
The server will hit URL that is relative to the original URL, i.e http://localhost:8080/MyApp/MyServlet/thanks.jsp

and in second case,
the server builds the complete URL, relative to the web application itself.(instead of relative to original URL)
i.e.  http://localhost:8080/MyApp/thanks.jsp

One more thing to note here is that the argument to sendRedirect() is a String only and not an object and the URL should be something that the server can generate a complete URL from the relative. i.e. it should be relative to something (be it relative to current URL or to the web-app).
So, any argument like "www.pragyarawal.tk/welcome.jsp" would not work as the server would try to create a complete URL from it. and in this case, the server would throw an "IllegalStateException".

PrintWriter : print() vs write()

Today while reading Servlets, I came across the class PrintWriter.


I found that there we have two methods (both with various overloaded versions) : write() and print()
and I saw that print() internally calls write() only. So I was wondering that why there are different methods although they are doing the same thing. So, this seemed to be kind of redundancy to me.


Then I realized that print() is actually used as a utility method there, to which u can pass any literal value say whether it is boolean/char/int/float/double (to its overloaded versoins), but in write u have a limitaion of using only int /char array or string.So print() is just encapsulating the implementation of write() method by providing number of overlaoded method by which u can call the write().



August 1, 2010

HttpServletRequest

Some not very frequently used methods :

getServerPort():  Returns the port number to which the request was sent. (8080 in my case)
getServerName(): Name of the server (I used localhost)
getLocalAddr(): I got 0.0.0.0
getLocalPort() : This returns the port at which the request actually ends up. This is as the requests are made to the same port, but the server forwards them to different ports so that the application it can handle multiple threads.
getRemotePort(): Since the server asks for remote port, it is the client that is remote for the server. So, it returns the port number on the client on which the request was sent.