Servlet interview questions

What is the Servlet?
A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request- response programming model.

What are the uses of Servlet?
Typical uses for HTTP Servlets include:
Processing and/or storing data submitted by an HTML form.
Providing dynamic content, e.g. returning the results of a database query to the client.
A Servlet can handle multiple request concurrently and be used to develop high performance system
Managing state information on top of the stateless HTTP, e.g. for an online shopping cart system which manages shopping carts for many concurrent customers and maps every request to the right customer.
How the servlet is loaded?
A servlet can be loaded when:
First request is made.
Server starts up (auto-load).
There is only a single instance which answers all requests concurrently. This saves memory and allows a Servlet to easily manage persistent data.
Administrator manually loads.

What is Servlet interface?

The central abstraction in the Servlet API is the Servlet interface. All servlets implement this interface, either directly or , more commonly by extending a class that implements it.
Servlet

Note: Most Servlets, however, extend one of the standard implementations of that interface, namely javax.servlet.GenericServlet andjavax.servlet.http.HttpServlet.

What is difference between forward() method of RequestDispatcher and sendRedirect() method ?

forward() method
sendRedirect() method
1) forward() sends the same request to another resource.
1) sendRedirect() method sends new request always because it uses the URL bar of the browser.
2) forward() method works at server side.
2) sendRedirect() method works at client side.
3) forward() method works within the server only.
3) sendRedirect() method works within and outside the server.
 
What is the difference between doGet() and doPost()?
doGet()
doPost()
In doGet() the parameters are appended to the URL and sent along with header information
In doPost(), on the other hand will (typically) send the information through a socket back to the webserver and it won't show up in the URL bar.
The amount of information you can send back using a GET is restricted as URLs can only be 1024 characters.
You can send much more information to the server this way - and it's not restricted to textual data either. It is possible to send files and even binary data such as serialized Java objects!
doGet() is a request for information; it does not (or should not) change anything on the server. (doGet() should be idempotent)
doPost() provides information (such as placing an order for merchandise) that the server is expected to remember
Parameters are not encrypted
Parameters are encrypted
doGet() is faster if we set the response content length since the same connection is used. Thus increasing the performance
doPost() is generally used to update or post some information to the server.doPost is slower compared to doGet since doPost does not write the content length
doGet() should be idempotent. i.e. doget should be able to be repeated safely many times
This method does not need to be idempotent. Operations requested through POST can have side effects for which the user can be held accountable.
doGet() should be safe without any side effects for which user is held responsible
This method does not need to be either safe
It allows bookmarks.
It disallows bookmarks.

What is the difference between PrintWriter and ServletOuputStream?
PrintWriter is a character-stream class where as ServletOutputStream is a byte-stream class. The PrintWriter class can be used to write only character-based information where as ServletOutputStream class can be used to write primitive values as well as character-based information.



What is the difference between GenericServlet and HttpServlet?
GenericServlet
HttpServlet
The GenericServlet is an abstract class that is extended by HttpServlet to provide HTTP protocol-specific methods.
An abstract class that simplifies writing HTTP servlets. It extends the GenericServlet base class and provides an framework for handling the HTTP protocol.
The GenericServlet does not include protocol-specific methods for handling request parameters, cookies, sessions and setting response headers.
The HttpServlet subclass passes generic service method requests to the relevant doGet() or doPost() method.
GenericServlet is not specific to any protocol.
HttpServlet only supports HTTP and HTTPS protocalls.

JSP interview questions

Q: What is JSP?
A: JavaServer Pages (JSP) is a technology for developing web pages that support dynamic content which helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with <% and end with %>.
Q: What are advantages of using JSP?
A: JSP offer several advantages as listed below:
Performance is significantly better because JSP allows embedding Dynamic Elements in HTML Pages itself.
JSP are always compiled before it's processed by the server unlike CGI/Perl which requires the server to load an interpreter and the target script each time the page is requested.
JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP also has access to all the powerful Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP etc.
JSP pages can be used in combination with servlets that handle the business logic, the model supported by Java servlet template engines.
Q:What are the advantages of jsp over servlet?
A: The advantage of JSP is that they are document-centric. Servlets, on the other hand, look and act like programs. A Java Server Page can contain Java program fragments that instantiate and execute Java classes, but these occur inside an HTML template file and are primarily used to generate dynamic content. 
Some of the JSP functionality can be achieved on the client, using JavaScript. The power of JSP is that it is server-based and provides a framework for Web application development.
What are the different types of JSP tags?
The different types of JSP tags are as follows:
JSP tags




Q: What is the life cycle of jsp?
A: Life cyle of jsp:
Translation
Compilation
Loading the class
Instantiating the class
jspInit()
_jspService()
jspDestroy()

Q:What jsp life cycle method can I override?
A: You cannot override the _jspService() method within a JSP page. You can however, override the jspInit() and jspDestroy() methods within a JSP page. JspInit() can be useful for allocating resources like database connections, network connections, and so forth for the JSP page. It is good programming practice to free any allocated resources within jspDestroy().
Q: What are implicit objects in jsp?
A: Implicit objects in JSP are the Java objects that the JSP Container makes available to developers in each page. These objects need not be declared or instantiated by the JSP author. They are automatically instantiated by the container and are accessed using standard variables; hence, they are called implicit objects.
Q: Difference Between include Directive and include Action of JSP
This JSP interview question is a continuation of earlier question I just made it a separate one to write answer in clear tabular format.
Include Directive
Include Action
include directive is processed at the translation time
Include action is processed at the run time.
include directive can use relative or absolute path
Include action always use relative path
Include directive can only include contents of resource it will not process the dynamic resource
Include action process the dynamic resource and result will be added to calling JSP
We can not pass any other parameter
Here we can pass other parameter also using JSP:param
We cannot  pass any request or response object to calling jsp to included file or JSP or vice versa
In this case it’s possible.

Q: What are the JSP implicit objects ?
JSP provides 9 implicit objects bydefault.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

Q:
 What is a Declaration?
A:
A declaration declares one or more variables or methods for use later in the JSP source file.A declaration must contain at least one complete declarative statement. You can declare any number of variables or methods within one declaration tag, as long as they are separated by semicolons. The declaration must be valid in the scripting language used in the JSP file.

<%! somedeclarations %>
<%! int i = 0; %>
<%! int a, b, c; %>

Q:
What is a Expression?
A:
An expression tag contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. Because the value of an expression is converted to a String, you can use an expression within text in a JSP file. Like
<%= someexpression %>
<%= (new java.util.Date()).toLocaleString() %>
You cannot use a semicolon to end an expression



Q:
 What is a Scriptlet?
A:
1.   A scriptlet can contain any number of language statements, variable or method declarations, or expressions that are valid in the page scripting language.Within scriptlet tags, you can1.Declare variables or methods to use later in the file (see also Declaration).
2.   Write expressions valid in the page scripting language (see also Expression).
3.   Use any of the JSP implicit objects or any object declared with a <jsp:useBean> tag.
You must write plain text, HTML-encoded text, or other JSP tags outside the scriptlet.
Scriptlets are executed at request time, when the JSP engine processes the client request. If the scriptlet produces output, the output is stored in the out object, from which you can display it.



Q:
Difference between forward and sendRedirect?
A:
When you invoke a forward request, the request is sent to another resource on the server, without the client being informed that a different resource is going to process the request. This process occurs completly with in the web container. When a sendRedirtect method is invoked, it causes the web container to return to the browser indicating that a new URL should be requested. Because the browser issues a completly new request any object that are stored as request attributes before the redirect occurs will be lost. This extra round trip a redirect is slower than forward.