- Back to Home »
- JEE »
- Java Servlet Interview Questions
How many objects of a servlet is created?
Only one. |
What is the life-cycle of a servlet?
What are the life-cycle methods for a servlet?
|
what is servlet collaboration?
Ans communication between two servlet is called servlet collaboration which is achieved by 3 ways.
1. RequestDispatchers include () and forward() method .
2. Using sendRedirect()method of Response object.
3. Using servlet Context methods
What is the difference between ServletConfig and ServletContext?
Ans: ServletConfig as the name implies provide the information about configuration of a servlet which is defined inside the web.xml file or we can say deployment descriptor.its a specific object for each servlet.
ServletContext is application specific object which is shared by all the servlet belongs to one application in one JVM .this is single object which represent our application and all the servlet access application specific data using this object.servlet also use their method to communicate with container.
When should you prefer to use doGet() over doPost()?
GET is preferred over POST in most of the situations except for the following:
- When the data is sensitive.
- When the data is greater than 1024 characters
- When the data is sensitive.
- When the data is greater than 1024 characters
What is lazy loading?
The servlets are not initialized by the container from the start. It happens when the servlet is requested for the first time. This is called lazy loading.
What are the types of Session Tracking ?
Following are the popular ways of session tracking:
a.) URL rewriting: In this method of session tracking, some extra data is appended at the end of the URL, which identifies the session. This method
is used for those browsers which do not support cookies or when the cookies are disabled by the user.
b.) Hidden Form Fields: This method is similar to URL rewriting. New hidden fields are embedded by the server in every dynamically generated form
page for the client. When the form is submitted to the server the hidden fields identify the client.
c.) Cookies: Cookie refers to the small amount of information sent by a servlet to a Web browser. Browser saves this information and sends it back to the server when requested next. Its value helps in uniquely identifying a client.
d.) Secure Socket Layer (SSL) Sessions
a.) URL rewriting: In this method of session tracking, some extra data is appended at the end of the URL, which identifies the session. This method
is used for those browsers which do not support cookies or when the cookies are disabled by the user.
b.) Hidden Form Fields: This method is similar to URL rewriting. New hidden fields are embedded by the server in every dynamically generated form
page for the client. When the form is submitted to the server the hidden fields identify the client.
c.) Cookies: Cookie refers to the small amount of information sent by a servlet to a Web browser. Browser saves this information and sends it back to the server when requested next. Its value helps in uniquely identifying a client.
d.) Secure Socket Layer (SSL) Sessions
What are the disadvantages of storing session state in cookies?
- Using a cookie, all the session data is stored with the client. If the cookies at client side get corrupt, purged or expired, the information received won't be complete.
- Some user may disable the cookies or their browser might not support them. Some users might have a firewall filtering out the cookies. So, you may either not receive the information or trying to switch to an alternate means may cause complexity.
- Cookie based solutions work only for HTTP clients.
- A low-level API controls the cookies. It is quite difficult to implement them.
- Some user may disable the cookies or their browser might not support them. Some users might have a firewall filtering out the cookies. So, you may either not receive the information or trying to switch to an alternate means may cause complexity.
- Cookie based solutions work only for HTTP clients.
- A low-level API controls the cookies. It is quite difficult to implement them.
Define HTTP Tunneling?
AnswerIn some organizations, the intranet is blocked by a firewall to the internet. It is exposed to the outer networks only by means of webserver port that accept only Http requests. In such situations, if protocols other than http are used, then they get rejected. The solution is to have them encapsulated in http or https and sent as an HttpRequest. Thus, masking other protocols as http requests is called HTTP Tunneling.
What is the difference between using getSession(true) and getSession(false) methods?
AnswergetSession(true) will check whether a session already exists for the user. If yes, it will return that session object else it will create a new session object and return it.
getSession(false) will check existence of session. If session exists, then it returns the reference of that session object, if not, this methods will return null.
Explain session tracking in Java Servlet.
The state of requests for the same user is being maintained by the session of a servlet. Session tracking is a mechanism that is tracked and maintained by such requests by the user. These sessions are being shared by the servlets and accessed by a client. This provides a convenience to the web applications that uses multiple servlets. For example a shopping cart application uses the session tracking, which tracks the requests of the user for various mobile devices. In java servlets this task is being maintained by:
Cookies, URL rewriting, session objects(HttpSession) and hidden fields.
Explain session tracking in servlets.
The states of a series of requests from a single user, for a stipulated period of time, are performed by the mechanism called session tracking. Sessions can be shared among the servlets which is a convenient for a web application that uses multiple servlets.
HTTP is a stateless protocol. The server cannot remember the previous transactions. To identify all the requests from one user, the web server is to remember every user. To overcome the problem user’s information is being managed by implemented by Cookies, URL rewriting, hidden fields. A session i.e., a user’s requests are tracked by – obtaining a session object, storing and retrieving data from the HttpSession object and invalidate the session after a user completes flushing the requests.
Explains the differences between context.getRequestDispatcher() and request.getRequestDispatcher() ? [Very Important]
• To create the relative path for the resource we use request.getRequestDispatcher(path) .
• To create the absolute path of the resource we use resourcecontext.getRequestDispatcher(path).
• To create the absolute path of the resource we use resourcecontext.getRequestDispatcher(path).