1. What is difference between PrintWriter and ServletOutputStream? PrintWriter
is a character-stream class whereas ServletOutputStream is a
byte-stream class. We can use PrintWriter to write character based
information such as character array and String to the response whereas
we can use ServletOutputStream to write byte array data to the response. We
can use ServletResponse getWriter() to get the PrintWriter instance
whereas we can use ServletResponse getOutputStream() method to get the
ServletOutputStream object reference.
2. Can we get PrintWriter and ServletOutputStream both in a servlet? We
can?t get instances of both PrintWriter and ServletOutputStream in a
single servlet method, if we invoke both the methods; getWriter() and
getOutputStream() on response; we will get
java.lang.IllegalStateException at runtime with message as other method
has already been called for this response.
3. What is the use of servlet wrapper classes? Servlet
HTTP API provides two wrapper classes ? HttpServletRequestWrapper and
HttpServletResponseWrapper. These wrapper classes are provided to help
developers with custom implementation of servlet request and response
types. We can extend these classes and override only specific methods we
need to implement for custom request and response objects. These
classes are not used in normal servlet programming.
4. What is SingleThreadModel interface? The
servlet programmer should implement SingleThreadModel interface to
ensure that servlet can handle only one request at a time. It is a
marker interface, means have no methods.
This interface is
currently deprecated since Servlet API 2.4 because it doesn't solves all
the thread-safety issues such as static variable and session attributes
can be accessed by multiple threads at the same time even if we have
implemented the SingleThreadModel interface. So it is recommended to use
other means to resolve these thread safety issues such as synchronized
block etc.
5. Are Servlets Thread Safe? How to achieve thread safety in servlets? HttpServlet
init() method and destroy() method are called only once in servlet life
cycle, so we don?t need to worry about their synchronization. But
service methods such as doGet() or doPost() are getting called in every
client request and since servlet uses multithreading, we should provide
thread safety in these methods. If there are any local variables in
service methods, we don?t need to worry about their thread safety
because they are specific to each thread but if we have a shared
resource then we can use synchronization to achieve thread safety in
servlets when working with shared resources. The thread safety mechanisms are similar to thread safety in standalone java application. 6. What are different methods of session management in servlets? Session
is a conversional state between client and server and it can consists
of multiple request and response between client and server. Since HTTP
and Web Server both are stateless, the only way to maintain a session is
when some unique information about the session (session id) is passed
between server and client in every request and response. Some of the common ways of session management in servlets are: User Authentication HTML Hidden Field Cookies URL Rewriting Session Management API
7. Difference between statefull and stateless session?
8. Types of client side session is servlet? Cookies URL rewriting Hidden Form Fields Cookies: Cookies
are information that a Web server sends to a browser and that the
browser returns back to web server when visiting the same Web site
later. Cookie can be saved in a browser as a key value pair To add
a cookie in browser we need to create an object for cookie class and
use a HttpResponse object to send the data to browser. To create cookie object: Cookie ck=new Cookie(?key?,?value?); To add cookie to browser Response.addCookie(ck); // addCookie method takes cookie objects as argument To get or retrieve the all the cookie present in the browser we need to use a HttpRequest object To get cookie:
Cookie ck[]=request.getCookies();
To display cookie present in cookie array:
for(int i=0;i<ck.length;i++){
String str=ck[i].getName();
String value=ck[i].getValue();
out.println("<b><br>Name of the Cookie :"+str);
out.println("<b><br> Value of the cookie :"+value);
}
URL Rewriting: URL rewriting is the process of sending information to another servlet or same servlet in the form of url URL rewriting cannot be used for secure data, it will not works under post request. URL rewriting can be used to maintain a page hits In
URL rewriting we append a identifier to the URL of next servlet or next
resource. We can send parameter name/values pairs using the following
format: http://mydomain/url?name1=value&name2=value2&?? From the servlet we can use getParameter() method to obtain a parameter value.
String value1=request.getParameter("name1");
String value2=request.getParameter("name2");
Hidden Form fields: Hidden form is a form of session tracking which saves the information in client browser itself. We uses hidden box in html to hide the session information from client. We
cannot use this type of session for secure data because when a client
views the source code of the html page the session information can be
view by all.
<input type ="hidden" name ="session" value="sessionid"/>
In servlet to get session information
String value = request.getParameter("session");
One
way to support anonymous session tracking is to use hidden form fields.
As the name implies, these are fields added to an HTML form that are
not displayed in the client?s browser. They are sent back to the server
when the form that contains them is submitted. In case of hidden form field an invisible text field is used for maintaining state of an user.
9. Types of server side session? 1. HttpSession can be used to maintain token on server side.
10. What is URL Rewriting? A
rewrite engine is a software component that performs rewriting on
Uniform Resource Locators, modifying their appearance. This modification
is called URL rewriting. It is a way of implementing URL mapping or
routing within a web application. The engine is typically a component of
a web server or web application framework