Thursday, 4 February 2016

Servlet FAQ

High
  • What is a servlet?
  • What is servlet life cycle?
  • Servlet vs filter?
  • Can we call destroy method inside init? What will happen?
  • Forward vs redirect?
  • ServletConfig vs ServletContext?
  • <load-on-startup> means?
  • What is servlet Colllaboration? How to do it?
  • How to create deadlock in servlet?
  • Web server vs Application Server?
  • Get vs Post?
  • Idempotent vs Non-idempotent?
  • Servlet vs CGI?
  • What is URL encoding?
  • What is URL rewriting?
  • How to maintain session?

*****************ANSWERS*****************
What is a servlet?
Servlet is a Java class that extends server capabilities which hosts applications based on request-response programming model.

What is servlet life cycle?
The life cycle of servlet is controlled by the container. When a request is mapped to a servlet, the container checks whether there is an instance of the servlet, if not then:
a)It loads the servlet, creates an instance of it and initializes it by calling init() method.
b) Then it calls the service() method passing request and response objects, which calls either doGet() or doPost() according to the request.
c) It it needs to remove the object, it calls the destroy() method.

Servlet vs filter?
Filter is used to modify,block or log the request or response based on specific conditions. Filters are called on per request basis, thus they are best for purpose like authentication,logging,etc.
Servlet on the other hand are meant to contain business logic, process request and generate response.

Filter vs listener?
1) Filter is used to modify,block or log the request or response based on specific conditions while listener is used to listen to events in container such as context intialized,destroyed or session is created,destroyed and to monitor attributes in session,request and context.
2) Filter is called on per request basis while listener is called when an event occurs.
3) Filter has access to request objects while listener doesn't.


Can we call destroy method inside init? What will happen? 
Yes, we can call destroy method inside init. However, nothing will happen, container will just ignore it and continue to proceed in normal way and servlet will complete it's lifecycle by calling destroy method.

Forward vs sendRedirect?
Forward is used to forward a request to another resource in the same server while redirect sends a response to the user with the path of new url to which the browser then send fresh request.
Forward method is part of requestdispatcher which we can get either from request or servletcontext while sendredirect is part of response object.
All the attributes are part of the request in case of forward while in case of sendRedirect everything is afresh.
Forward is faster than sendRedirect because in case of sendRedirect there is extra work of response going to browser and then browser makes a fresh request.
In case of forward session is maintained while in case of sendRedirect we have to encode the session data with URL.

ServletConfig vs ServletContext?
Each servlet has it's own ServletConfig object which has servlet parameters and other information while there is a ServletContext object for the entire application which contains global attributes and application related information.

<load-on-startup> means?
if <load-on-startup> is positive, the servlet is loaded at application startup otherwise it will be loaded when  it's first required for a request processing. Thus, it reduces the request processing time for the first request.

What is servlet Colllaboration? How to do it?
Communication among servlets is called servlet collaboration. It can be done using RequestDispatcher. RequestDispatcher has two methods, include and forward. Using include we can include the content of other servlet in a servlet and using forward we can send the request to any other servlet for further processing.

How to create deadlock in servlet?
Call doGet() from doPost() and vice versa.

Web server vs Application Server?
Web server is used for handling request and serving response while Application Server does more than that like it acts as container and handle lifeycle of servlets , provides event listener mechanism and session maintenance option.

Get vs Post?
Get is idempotent while Post is non-idempotent.
Get is default while for Post we have to explicitly mention request as post.
Get carries data in header while Post carries data in body.
Get can carry limited amount of data while Post can carry a lot of data.
Get request is visible in browser url hence unsafe and can be bookmarked while Post in not visible, hence it's safe and can't be bookmarked in browser.


Idempotent vs Non-idempotent?
Idempotent methods are safe and produce same result no matter how many times you use while Non-idempotent are unsafe and can produce different results for the same request if used multiple times. Post is the only non-idempotent method.

Servlet vs CGI?
Servlet spawns a new thread per request while CGI spawns a new process per request. Thus, Servlet has better performance than CGI.

What is URL encoding?

What is URL rewriting?

How to maintain session?

