Tuesday, June 28, 2011

Latest Servlet Interview Question beginner servlet tutorial

1) What is a servlet?
 Answer: Servlets are modules that extend request/response-oriented  servers, such as java-enabled web servers. For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to update a company’s order database.

2) What are the classes and  interfaces for servlets?

Answer: There are two packages in servlets and they are javax.servlet and javax.servlet.http.
Javax.servlet contains:
Interfaces Classes
Servlet Generic Servlet
ServletRequest ServletInputStream
ServletResponse ServletOutputStream
ServletConfig  ServletException
ServletContext UnavailableException
SingleThreadModel


Javax.servlet.http contains:
Interfaces    Classes
HttpServletRequest Cookie
HttpServletResponse HttpServlet
HttpSession HttpSessionBindingEvent
HttpSessionContext   HttpUtils
HttpSeesionBindingListener

3)What are the advantage of using Sessions over Cookies and URLReWriting?

Answer: Sessions are more secure and fast becasue they are stored at serverside. But Sessions has to be used combindly with Cookies or URLReWriting for mainting the client id that is sessionid at client side.Cookies are stored  at client side so some clients may disable cookies   so we may not sure that the cookies which  we are mainting  may work  or not but in sessions cookies are disable we can maintain our   sessionid using URLReWriting .In URLReWriting we can’t maintain large data because it leads to    network traffic and access may be become slow.Where as in seesions   will not maintain the data which we have to maintain instead we will maintain only the session id.

What is Servlet chaining ?
Answer: Servlet chaining is a technique in which two or more servlets can cooperate in servicing a single request.
In servlet chaining, one servlet output is piped to the next servlet  input. This process continues until the last servlet is reached. Its output is then sent back to the client.

1. Include:
    RequestDispatcher rd = req.getRequestDispatcher("Servlet2");
       rd.include(req, resp);

2. Forward, where req is HttpServletRequest and resp is HttpServletResponse:

    RequestDispatcher rd = req.getRequestDispatcher("Servlet3");
     rd.forward(req, resp);

What is a Servlet Filter java


Explain the Servlet Filter?


what purpose used the Servlet Filter?


Filters are not servlets. They do not implement and override HttpServlet methods such as doGet() or doPost(). Rather, a filter implements the methods of the javax.servlet.Filter interface. The methods are:


init()
destroy()
doFilter()


* Intercept a servlet's invocation before the servlet is called
* Examine a request before a servlet is called
* Modify the request headers and request data by providing a customized version of the request object that wraps the real request
* Modify the response headers and response data by providing a customized version of the response object that wraps the real response
* Intercept a servlet's invocation after the servlet is called

A filter implements javax.servlet.Filter and defines its three methods:

1. void init(FilterConfig config) throws ServletException: Called before the filter goes into service, and sets the filter's configuration object
2. void destroy(): Called after the filter has been taken out of service
3. void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException: Performs the actual filtering work



Example

import javax.servlet.*;

public class MyFilter implements javax.servlet.Filter {
public FilterConfig filterConfig; //1

public void doFilter(final ServletRequest request, //2
final ServletResponse response,
FilterChain chain)
throws java.io.IOException, javax.servlet.ServletException {
chain.doFilter(request,response); //3
}

public void init(final FilterConfig filterConfig) { //4
this.filterConfig = filterConfig;
}

public void destroy() { //5
}
}

No comments:

Post a Comment