1. What is the use of ServletConfig and ServletContext..?
2. How to communicate between two servlets?
3. What is difference between sendRedirect() and forward()..? Which one is faster then other and which works on server?
4. How to create and call stored procedures?
Ans To create stored procedures:
Create procedure procedurename (specify in, out and in out parameters)
BEGIN Any multiple SQL statement;
END;
To call stored procedures:
CallableStatement csmt = con. prepareCall(”{call procedure name(?,?)}”);
csmt. registerOutParameter(column no. , data type);
csmt. setInt(column no. , column name)
csmt. execute();
5. What is session tracking and how do you track a user session in servlets?
Ans: Session tracking is a mechanism that servlets use to maintain state about a series requests from the same user across some period of time.
The methods used for session tracking are:
a) User Authentication - occurs when a web server restricts access to some of its resources to only those clients that log in using a recognized username and password.
b) Hidden form fields - fields are added to an HTML form that are not displayed in the client’s browser. When the form containing the fields is submitted, the fields are sent back to the server.
c) URL rewriting - every URL that the user clicks on is dynamically modified or rewritten to include extra information. The extra information can be in the form of extra path information, added parameters or some custom, server-specific URL change.
d) Cookies - a bit of information that is sent by a web server to a browser and which can later be read back from that browser.
e) HttpSession- places a limit on the number of sessions that can exist in memory. This limit is set in the session. maxresidents property.
6. How do servlets handle multiple simultaneous requests?-
Ans: The server has multiple threads that are available to handle requests. When a request comes in, it is assigned to a thread, which calls a service method (for example: doGet(), doPost() and service()) of the servlet. For this reason, a single servlet object can have its service methods called by many threads at once.
7. How can an existing session be invalidated?
Ans: An existing session can be invalidated in the following two ways:
<session-timeout>10</session-timeout>
</session-config>
This will set the time for session timeout to be ten minutes.
The setMaxInactiveInterval() method sets the maximum time in seconds before a session becomes invalid.
Note :Setting the inactive period as negative(-1), makes the container stop tracking session, i.e, session never expires.
8. How are filters?
Ans: Filters are Java components that are used to intercept an incoming request to a Web resource and a response sent back from the resource. It is used to abstract any useful information contained in the request or response. Some of the important functions performed by filters are as follows:
9. What are the functions of an intercepting filter?
Ans: The functions of an intercepting filter are as follows:
10. Why there are no constructors in servlets?
Ans: A servlet is just like an applet in the respect that it has an init() method that acts as a constructor. Since the servlet environment takes care of instantiating the servlet, an explicit constructor is not needed. Any initialization code you need to run should be placed in the init () method since it gets called when the servlet is first loaded by the servlet container.
11. What does Servlet API provide to store user data between requests? (Hint: HttpSession)
12. What is the difference between forwarding a request and redirecting? (Hint: redirect return to browser )
13. What object do you use to forward a request? (Hint: RequestDispatcher)
14. What do you need to be concerned about with storing data in a servlet instance fields? (Hint: Multi-threaded.)
15. What’s the requirement on data stored in HttpSession in a clustered (distributable) environment? (Hint: Serializable)
16. If I store an object in session, then change its state, is the state replicated to distributed Session? (Hint: No, only on setAttribute() call.)
17. How does URL-pattern for servlet work in the web.xml? (Hint: /ddd/* or *.jsp)
18. What is a filter, and how does it work? (Hint: Before/after request, chain.)
19. Difference between HttpServlet and GenericServlet?
Ans:
javax.servlet.GenericServlet
Signature: public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig, java.io.Serializable
Signature: public abstract class HttpServlet extends GenericServlet implements java.io.Serializable
2. How to communicate between two servlets?
3. What is difference between sendRedirect() and forward()..? Which one is faster then other and which works on server?
4. How to create and call stored procedures?
Ans To create stored procedures:
Create procedure procedurename (specify in, out and in out parameters)
BEGIN Any multiple SQL statement;
END;
To call stored procedures:
CallableStatement csmt = con. prepareCall(”{call procedure name(?,?)}”);
csmt. registerOutParameter(column no. , data type);
csmt. setInt(column no. , column name)
csmt. execute();
5. What is session tracking and how do you track a user session in servlets?
Ans: Session tracking is a mechanism that servlets use to maintain state about a series requests from the same user across some period of time.
The methods used for session tracking are:
a) User Authentication - occurs when a web server restricts access to some of its resources to only those clients that log in using a recognized username and password.
b) Hidden form fields - fields are added to an HTML form that are not displayed in the client’s browser. When the form containing the fields is submitted, the fields are sent back to the server.
c) URL rewriting - every URL that the user clicks on is dynamically modified or rewritten to include extra information. The extra information can be in the form of extra path information, added parameters or some custom, server-specific URL change.
d) Cookies - a bit of information that is sent by a web server to a browser and which can later be read back from that browser.
e) HttpSession- places a limit on the number of sessions that can exist in memory. This limit is set in the session. maxresidents property.
6. How do servlets handle multiple simultaneous requests?-
Ans: The server has multiple threads that are available to handle requests. When a request comes in, it is assigned to a thread, which calls a service method (for example: doGet(), doPost() and service()) of the servlet. For this reason, a single servlet object can have its service methods called by many threads at once.
7. How can an existing session be invalidated?
Ans: An existing session can be invalidated in the following two ways:
- Setting timeout in the deployment descriptor: This can be done by specifying timeout between the <session-timeout>tags as follows:
<session-timeout>10</session-timeout>
</session-config>
This will set the time for session timeout to be ten minutes.
- Setting timeout programmatically: This will set the timeout for a specific session. The syntax for setting the timeout programmatically is as follows:
The setMaxInactiveInterval() method sets the maximum time in seconds before a session becomes invalid.
Note :Setting the inactive period as negative(-1), makes the container stop tracking session, i.e, session never expires.
8. How are filters?
Ans: Filters are Java components that are used to intercept an incoming request to a Web resource and a response sent back from the resource. It is used to abstract any useful information contained in the request or response. Some of the important functions performed by filters are as follows:
- Security checks
- Modifying the request or response
- Data compression
- Logging and auditing
- Response compression
9. What are the functions of an intercepting filter?
Ans: The functions of an intercepting filter are as follows:
- It intercepts the request from a client before it reaches the servlet and modifies the request if required.
- It intercepts the response from the servlet back to the client and modifies the request if required.
- There can be many filters forming a chain, in which case the output of one filter becomes an input to the next filter. Hence, various modifications can be performed on a single request and response.
10. Why there are no constructors in servlets?
Ans: A servlet is just like an applet in the respect that it has an init() method that acts as a constructor. Since the servlet environment takes care of instantiating the servlet, an explicit constructor is not needed. Any initialization code you need to run should be placed in the init () method since it gets called when the servlet is first loaded by the servlet container.
11. What does Servlet API provide to store user data between requests? (Hint: HttpSession)
12. What is the difference between forwarding a request and redirecting? (Hint: redirect return to browser )
13. What object do you use to forward a request? (Hint: RequestDispatcher)
14. What do you need to be concerned about with storing data in a servlet instance fields? (Hint: Multi-threaded.)
15. What’s the requirement on data stored in HttpSession in a clustered (distributable) environment? (Hint: Serializable)
16. If I store an object in session, then change its state, is the state replicated to distributed Session? (Hint: No, only on setAttribute() call.)
17. How does URL-pattern for servlet work in the web.xml? (Hint: /ddd/* or *.jsp)
18. What is a filter, and how does it work? (Hint: Before/after request, chain.)
19. Difference between HttpServlet and GenericServlet?
Ans:
javax.servlet.GenericServlet
Signature: public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig, java.io.Serializable
- GenericServlet defines a generic, protocol-independent servlet.
- GenericServlet gives a blueprint and makes writing servlet easier.
- GenericServlet provides simple versions of the lifecycle methods init and destroy and of the methods in the ServletConfig interface.
- GenericServlet implements the log method, declared in the ServletContext interface.
- To write a generic servlet, it is sufficient to override the abstract service method.
Signature: public abstract class HttpServlet extends GenericServlet implements java.io.Serializable
- HttpServlet defines a HTTP protocol specific servlet.
- HttpServlet gives a blueprint for Http servlet and makes writing them easier.
- HttpServlet extends the GenericServlet and hence inherits the properties GenericServlet.