Wednesday, 15 June 2016

Servlet / JSP Basics

Servlet :-
Q):- Servlet Life Cycle ?


Q):-  what is differene between httpServlet and generic servlet ?
  • Generic servlet are protocol independent while http Servlet uses http protocol.
  • GenericServlet provides abstract service(ServletRequest, ServletResponse) method to implement which gets called by container whenever it receives request for processing,
  • HttpServlet overrides service method and provides callback on doGet and doPost whenever it receives HTTP request from GET or POST method. It also provides several other method based upon various HTTP methods of sending request e.g. doPut() or doDelete() to handle HTTP PUT and HTTP DELETE request.
  • Another difference between GenericServlet and HttpServlet is that later is a subclass of GenericServlet and inherit properties of GenericServlet.

9) What is servlet collaboration?
When one servlet communicates to another servlet, it is known as servlet collaboration. There are many ways of servlet collaboration:
  • RequestDispacher interface
  • sendRedirect() method etc.

12) Difference between forward() method and sendRedirect() method ?
                forward() method
                  sendRedirect() method
forward() sends the same request
to another resource.
sendRedirect() method sends new request always because it
uses the URL bar of the browser.
forward() method works at server side.
sendRedirect() method works at client side.
3) forward() method works within the server only.
sendRedirect() method works within and outside the
server.


13) What is difference between ServletConfig and ServletContext?

The container creates object of ServletConfig for each servlet whereas object of ServletContext is created for each web application

17) What is filter?

A filter is an object that is invoked either at the preprocessing or postprocessing of a request. It is pluggable.

24) How to create war file?
  • The war file can be created using jar tool found in jdk/bin directory. If you are using Eclipse or Netbeans IDE, you can export your project as a war file.
  • To create war file from console, you can write following code.
  • jar -cvf abc.war *  
  • Now all the files of current directory will be converted into abc.war file.


JSP :--

4)What are the JSP implicit objects ?
JSP provides 9 implicit objects by default. They are as follows:
Object
Type
1) out
JspWriter
2) request
HttpServletRequest
3) response
HttpServletResponse
4) config
ServletConfig
5) session
HttpSession
6) application
ServletContext
7) pageContext
PageContext
8) page
Object
9) exception
Throwable


3)What is difference between hide comment and output comment?

The jsp comment is called hide comment whereas html comment is called output comment. If user views the source of the page, the jsp comment will not be shown whereas html comment will be shown.
5)What is difference between include directive and include action?
                           include directive
                        include action
1) The include directive includes the content at page translation time.
1) The include action includes the content at request
time.
2) The include directive includes the original content of the page so page size increases at runtime.
2) The include action doesn't include the original
content rather invokes the include() method of
Vendor provided class.
3) It's better for static pages.
3) It's better for dynamic pages.


7) How can I implement a thread-safe JSP page? What are the advantages and Disadvantages of using it?

You can make your JSPs thread-safe by having them implement the SingleThreadModel interface. This is done by adding the directive <%@ page isThreadSafe="false" %> within your JSP page.

5) What are the different scope values for the <jsp:useBean> tag?
There are 4 values:
  1. page
  2. request
  3. session
  4. application

scope: represents the scope of the bean. It may be page, request, session or application. The default scope is page.
  • page: specifies that you can use this bean within the JSP page. The default scope is page.
  • request: specifies that you can use this bean from any JSP page that processes the same request. It has wider scope than page.
  • session: specifies that you can use this bean from any JSP page in the same session whether processes the same request or not. It has wider scope than request.
  • application: specifies that you can use this bean from any JSP page in the same application. It has wider scope than session.

17)What is d difference in using request.getRequestDispatcher() and context.getRequestDispatcher()?

  • request.getRequestDispatcher(path) is used in order to create it we need to give the relative path of the resource
  • context.getRequestDispatcher(path) in order to create it we need to give the absolute path of the resource.
24) What are the 3 tags used in JSP bean development?
  1. jsp:useBean
  2. jsp:setProperty
  3. jsp:getProperty
