HOME      BLOG       FORUMS      CONTACT       ABOUT

JSP/Servlets interview questions – Part 3

61. How will you delete a cookie?

	Cookie c = new Cookie ("name", null);

62. What is the difference between Context init parameter and Servlet init parameter?
Servlet init parameters are for a single servlet only. No body out side that servlet can access that. It is declared inside the tag inside Deployment Descriptor, where as context init parameter is for the entire web application. Any servlet or JSP in that web application can access context init parameter. Context parameters are declared in a tag directly inside the tag. The methods for accessing context init parameter is getServletContext ().getInitParamter (“name”) where as method for accessing servlet init parameter is getServletConfig ().getInitParamter (“name”);

63. What are the different ways for getting a servlet context?
We will get ServletContext by calling getServletConfig ().getServletContext (). This is because a ServletConfig always hold a reference to ServletContext. By calling this.getServletContext () also we will get a ServletContext object.

64. What is the difference between an attribute and a parameter?
The return type of attribute is object, where the return type of parameter is String. The method to retrieve attribute is getAttribute () where as for parameter is getParameter (). We have a method setAttribute to set an attribute. But there is no setters available for setting a parameter.

65. How to make a context thread safe?
Synchronizing the ServletContext is the only solution to make a ServletContext thread safe.

	synchronized (getServletContext ()) {
		  // do whatever you want with thread safe context.

66. What is the difference between setting the session time out in deployment descriptor and setting the time out programmatically?
In DD time out is specified in terms of minutes only. But in programmatically it is specified in seconds. A session time out value of zero or less in DD means that the session will never expire. To specify session will never expire programmatically it must be negative value.

67. What JSP lifecycle methods we can override?
You cannot override the _jspService() method within a JSP page. You can however, override the jspInit() and jspDestroy() methods within a JSP page.

68. How will you include a static file in a JSP page?
You can include a static resource to a JSP using or <%@ inlcude >.

69. How you can perform browser redirection?
We can use the method sendRedirect of HttpServletResponse or forward method of RequestDispatcher.

70. Can we use ServletOutputStream object from a JSP page?
No. You are supposed to use JSPWriter object (given to you in the form of the implicit object out) only for replying to clients.

71. How can you stop JSP execution in the middle of processing a request?
We can use the return statement to stop the processing of JSP. Because JSP is compiled to servlet and all the statements will go inside service method, any time you can stop the processing using return statement.

72. How can I invoke a JSP error page from a servlet?
You can invoke the JSP error page and pass the exception object to it from within a servlet. For that you need to create a request dispatcher for the JSP error page, and pass the exception object as a javax.servlet.jsp.jspException request attribute.

73. How will you pass information from JSP to included JSP?
By using <%jsp:param> tag.

74. How does JSP handle runtime exceptions?
Using errorPage attribute of page directive JSP handles runtime exceptions. We need to specify isErrorPage=true if the current page is intended to use as a JSP error page.

75. How can I enable session tracking for JSP pages if the browser has disabled cookies?
By default session tracking uses cookies to associate a session identifier with a user. If the browser does not support cookies, or if cookies are disabled, you can still use session tracking using URL rewriting. For URL rewriting to be effective, you need to append the session ID for each and every link that is part of your servlet response. By using the methods response.encodeURL() and response.encodeRedirectURL() we can achieve this.

76. How can you declare methods in your JSP page?
You can declare methods as declarations in your JSP page. The methods can then be invoked within any other methods you declare, or within JSP scriptlets and expressions.

77. How can we set the inactivity period on a per-session basis?
We can set the session time out programmatically by using the method setMaxInactiveInterval() of HttpSession.

78. How do you pass an init parameter to a JSP?
You need to configure the DD for passing init parameter to a JSP. You can configure the DD as follows.


79. How can my application get to know when a HttpSession is removed?
You can define a class which implements HttpSessionBindingListener and override the valueUnbound() method.

80. How many cookies can one set in the response object of the servlet? Also, are there any restrictions on the size of cookies?
If the client is using Netscape, the browser can receive and store 300 total cookies and 4 kilobytes per cookie. And the no of cookie is restricted to 20 cookies per server or domain


Tags: , , , ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply