servlets tutorial

Servlets Interview Questions and Answers

we have list out latest and updated Servlets interview questions and their answers for fresher’s as well as experienced users.  These interview question covers latest version of Servlets. Java Soft (a company that has since been reintegrated into Sun Microsystems as the Java Software division) finalized Java servlets. This action consolidated the scattered technologies into a single, standard, generic mechanism for developing modular server-side Java code. Servlets were designed to work with both Java-based and non-Java-based servers. Support for servlets has since been implemented in nearly every web server, from Apache to Zeus, and in many non-web servers as well. 

Best Servlets Interview Questions and Answers

Here you can find top best Servlets interview questions and Answers for beginners and professionals,

What is servlet?

Servlets are modules that extend request/response-oriented servers, such as java-enabled web servers.

For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to update a company’s order database.

What are the classes and interfaces for servlets?

There are two packages in servlets and they are javax.servlet and javax.servlet.http.

Javax.servlet contains:

  1. Interfaces Classes
  2. Servlet Generic Servlet
  3. ServletRequest ServletInputStream
  4. ServletResponse ServletOutputStream
  5. ServletConfig ServletException
  6. ServletContext UnavailableException
  7. SingleThreadModel

Javax.servlet.http contains:

  1. Interfaces Classes
  2. HttpServletRequest Cookie
  3. HttpServletResponse HttpServlet
  4. HttpSession HttpSessionBindingEvent

What is the difference between an applet and a servlet?

Servlets are to servers what applets are to browsers. Applets must have graphical user interfaces whereas servlets have no graphical user interfaces.

What is the lifecycle of a servlet?

Each Servlet has the same life cycle:

  1. a) A server loads and initializes the servlet by init () method.
  2. b) The servlet handles zero or more client’s requests through service( ) method.
  3. c) The server removes the servlet through destroy() method.

What is the ServletConfig() and why are using ServletConfig?

This interface is implemented by services in order to pass configuration information to a servlet when it is first loaded.A service writer implementing this interface must write methods for the servlet to use to get its initialization parameters and the context in which it is running. public interface ServletConfig

What is meant by the ServletContext() and use of the method ?

public interface ServletContext The ServletContext interface gives servlets access to information about their environment, and allows them to log significant events. Servlet writers decide what data to log. The interface is implemented by services, and used byservlets. Different virtual hosts should have different servlet contexts.

What is use of parseQueryString ?

Parses a query string and builds a hashtable of key-value pairs, where the values are arrays of strings. The query string should have the form of a string packaged by the GET or POST method. (For example, it should have its key-value pairs delimited by ampersands (&) and its keys separated from its values by equal signs (=).)

Note:

public static Hashtable parseQueryString(String s)

What are the types of servlets.

Genereic Servlets,HttpServlets.

What are the different methods in HttpServlet.

doGet(),doPost(),doHead,doDelete(),deTrace()

What is the difference between GET and POST.

  • doGet() method is used to get information, while doPost( ) method is used for posting information.
  • doGet() requests can’t send large amount of information and is limited to 240-255 characters.

However,

doPost( )requests passes all of its data, of unlimited length.

doGet( ) request is appended to the request URL in a query string and this allows the exchange is visible to the client, whereas a doPost() request passes directly over the socket connection as part of its HTTP request body and the exchange are invisible to the client.

Why do you need both GET and POST method implementations in Servlet?

Ans: A single servlet can be called from different HTML pages, so Different method calls can be possible.

When init() and Distroy() will be called.

init() is called whenever the servlet is loaded for the first time into the webserver.Destroy will be called whenever the servlet is removed from the webserver.

Who is loading the init() method of servlet?

Web server

If you want to modify the servlet,will the Webserver need to be ShutDown.

No

What is the advantage of Servlets over other serverside technologies?

Platform independent, so once compiled can be used in any

What is the uses Sessions?

It’s a part of the SessionTracking and it is for mainting the client state at server side.

What are the advantage of using Sessions over Cookies and URLReWriting?

Sessions are more secure and fast becasue they are stored at serverside. But Sessions has to be used combindly with Cookies or URLReWriting for maintaining the client id that is sessionid at client side. Cookies are stored at client side so some clients may disable cookies so we may not sure that the cookies which we are mainting may work or not but in sessions cookies are disable we can maintain our sessionid using URLReWriting.

In URLReWriting we can’t maintain large data because it leads to network traffic and access may be become slow.Where as in seesions will not maintain the data which we have to maintain instead we will maintain only the session id.

What is Cookies and what is the use of Cookies ?

Cookies are used to get user agents (web browsers etc) to hold small amounts of state associated with a user’s web browsing.Later that infromation read by server

What are cookies and how will you use them?

Cookies are a mechanism that a servlet uses to have a client hold a small amount of stateinformation associated with the user.

1), Create a cookie with the Cookie constructor:

public Cookie(String name, String value)

2), servlet can send a cookie to the client by passing a Cookie object to the addCookie()

method of

HttpServletResponse:

public void HttpServletResponse.addCookie(Cookie cookie)

  1. c) A servlet retrieves cookies by calling the getCookies() method of HttpServletRequest:

public Cookie[ ] HttpServletRequest.getCookie( ).

What is the use of setSecure() and getSecure() in Cookies ?

setSecure

Indicates to the user agent that the cookie should only be sent using a secure protocol (https). This should only be set when the cookie’s originating server used a secure protocol to set the cookie’s value.

public void setSecure(boolean flag)
getSecure:
Returns the value of the 'secure' flag.
public boolean getSecure()

What is meant by Httpsession and what is the use of sessions?

The HttpSession interface is implemented by services to provide an association between an HTTP client and HTTP server. This session, persists over multiple connections and/or requests during a given time period. Sessions are used to maintain state and user identity across multiple

page requests.


HttpSession session = req.getSession(true);

Servelts Interview Questions

Thus, the compilation of these questions includes some of the major points that can be asked during an interview. The Servlets interviewers generally check the base knowledge of a candidate, which can be gained by practicing these questions prior interview.

So if you have finally found your dream job in Servlets coder/or developer but are wondering how to crack the Servlets Interview and what could be the probable Servelts Interview Questions. Every interview is different and the scope of a job is different too. Hope above assist you to get a dream Job. Keep Learn.