The jsp:useBean action tag is used to locate or instantiate a bean class. If bean object of the Bean class is already created, it doesn't create the bean depending on the scope. But if object of bean is not created, it instantiates the bean.
<jsp:useBean id="obj" class="bean.LoginBean"/>  
  1. <jsp:setProperty property="name" name="obj" value=”s”/>  | <jsp:setProperty property="password" name="obj" value=”s”/>
  2. <jsp:getProperty property="name" name="obj"/>  
8) How can I prevent the output of my JSP or Servlet pages from being cached by the browser?
(OR) How to disable caching on back button of the browser?
  1. <%  
  2. response.setHeader("Cache-Control","no-store");   
  3. response.setHeader("Pragma","no-cache");   
  4. response.setHeader ("Expires", "0"); //prevents caching at the proxy server  
  5. %>   


Some basic of Web Service


Q):- What is “REST”?
Rest:-
  • Restful web services are an architecture style stateless web services
  • It is mostly used for building client-server applications over HTTP.
  • Representation State Transfer is simple,
  • language and plateform independent
  • It relies on the URL
  • In Rest web services are treated as resource and can be accessed and identified by there URL unlikeSOAP web services which were defined by WSDL.
Q):-RESTful Web Service :-
  • Web services written by apply REST Architectural concept are called RESTful web services which focus on System resources and how state of Resource should be transferred over http protocol to a different clients written in different languages
  • In RESTfulweb services http methods like GET, PUT, POST and DELETE can can be used to perform CRUD operations.
Q):- What is Web Service ?
web service is a kind of software that is accessible on the Internet. It makes use of the XML messaging system and offers an easy to understand, interface for the end users.




  • Q):- What is differences between RESTful web services and SOAP web services ?
    Though both RESTful web series and SOAP web service can operate cross platform they are architecturally different to each other, here is some of differences between REST and SOAP:

    1) REST is more simple and easy to use than SOAP

    2) REST uses HTTP protocol for producing or consuming web services while SOAP uses XML.
    3) REST is lightweight as compared to SOAP and preferred choice in mobile devices and PDA's.
    4) REST supports different format like text, JSON and XML while SOAP only support XML.
    5) REST web services call can be cached to improve performance.
6):- Rest uses the HTTP headers to hold meta information and SOAP  runs on HTTP but envelopes the message


Q):- Define SOAP?
SOAP is an XML based protocol to transfer between computers.

Q):- Define WSDL?
It means Web Services Description Language. It is basically the service description layer in the web service protocol stock. The Service Description layer describes the user interface to a web service.




Hashmap in java

How HashMap  works in Java
  • HashMap  works on the principle of hashing,
  • we have put() and get() method for storing and retrieving object from HashMap.
  • When we pass both key and value to put() method to store on HashMap,
  • it uses key object hashcode() method to calculate hashcode and them by applying hashing on that hashcode it identifies bucket location for storing value object.
  • While retrieving it uses key object equals method to find out correct key value pair and return value object associated with that key.
  • HashMap  uses linked list in case of collision and object will be stored in next node of linked list. 
  • Also, HashMap stores both key and value tuple in every node of linked list in the form of Map.Entry object. 

Q):- What will happen if two different objects have the same hashcode?
Since hashcode is same, bucket location would be same and collision will occur in HashMap Since HashMap uses LinkedList to store object, this entry (object of Map.Entry comprise key and value )  will be stored in LinkedList. Great this answer make sense though there are many collision resolution methods available  like linear probing and chaining
How+HashMap+works+in+Java+(1).jpg (276×182)

Q):- How null key is handled in HashMap? Since equals() and hashCode() are used to store and retrieve values, how does it work in case of the null key?
  • The null key is handled specially in HashMap, there are two separate methods for that putForNullKey(V value) and getForNullKey().
  • Later is offloaded version of get() to look up null keys.  
  • Null keys always map to index 0.  
  • This null case is split out into separate methods for the sake of performance in the two most commonly used operations (get and put), but incorporated with conditionals in others.
  • In short, equals() and hashcode()method are not used in case of null keys in HashMap.

    here is how nulls are retrieved from HashMap

       private V getForNullKey() {
       if (size == 0) {
           return null;
       }
       for (Entry<K,V> e = table[0]; e != null; e = e.next) {
           if (e.key == null)
               return e.value;
       }
       return null;
   }