Gadgets

Wednesday, 27 January 2016

Hibernate Interview Questions and Answers

1. What is Hibernate?
Hibernate framework simplifies the development of java application to interact with the database. Hibernate is an open source and lightweight ORM (Object Relational Mapping) tool.

2. What is ORM?
ORM stands for Object Relational Mapping. ORM tool simplifies the data creation, data manipulation and data access. It is a programming technique that maps the object to the data stored in the database.

Monday, 25 January 2016

JSP Interview questions and answer

1. What is JSP?
JSP stands for Java Server Pages. JSP is java server side technology to create dynamic web pages
JSP file compiled by JSP container and convert it into servlet file.

Sunday, 24 January 2016

Complete Servlet interview questions and answers

1. What is the different between web server and application                 server?

Web server

Application server
Web server provides environment to run Web Applications

Application server provides environment to run Enterprise Applications

Web server supports only for web related Technologies like Servlet, Jsp, HTML etc.

Application server supports for any technology from java like Servlet, jsp, EJB, and Jms components etc.

Example: Tomcat, Oracle HTTP Server
Example: Weblogic, WebSphere, Jboss


Application server provides additional features along with WebServer

2. What is the different between Web Application and Enterprise         Application?

Web Application:
Web application can be developed by only web related technologies like servlet, jsp, HTML etc.
Web application run on server side and provides static and dynamic content to the client.
Example: library management system
Enterprise Application:
Enterprise Application can be developed by any technology from java like Servlet, jsp, EJB, and Jms components etc.
Example: Banking applications and Telecom based applications

3. What is the difference between GET and POST method?

GET
POST

We can send limited amount of data because data is send in header.
We can send large amount of data because data is send in body.

GET method is not secure because data is exposed in the URL bar

POST method is secure because data is not exposed in the URL bar

we can easily bookmark it

We can’t bookmark it
GET is the default HTTP method 

WE can specify the POST method
It is more efficient and idempotent
It is less efficient and non- idempotent


4. What is web application directory structure?

 



5. What is a Servlet?

Servlet is server side technology is used to create web application, Servlet is an interface that must be implemented for creating any servlet.

Servlet is an API that provides many interfaces and classes.

public abstract class GenericServlet implements Servlet, ServletConfig, Serializable
public abstract class HttpServlet extends GenericServlet
  



6. What is the difference between GenericServlet and HttpServlet?
GenericServlet
HttpServlet

GenericServlet is protocol independent implementation of Servlet interface
HttpServlet is HTTP protocol specific implementation

javax.servlet.GenericServlet;
javax.servlet.http.HttpServlet;

GenericServlet implements Servlet
HttpServlet extends GenericServlet

It is contain ServletRequest ,ServletResponse  intefaces
It contain HttpServletRequest ,HttpServletResponse interfaces


7. What is the Servlet life cycle?
A servlet life cycle can be defined as the entire process from its creation till the destruction. The following are the paths followed by a servlet.
init() method is used initialize the servelt. It is called only once when the servlet is first created.
service() method is used process the client request. Each time service() method is called for every client request, create one thread for one request.
destroy() method is used to terminate the servlet. destroy() method is called only once at the end of the life cycle of a servlet.


8. What is the difference between PrintWriter and ServletOutputStream?
PrintWriter

ServletOutputStream
PrintWriter is a character-stream class
ServletOutputStream is a byte-stream class

The PrintWriter class can be used to write only character-based information
ServletOutputStream class can be used to write primitive values as well as character-based information

PrintWriter out =response.getWriter();             out.println(“Hello”);  
        ServletOutputStream
out = response.getOutputStream();  


9. What is the purpose of RequestDispatcher Interface?
The RequestDispacher interface provides the facility of dispatching the request to another resource it may be html, jsp or another servlet in same application. We can also include the content of another resource to the response.

10. How do we call one Servlet from another Servlet?
We can use RequestDispatcher forward() method to forward the processing of a request to another servlet. If we want to include the another servlet output to the response, we can use RequestDispatcher include() method.
11. What are the major tasks of Servlet Container?
Multithreading Support: Container creates new thread for every request to the servlet and provides them request and response objects to process.
JSP Support: JSPs doesn’t look like normal java classes but every JSP in the application is compiled by container and converted to Servlet and then container manages them like other servlets.
Lifecycle and Resource Management: Servlet Container takes care of managing the life cycle of servlet. From the loading of servlets into memory, initializing servlets, invoking servlet methods and to destroy them.
Communication Support: Servlet Container provides easy way of communication between web client (Browsers) and the servlets and JSPs. Because of container, we don’t need to build a server socket to listen for any request from web client, parse the request and generate response. All these important and complex tasks are done by container and all we need to focus is on business logic for the applications.
12. What are the advantages of Servlet over CGI?
Servlet
CGI (Common Gateway Interface)

