JSP/Servlets interview questions – Part 1
21. What are the implicit objects?
Certain objects that are available for the use in JSP documents without being declared first. These objects are parsed by the JSP engine and inserted into the generated servlet. The implicit objects are: request, response, pageContext, session, application, out, config, page, exception
22. What’s the difference between forward and sendRedirect?
forward is server side redirect and sendRedirect is client side redirect. When you invoke a forward request, the request is sent to another resource on the server, without the client being informed that a different resource is going to process the request. This process occurs completely with in the web container And then returns to the calling method. When a sendRedirect method is invoked, it causes the web container to return to the browser indicating that a new URL should be requested. Because the browser issues a completely new request any object that are stored as request attributes before the redirect occurs will be lost. This extra round trip a redirect is slower than forward. Client can disable sendRedirect.
23. What are the different scopes available ?
page, request, session, application
24. Is JSP extensible ?
Yes, it is. JSP technology is extensible through the development of custom actions, or tags, which are encapsulated in tag libraries.
25. What’s the Servlet Interface?
The central abstraction in the Servlet API is the Servlet interface. All servlets implement this interface, either directly or, more commonly, by extending a class that implements it such as HttpServlet.
26. What are two different types of Servlets ?
GenericServlet and HttpServlet. HttpServlet is used to implement HTTP protocol, where as Generic servlet can implement any protocol.
27. What is the life cycle of servlet?
Each servlet has the same life cycle: first, the server loads and initializes the servlet by calling the init method. This init() method will be executed only once during the life time of a servlet. Then when a client makes a request, it executes the service method. finally it executes the destroy() method when server removes the servlet.
28. Can we call destroy() method on servlets from service method ?
29. What is the need of super.init (config) in servlets ?
Then only we will be able to access the ServletConfig from our servlet. If there is no ServletConfig our servlet will not have any servlet nature.
30. What is the difference between GenericServlet and HttpServlet?
GenericServlet supports any protocol. HttpServlet supports only HTTP protocol. By extending GenericServlet we can write a servlet that supports our own custom protocol or any other protocol.
32. Can we write a constructor for servlet ?
Yes. But the container will always call the default constructor only. If default constructor is not present , the container will throw an exception.
33. What is the difference between <%@ include ...> (directive include) and 34. Can I just abort processing a JSP? 35. What are the parameters for service method ? 36. What are cookies ? 37. How do I prevent the output of my JSP or Servlet pages from being cached by the browser? 38. How to refer the “this” variable within a JSP page? 39. How many JSP scripting elements and what are they? 40. Can we implement an interface in JSP ?
@ include is static include. It is inline inclusion. The contents of the file will get included on Translation phase. It is something like inline inclusion. We cannot have a dynamic filename for directive include.
Yes. You can put a return statement to abort JSP processing.
ServletRequest and ServletResponse
Cookies are small textual information that are stored on client computer. Cookies are used for session tracking.
By setting appropriate HTTP header attributes we can prevent caching by the browser
response.setHeader("Cache-Control","no-store"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader ("Expires", 0); //prevents caching at the proxy server
Under JSP 1.0, the page implicit object page is equivalent to “this”, and returns a reference to the servlet generated by the JSP page.
There are three scripting elements in JSP . They are declarations, scriptlets, expressions.
34. Can I just abort processing a JSP?
35. What are the parameters for service method ?
36. What are cookies ?
37. How do I prevent the output of my JSP or Servlet pages from being cached by the browser?
38. How to refer the “this” variable within a JSP page?
39. How many JSP scripting elements and what are they?
40. Can we implement an interface in JSP ?