Servlets – Question Answer way!
Q. What is Container ?
A. Servlets dint have a main() method. They are under control of another Java application called a “Container”.
Example of a Container is Tomcat.
Example of a web server is Apache.
Container provides:-
1. Communication Support: provide easy way for servlet to talk to your web server.
2. Lifecycle Management: of servlet
3. Multithreading support: a new java thread is create to handle each servlet request.
4. Declarative security: via xml configuration.
5. JSP Support: converting JSP to real java code.
Q. Explain Life-cycle of a servlet
A.1 Container Load class
2. Instantiate Servlet (Invoke its default constructor)
2.A Load ServletConfig
2.B Load ServletContext
3. Call init() -> which may contain, getting a DB Connection.
4. service(), which eventually generate a thread to process each request and subsequently call doGet() & doPost()
5. destroy()
!Note
When you have multiple parameter values for a single parameter, you need getPatameterValues() that returns an array instead of getParameter()
Q. Beside parameter, what can I get from request object?
A. request.getHeader(“User-Agent”); -> Get request header
request.getCookies();
request.getSession();
request.getMethod();
request.getInputStream(); -> get all raw request information.
Q. Important method to call on response
A. setContentType()
getWriter()
getOutputStream()
Q. Difference between forward and sendRedirect
A.1 forward (page 89)
- Control can be forward to resources available within the server from where the call is made. This transfer of control is done by the container internally and browser / client is not involved.
- The original request and response objects are transfered along with additional parameters if needed.
RequestDispatcher view = request.getRequestDispatcher(“result.jsp”);
view.forward(request, response);
A.2 sendRedirect (page 136)
if (workforme)
{
//hadel request
}else {
response.sendRedirect(“http://www.oreilly.com”);
}
Control can be redirect to resources to different servers or domains. This transfer of control task is delegated to the browser by the container. That is, the redirect sends a header back to the browser / client. This header contains the resource url to be redirected by the browser. Then the browser initiates a new request to the given url. Since it is a new request, the old request and response object is lost.
For example, sendRedirect can transfer control from http://javapapers.com to http://anydomain.com but forward cannot do this.
‘session’ is not lost in both forward and redirect.
To feel the difference between forward and sendRedirect visually see the address bar of your browser,
in forward, you will not see the forwarded address (since the browser is not involved)
in redirect, you can see the redirected address.
You can call sendRedirect() after response is already written. ->IlligalStateException.
Attributes are not parameters!
3 Attribute types:-
1. Application/context
2. Request
3. Session
1. Application/context
getServletContext().set/getAttribute(“”,”");
They are not thread safe. to make it thread safe, we need to lock servletContext as below:-
Synchronized (getServletContext()) {}
2. Session
requirest.getSession().get/setAttribute(“”,”");
Multiple request from same session, say multiple browser window
Lock on session
Synchronized (session){}
Implementing SingleThreadModel interface is also not a solution , it is as good as synchronizing service method.
Also result in poor performance.
3. Request
Only request attribute and local variable are thread safe
Use when some other component of the app take over all or part of the request via
RequestDispatcher.
By two of its method forward() & include() ()
Get RD from request or servletContext(/ is must, root of application, not relative to current request)
Session
If http uses state less connection so how container keep track of the user session ?
On first request , container generate a unique session Id to send with the response & client keep sending it for each subsequent request.
This happen via cookie maintained by the container.
What if client has cookies disabled on his browser ?
URL re-writing happen automatically if cookies not supported, we only need to call response.encodeUrl()
Invalidate the session if not active
Now! JSP
Page directives:-
@
scriptlet
%%
expression
=
s and e land up in service method
Declaration
!
Added to class outside of the service method, it can be variable declaration or method.
Implicit objects :-
out JspWriter
session HttpSession
request HttpServletRequest
response HttpServletResponse
config ServletConfig
application ServletContext
exception JspException
pageContext PageContext
page Object
Comments
HTML !
Jsp %%