Servlets can run on any operating system because Servlets are platform-independent

We need to recompile the programs in the new operating system
Performance is significantly better
Performance is not good

Servlets share the memory space of the JVM
CGI create their own process when they are executed


Servlets run on the JVM
CGI programs run as separate processes

The full functionality of the Java class libraries is available to a servlet. It can communicate with applets, databases, or other software via the sockets and RMI mechanisms


13. What is doGet() and doPost() methods?
doGet() method:
A GET request results from a normal request for a URL or from an HTML form that has no METHOD specified and it should be handled by doGet() method.
This method should be used to get data from server.
doPost() method:
A POST request results from an HTML form that specifically lists POST as the METHOD and it should be handled by doPost() method.
This method should be used to get data from server.

14. What is difference between ServletConfig and ServletContext?
ServletConfig
ServletContext

ServletConfig is a unique object per servlet

ServletContext is a unique object for complete application

ServletConfig is used to provide init parameters to the servlet
ServletContext is used to provide application level init parameters that all other servlets can use

We can’t set attributes in ServletConfig object
we can set attributes in ServletContext that other servlets can use in their implementation

javax.servlet.ServletConfig;
javax.servlet.ServletContext;


15. What are the Cookies?
Cookies are text files stored on the client computer and they are kept for various information tracking purpose. Java Servlets transparently supports HTTP cookies. Cookie works at client side.
Create Cookie                                                                                                                               Cookie ck=new Cookie(“key”, “value”);                                                                                                     To read Cookie we call getCookies( ) method of HttpServletRequest.
16. What is the session?
Session provides a way to identify a user across more than one page request. The session persists for a specified time period, across more than one connection or page request from the user. Session is work at server side.
Create session
HttpSession session=request.getSession();  
session.setAttribute("name",n);

17. What are the HttpServletRequest and HttpServletResponse?
HttpServletRequest:
When a browser sends requests for a web page, HttpServletRequest is to receive data sent by client

HttpServletResponse:
When a Web server responds to the client request, HttpServletResponse is to send the response data to the client.

18. Can we call a jsp file from the servlet?
Yes, we can call a jsp file from the Servlet by using RequestDispatcher interface
RequestDispatcher rd=request.getRequestDispatcher("home.jsp");  
rd.forward(request,response);
19. How to read form data in Servlet?
getParameter(): we call request.getParameter() method to get the value of a form parameter.
getParameterValues(): Call this method if the parameter appears more than once and returns multiple values, for example checkbox.
getParameterNames(): Call this method if you want a complete list of all parameters in the current request.

20. What are the attribute in Servlets and uses?
Attribute are set, get or remove and their scope request, session or application scope. Servlet attributes are used to communicate one servlet to other servlet.

21. What are the annotations used in Servlet?
There are mainly 3 annotations we use in servlet.
@WebServlet : use for servlet class.
@WebListener : use for listener class.
@WebFilter : use for filter class.

22. When servlet object is created and who is responsible to create the servlet object?
Servlet object create at the time of first request and web container or servlet container is responsible to create servlet object.

23. Difference between forward() method and sendRedirect() method ?
forward()
sendRedirect()

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 client side

forward() is handled internally by the container
sednRedirect() is handled by browser

24. What is the filter? And types of filters in servlet?
Filter is an object that is invoked and it is pluggable java components the we can use to preprocessing or post processing of the request.
Various types of filters:
Authentication Filters.                                                                                                                       Data compression Filters.                                                                                                                       Encryption Filters.                                                                                                                                   Filters that trigger resource access events.                                                                                       Image Conversion Filters.                                                                                                             Logging and Auditing Filters.

Friday, 22 January 2016

Collection Framework interview questions and answers

Collection framework topic is most important for interview, most of the interviewers compulsory ask at least few questions from collection framework. Collection interface most important topic in core java, we use collection topic in most of the java applications.
1) What are the limitation of object Arrays?

Thursday, 21 January 2016

Explain about Stack with an example

Stack is a child class of Vector. Stack is Last In First Out (LIFO).
Heterogeneous Objects are allowed.
Insertion order is preserved.
Duplicate Objects are allowed.
Null insertion is possible.

Constructor:
Stack s=new Stack();

Methods in Stack:
push() – To insert object into stack
pop() – To remove object
peek() – To return object without remove
boolean empty() – Return true if the stack is empty
int search() – Return offset, if the element is available otherwise return-1

import java.util.*;
public class StackDemo {
               public static void main(String[] arg) {                  
                               Stack s=new Stack();
                               s.push("a");
                               s.push("b");
                               s.push(null);//null insertion allowed
                               s.push("c");
                               s.push("a");//duplicates are allowed        
                               System.out.println(s); // [a, b, null, c, a]
                               s.pop();
                               System.out.println(s); //[a, b, null, c]
                               System.out.println(s.search("c")); // 1 (offset is 1)                            
               }
}