******************************************************************************************
Avg
Servlet lifecycle vs filter lifecycle?
What is filter? Lifecycle?
What is listener? Types? Uses?
What is different between web server and application server?
Which HTTP method is non-idempotent?
What is the difference between GET and POST method?
What is MIME Type?
What is a web application and what is it’s directory structure?
What is a servlet?
What are the advantages of Servlet over CGI?
What are common tasks performed by Servlet Container?
What is ServletConfig object?
What is ServletContext object?
What is difference between ServletConfig and ServletContext?
What is Request Dispatcher?
What is difference between PrintWriter and ServletOutputStream?
Can we get PrintWriter and ServletOutputStream both in a servlet?
How can we create deadlock situation in servlet?
What is the use of servlet wrapper classes?
What is SingleThreadModel interface?
Do we need to override service() method?
Is it good idea to create servlet constructor?
What is difference between GenericServlet and HttpServlet?
What is the inter-servlet communication?
Are Servlets Thread Safe? How to achieve thread safety in servlets?
What is servlet attributes and their scope?
How do we call one servlet from another servlet?
How can we invoke another servlet in a different application?
What is difference between ServletResponse sendRedirect() and RequestDispatcher forward() method?
Why HttpServlet class is declared abstract?
What are the phases of servlet life cycle?
What are life cycle methods of a servlet?
why we should override only no-agrs init() method.
What is URL Encoding?
What are different methods of session management in servlets?
What is URL Rewriting?
How does Cookies work in Servlets?
How to notify an object in session when session is invalidated or timed-out?
What is the difference between encodeRedirectUrl and encodeURL?
Why do we have servlet filters?
What is the effective way to make sure all the servlets are accessible only when user has a valid session?
Why do we have servlet listeners?
How to handle exceptions thrown by application with another servlet?
What is a deployment descriptor?
How to make sure a servlet is loaded at the application startup?
How to get the actual path of servlet in server?
How to get the server information in a servlet?
Write a servlet to upload file on server.
How do we go with database connection and log4j integration in servlet?
How to get the IP address of client in servlet?
What are important features of Servlet 3?
What are different ways for servlet authentication?
How can we achieve transport layer security for our web application?

What are Servlets?
What are the advantages of servlets over CGI?
What are the major tasks of servlets?
Explain servlet life cycle.
When init() method of servlet gets called?
When service() method of servlet gets called?
When doGet() method of servlet to be called?
When doPost() method of servlet to be called?
When destroy() method of servlet gets called?
For what purpose init() method of a servlet is used?
For what purpose destroy() method of a servlet is used?
For what purpose doGet() method of a servlet is used?
For what purpose doPost() method of a servlet is used?
Explain working of service() method of a servlet.
How to read form data in servlet?
How to read name of all parameters in servlet?
How to read http header information in servlet?
What is HTTPServletRequest class?
What is HTTPServletResponse class?
How to write html contents using servlets?
How to send an authentication error from a servlet?
How to redirect a request from a servlet to another servlet?
How sendRedirect method works?
How sendError method works?
What are servlets filters?
Name some of the servlets filters?
How to do servlet filter mapping?
For what purpose init() method of a filter is used?
For what purpose doFilter() method of a filter is used?
For what purpose destroy() method of a filter is used?
Can multiple filters be configured?
Can filtering be done in an ordered way? If so then how to achieve it?
How to configure a central error handling page in servlets?
How to configure a central error handler in servlets?
What are cookies?
How to create a cookie using servlet?
How to read a cookie using servlet?
How to delete a cookie using servlet?
What is session?
What is URL rewriting?
How to create a session in servlet?
How to delete a session in servlet?
How to update an attribute in session in servlet?
How to set session timeout in servlet?
How to set auto page refresh in servlet?
What is internalization?
What is localization?
What is locale?
How to detect locale in Servlets?
How to get country name in Servlets?

