Servlet Interview Questions

Servlet Interview Questions

1 what is a Servlet?

Ans: A Servlet is a server side java program which processes client requests and generates dynamic web content.

2 Explain the architechture of a Servlet?

Ans: The javax.servlet.Servlet interface is the core abstraction which has to be implemented by all servlets either directly or indirectly. Servlet run on a server side JVM ie the servlet container. Most servlets implement the interface by extending either javax.servlet.GenericServlet or javax.servlet.http.HTTPServlet.A single servlet object serves multiple requests using multithreading.

3 what is the difference between an Applet and a Servlet?

Ans: An Applet is a client side java program that runs within a Web browser on the client machine whereas a servlet is a server side component which runs on the web server. An applet can use the user interface classes like AWT or Swing while the servlet does not have a user interface. Servlet waits for client’s HTTP requests from a browser and generates a response that is displayed in the browser.

4 what is the difference between GenericServlet and HTTP Servlet?

Ans: GenericServlet is a generalised and protocol independent servlet which defined in javax.servlet package. Servlets extending GenericServlet should override service () method. Javax.servlet.http.HTTPServlet extends GenericServlet. HTTPServlet is http protocol specific ie it services only those requests thats coming through http. A subclass of HttpServlet must override at least one method of doGet (), doPost (), doPut(), do Delete (), init (), destroy () or getServletInfo ().

5 Explain life cycle of a Servlet?

Ans: On client’s initial request, Servlet Engine loads the servlet and invokes the init () methods to initialize the servlet. The servlet object then handles subsequent client requests by invoking the service () method. The server removes the servlet by calling destry () method.

6 what is the difference between doGet () and doPost ()?

Ans: doGET Method: Using get method we can able to pass 2K data from HTML All data we are passing to Server will be displayed in URL (request string).DoPOST Method: In this method we does not have any size limitation. All data passed to server will be hidden; User cannot able to see this info on the browser.

7 what is the difference between ServletConfig and ServletContext?

Ans: ServletConfig is a servlet configuration object used by a servlet container to pass information to a servlet during initialization. All of its initialization parameters can only be set in deployment descriptor. The ServletConfig parameters are specified for a particular servlet and are unknown to other servlets.The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized. ServletContext is an interface which defines a set of methods which the servlet uses to interact with its servlet container. ServletContext is common to all servlets within the same web application. Hence, servlets use ServletContext to share context information.

8 what is the difference between using get Session (true) and get Session (false) methods?

Ans: get Session (true) method will check whether already a session exists for the user. If a session is existing, it will return the same session object, Otherwise it will create a new session object and return that object.

Get Session (false) method will check for the existence of a session. If a session exists, then it will return the reference of that session object, if not, it will return null.

9 what is meant by a Web Application?

Ans: A Web Application is a collection of servlets and content installed under a specific subset of the server’s URL namespace such as /catalog and possibly installed via a .war file.

10 what is a Server Side Include?

Ans: Server Side Include is a Web page with an embedded servlet tag. When the Web page is accessed by a browser, the web server pre-processes the Web page by replacing the servlet tag in the web page with the hyper text generated by that servlet.

11 what is Servlet Chaining?

Ans: Servlet Chaining is a method where the output of one servlet is piped into a second servlet. The output of the second servlet could be piped into a third servlet, and so on. The last servlet in the chain returns the output to the Web browser.

12 How do you find out what client machine is making a request to your servlet?

Ans: The ServletRequest class has functions for finding out the IP address or host name of the client machine. GetRemoteAddr () gets the IP address of the client machine and getRemoteHost () gets the host name of the client machine.

13 what is the structure of the HTTP response?

Ans: The response can have 3 parts:

1) Status Code – describes the status of the response. For example, it could indicate that the request was successful, or that the request failed because the resource was not available. If your servlet does not return a status code, the success status code,HttpServletResponse.SC_OK, is returned by default.

2) HTTP Headers – contains more information about the response. For example, the header could specify the method used to compress the response body.

