Interview questionsJava

J2EE Interview questions

HTTP is a stateless protocol. So, how to maintain state between requests in J2ee ?

State can be maintained in following ways:

HttpSession

Hidden Field

URL Rewriting

Cookies

Hidden fields on the page can maintain state and they aren’t visible on the browser. The downside is they may expose sensitive information.

In URL rewriting, sensitive information is appended to the URL. Since it shows up in the URL, it can’t be used for sensitive information.

Cookies are stored as text in name-value pair that the webserver can store on the client and retrieve later. The cookie size is limited and the browser may choose to turn off cookies.

HttpSession is the preferred way to maintain state. A session can identify requests generating from same browser during a conversation time period. The session object can be used by multiple servlets. When a new session is created on the server, the SESSIONID can be passed to the client using cookies or url rewriting.

 

Explain life cycle of Servlet

A web container is responsible for maintaining servlet life cycle.

The container instantiates a server and calls init() method.

Once init() completes, the servlet is ready to receive requests from clients. For each request, the container calls the service method by creating a new thread.

Before destroying the servlet instance, the container calls the destroy() method in the servlet. At this point the servlet is eligible for garbage collection.

 

Difference between request forward and redirect(sendRedirect)

Forward action takes place within the server without the browser knowing about it.

SendRedirect sends a header back to the browser containing redirecting information. The browser makes a new request to the resource mentioned in the header information. The overhead is the extra trip, but the advantage is it can access any resource in same domain or a different domain.

 

What are ServletContext and ServletConfig objects ?

ServletConfig keeps configuration parameters for a particular servlet. The parameters are specified under <servlet> tag in web.xml file

ServletContext object holds paramters for the entire web application. The parameters are specified under the <context-param> tag in the web.xml

 

How to make a servlet ThreadSafe ?

Typically a web-container creates a single instance of the servlet and creates multiple threads to handle the service method for multiple requests.

There are 2 ways for thread safety:

The servlet can implement the marker interface SingleThreadModel. This will affect performance though.

The other approach is to access the shared resources in a synchronized manner. This is the preferred approach. We can use shared resources in a read only manner or synchronize access to them.

 

What are filters ?

Fliters dynamically intercept request or response to transform or use it some way.

It’s an implementation of Chain of Responsibility pattern where each filter does some task and forwards to the next filter.

Filters improve reusability.

Common use for filters includes common tasks like logging, authentication, auditing etc.

Filters are configured through the web.xml file using the <filter> and <filter-mapping> tags.

 

What is a RequestDispatcher ?

RequestDispatcher is obtained from the ServletContext. It forwards the request to a servlet/jsp to get the response.

 
Here is the link to part 2 of the J2ee Interview questions and answers:

J2ee Interview Questions – part2

 

 You may also like the following posts for interview questions :

 
Java Interview Questions

Java Collections Interview Questions

Java Webservice Interview Questions

Hibernate Interview questions

Spring Interview questions

 

© 2015, www.techkatak.com. All rights reserved.

One thought on “J2EE Interview questions

Comments are closed.