What is JSP?
What are advantages of using JSP?
What are the advantages of JSP over Active Server Pages (ASP)?
What are the advantages of JSP over Pure Servlets?
What are the advantages of JSP over Server-Side Includes (SSI)?
What are the advantages of JSP over JavaScript?
What are the advantages of JSP over Static HTML?
Explain lifecycle of a JSP.
What is a sciptlet in JSP and what is its syntax?
What are JSP declarations?
What are JSP expressions?
What are JSP comments?
What are JSP Directives?
What are the types of directive tags?
What are JSP actions?
Name some JSP actions.
What are JSP literals?
What is a page directive?
What are various attributes Of page directive?
What is a buffer attribute?
What happens when buffer is set to a value “none”?
What is autoFlush attribute?
What is contentType attribute?
What is errorPage attribute?
What is isErrorPage attribute?
What is extends attribute?
What is import attribute?
What is info attribute?
What is isThreadSafe attribute?
What is language attribute?
What is session attribute?
What is isELIgnored Attribute?
What is isScriptingEnabled Attribute?
What is a include directive?
What is a taglib directive?
What do the id and scope attribute mean in the action elements?
What is the function of <jsp:include> action?
What is the difference between include action and include directive?
What is <jsp:useBean> action?
What is <jsp:setProperty> action?
What is <jsp:getProperty> action?
What is <jsp:forward> Action?
What is <jsp:plugin> Action?
What are the different scope values for the JSP action?
What are JSP implicit objects?
What implicit objects are supported by JSP?
What is a request object?
How can you read a request header information?
What is a response object?
What is the out implicit object?
What is the difference between JspWriter and PrintWriter?
What is the session Object?
What is an application Object?
What is a config Object?
What is a pageContext Object?
What is a page object?
What is an exception Object?
What is difference between GET and POST method in HTTP protocol?
How to read form data using JSP?
What are filters?
How do you define filters?
What are cookies?
How cookies work?
How do you set cookies in the JSP?
How to read cookies with JSP?
How to delete cookies with JSP?
How is Session Management done in JSP?
How can you delete a session data?
How can you upload a file using JSP?
Where will be the uploaded files stored?
What is JSP page redirection?
What is the difference between <jsp:forward page = ... > and response.sendRedirect(url)?
What is a hit count for a web page?
How do you impement hit counter in JSP?
How can you implement hit counter to avoid loss of count data with each restart of thh application?
What is auto refresh feature?
How do you implement the auto refresh in JSP?
What is JSTL?
What the different types of JSTL tags are ?
What is the use of <c:set > tag?
What is the use of <c:remove > tag?
What is the use of <c:catch> tag?
What is the use of <c:if> tag?
What is the use of <c:choose> tag?
What is the use of <c:forEach >, <c:forTokens> tag?
What is the use of <c:param> tag?
What is the use of <c:redirect > tag?
What is the use of <c:url> tag?
What are JSTL formatting tags ?
What are JSTL SQL tags?
What are JSTL XML tags?
What is a JSP custom tag?
What is JSP Expression Language?
What are the implicit EL objects in JSP ?
How can we disable EL ?
What type of errors you might encounter in a JSP code?
In JSP page how can we handle runtime exception?
What is Internationalization?
What is Localization?
What is locale?
What is difference between <%-- comment --%> and <!-- comment -->?
Is JSP technology extensible?
How do I include static files within a JSP page?
Can a JSP page process HTML FORM data?
How do you pass control from one JSP page to another?
Can you make use of a ServletOutputStream object from within a JSP page?
What is the page directive is used to prevent a JSP page from automatically creating a session?
How to pass information from JSP to included JSP?
Can we override the jspInit(), _jspService() and jspDestroy() methods?
Why is _jspService() method starting with an '_' while other life cycle methods do not?
A JSP page, include.jsp, has a instance variable "int a", now this page is statically included in another JSP page, home.jsp, which also has an instance variable "int a" declared. What happens when the home.jsp page is requested by the client?
How is scripting disabled?
When do use application scope?
What are the options in JSP to include files?
How does JSP engines instantiate tag handler classes instances?
What’s the difference between JavaBeans and taglib directives?

What is JSP and why do we need it?
What are the JSP lifecycle phases?
What are JSP lifecycle methods?
Which JSP lifecycle methods can be overridden?
How can we avoid direct access of JSP pages from client browser?
What are different types of comments in JSP?
What is Scriptlet, Expression and Declaration in JSP?
What are JSP implicit objects?
Can we use JSP implicit objects in a method defined in JSP Declaration?
Which implicit object is not available in normal JSP pages?
What are the benefits of PageContext implicit object?
How do we configure init params for JSP?
Why use of scripting elements in JSP is discouraged?
Can we define a class in a JSP Page?
How can we disable java code or scripting in JSP page?
Explain JSP Action Elements or Action Tags?
What is difference between include directive and jsp:include action?
What is JSP Expression Language and what are it’s benefits?
What are JSP EL implicit objects and how it’s different from JSP implicit Objects?
How to use JSP EL to get HTTP method name?
What is JSP Standard Tag Library, provide some example usage?
What are the types of JSTL tags?
What is JSP Custom Tag and what are it’s components?
Give an example where you need JSP Custom Tag?
Why don’t we need to configure JSP standard tags in web.xml?
How can we handle exceptions thrown by JSP service method?
How do we catch exception and process it using JSTL?
How do we print “<br> creates a new line in HTML” in JSP?
What is jsp-config in deployment descriptor?
How to ignore the EL expression evaluation in a JSP?
When will Container initialize multiple JSP/Servlet Objects?
Can we use JavaScript with JSP Pages?
How can we prevent implicit session creation in JSP?
What is difference between JspWriter and Servlet PrintWriter?
How can we extend JSP technology?
Provide some JSP Best Practices?

No comments:

Post a Comment