3) Body – contents of the response. The body could contain HTML code, an image, etc…

14 what is a cookie?

Ans: A cookie is a bit of information that the Web server sends to the browser which then saves the cookie to a file. The browser sends the cookie back to the same server in every request that it makes. Cookies are often used to keep track of sessions.

15 which code line must be set before any of the lines that use the Print Writer?

Ans: setContentType () method must be set

16 Which protocol will be used by browser and servlet to communicate?

Ans: HTTP

17 Can we use the constructor, instead of init(), to initialize servlet?

Ans: Yes, of course you can use the constructor instead of init(). There’s nothing to stop you. But you shouldn’t. The original reason for init() was that ancient versions of

Java couldn’t dynamically invoke constructors with arguments, so there was no way to give the constructur a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won’t have access to a ServletConfig or ServletContext.

18 How can a servlet refresh automatically if some new data has entered the database?

Ans: You can use a client-side Refresh or Server Push

19 what is the Max amount of information that can be saved in a Session Object?

Ans: As such there is no limit on the amount of information that can be saved in a Session Object. Only the RAM available on the server machine is the limitation. The only limit is the Session ID length (Identifier), which should not exceed more than 4K. If the data to be store is very huge, then it’s preferred to save it to a temporary file onto hard disk, rather than saving it in session. Internally if the amount of data being saved in Session exceeds the predefined limit, most of the servers write it to a temporary cache on hard disk.

20 what is HTTP Tunneling?

Ans: HTTP tunneling is used to encapsulate other protocols within the HTTP or HTTPS protocols. Normally the intra-network of an organization is blocked by a firewall and the network is exposed to the outer world only through a specific web server port that listens for only HTTP requests. To use any other protocol, that by passes the firewall, the protocol is embedded in HTTP and sent as Http Request. The masking of other protocol requests as http requests is HTTP Tunneling.

1 what is a Servlet?

Ans: A Servlet is a server side java program which processes client requests and

generates dynamic web content.

2 Explain the architechture of a Servlet?

Ans: The javax.servlet.Servlet interface is the core abstraction which has to be

implemented by all servlets either directly or indirectly. Servlet run on a server side JVM

ie the servlet container. Most servlets implement the interface by extending either

javax.servlet.GenericServlet or javax.servlet.http.HTTPServlet.A single servlet object

serves multiple requests using multithreading.

3 what is the difference between an Applet and a Servlet?

Ans: An Applet is a client side java program that runs within a Web browser on the

client machine whereas a servlet is a server side component which runs on the web

server. An applet can use the user interface classes like AWT or Swing while the servlet

does not have a user interface. Servlet waits for client’s HTTP requests from a browser

and generates a response that is displayed in the browser.

4 what is the difference between GenericServlet and HttpServlet?

Ans: GenericServlet is a generalised and protocol independent servlet which defined in

javax.servlet package. Servlets extending GenericServlet should override service ()

method. Javax.servlet.http.HTTPServlet extends GenericServlet. HTTPServlet is http

protocol specific ie it services only those requests thats coming through http. A

subclass of HttpServlet must override at least one method of doGet (), doPost (), doPut

(), do Delete (), init (), destroy () or getServletInfo ().

5 Explain life cycle of a Servlet?

Ans: On client’s initial request, Servlet Engine loads the servlet and invokes the init()

methods to initialize the servlet. The servlet object then handles subsequent client

requests by invoking the service () method. The server removes the servlet by calling

destry () method.

6 what is the difference between doGet () and doPost ()?

Ans: doGET Method: Using get method we can able to pass 2K data from HTML All data

we are passing to Server will be displayed in URL (request string).

DoPOST Method: In this method we does not have any size limitation. All data passed

to server will be hidden; User cannot able to see this info on the browser.

7 what is the difference between ServletConfig and ServletContext?

Ans: ServletConfig is a servlet configuration object used by a servlet container to pass

information to a servlet during initialization. All of its initialization parameters can only

be set in deployment descriptor. The ServletConfig parameters are specified for a

particular servlet and are unknown to other servlets.The ServletContext object is

contained within the ServletConfig object, which the Web server provides the servlet

when the servlet is initialized. ServletContext is an interface which defines a set of

methods which the servlet uses to interact with its servlet container. ServletContext is

common to all servlets within the same web application. Hence, servlets use

ServletContext to share context information.

8 what is the difference between using get Session (true) and get Session (false)

methods?

Ans: get Session (true) method will check whether already a session exists for the user.

If a session is existing, it will return the same session object, Otherwise it will create a

new session object and return taht object.

Get Session (false) method will check for the existence of a session. If a session

exists, then it will return the reference of that session object, if not, it will return null.

9 what is meant by a Web Application?

Ans: A Web Application is a collection of servlets and content installed under a specific

subset of the server’s URL namespace such as /catalog and possibly installed via a .war

file.

10 what is a Server Side Include?

Ans: Server Side Include is a Web page with an embedded servlet tag. When the Web

page is accessed by a browser, the web server pre-processes the Web page by

replacing the servlet tag in the web page with the hyper text generated by that servlet.

11 what is Servlet Chaining?

Ans: Servlet Chaining is a method where the output of one servlet is piped into a

second servlet. The output of the second servlet could be piped into a third servlet, and

so on. The last servlet in the chain returns the output to the Web browser.

12 How do you find out what client machine is making a request to your servlet?

Ans: The ServletRequest class has functions for finding out the IP address or host name

of the client machine. GetRemoteAddr () gets the IP address of the client machine and

getRemoteHost () gets the host name of the client machine.

13? what is the structure of the HTTP response?

Ans: The response can have 3 parts:

1) Status Code – describes the status of the response. For example, it could indicate

that the request was successful, or that the request failed because the resource was

not available. If your servlet does not return a status code, the success status code,

HttpServletResponse.SC_OK, is returned by default.

2) HTTP Headers – contains more information about the response. For example, the

header could specify the method used to compress the response body.

3) Body – contents of the response. The body could contain HTML code, an image, etc..

14 what is a cookie?

Ans: A cookie is a bit of information that the Web server sends to the browser which

then saves the cookie to a file. The browser sends the cookie back to the same server

in every request that it makes. Cookies are often used to keep track of sessions.

15 which code line must be set before any of the lines that use the Print Writer?

Ans: setContentType () method must be set

16 Which protocol will be used by browser and servlet to communicate ?

Ans: HTTP

17 Can we use the constructor, instead of init(), to initialize servlet?

Ans: Yes, of course you can use the constructor instead of init(). There’s nothing to

stop you. But you shouldn’t. The original reason for init() was that ancient versions of

Java couldn’t dynamically invoke constructors with arguments, so there was no way to

give the constructur a ServletConfig. That no longer applies, but servlet containers still

will only call your no-arg constructor. So you won’t have access to a ServletConfig or

ServletContext.

18 How can a servlet refresh automatically if some new data has entered the database?

Ans: You can use a client-side Refresh or Server Push

19 what is the Max amount of information that can be saved in a Session Object?

Ans: As such there is no limit on the amount of information that can be saved in a

Session Object. Only the RAM available on the server machine is the limitation. The

only limit is the Session ID length (Identifier), which should not exceed more than 4K.

If the data to be store is very huge, then it’s preferred to save it to a temporary file

onto hard disk, rather than saving it in session. Internally if the amount of data being

saved in Session exceeds the predefined limit, most of the servers write it to a

temporary cache on hard disk.

20 what is HTTP Tunneling?

Ans: HTTP tunneling is used to encapsulate other protocols within the HTTP or

HTTPS protocols. Normally the intra-network of an organization is blocked by a firewall

and the network is exposed to the outer world only through a specific web server port

that listens for only HTTP requests. To use any other protocol, that by passes the

firewall, the protocol is embedded in HTTP and sent as Http Request. The masking of

other protocol requests as http requests is HTTP Tunneling.