Advanced Servlet Interview Questions and Answers - Page 3

    1 Votes

39. What is post method usage?

One should normally use METHOD=POST if and only if the form submission may cause changes. There are some situations where one may consider using METHOD=POST even for pure queries too.
If the form data would contain non-ASCII characters, then METHOD=GET is inapplicable in principle, although it may work in practice.

Thus, for a query where the keywords might contain such as accented letters, you have to select among two evils: using METHOD=GET against the rules which restrict the character repertoire to ASCII within it, or using METHOD=POST against the rules which says that it should not be used when the processing is idempotent. The former alternative is probably more dangerous.

40. What is Unavailable exception in servlet API?
It is thrown when a particular servlet is temporarily or permanently not available. For example, if a servlet is not correctly configured , or resource it depends on is not available.
41. What is a status code and what are the categories of status codes?
A status code is the code that the application server sends to the client about the success or failure of a client request. Status codes are grouped into following five categories:
  • Information: Represents messages about the receipt of a request and that the application server is processing the request.
  • Success: Represents a success message, which indicates that the request is successful.
  • Redirection: It indicates that the request is redirected to another page for processing and service.
  • Client error: It indicates that the request has some error and cannot be serviced.
  • Server error: It indicates that the server is unable to fulfill the client request.
42. How to send error message to client?
Error message can be send to client by using methods:
public void sendError(int status_code)
or
public void sendError(int status_code, String message)

Status_code describes the type of error occurred, message describes the error and sends the error response to the client.

43. What will happen if sendError method is called after a response is committed?

if sendError method is called after a response is committed, it will throw an IllegalStateException.

44. How to set the status information about a servlet?

Status information can be set by using methods:

public void setStatus(int status_code)
or
public void setStatus(int status_code, String message)

Status_code represents the status code, message describes the status of the servlet.

45. What are the scope of servlet objects?

There are three scope objects:

  • Web Context (ServletContext) – Accessible from web components within a web context.
  • Session – Accessible from web components handling a request that belongs to the session.
  • Request – Accessible from web components handling the request.

46. What is the format of URL specification?

Format of URL Specification: ProtocolName:IPAddress:PortNumber//WebContextRoot/WebResourceName
Example: http://localhost:8080//WelcomeApplication/WelcomeServlet

47. Explain HTTP request briefly?

An HTTP request has three parts:

  • A request line
  • One or more request headers
  • A message

A request line looks like: GET /WelcomeProject/Home.html HTTP/1.1

  • 1st token is the name of the HTTP method, which is GET in this case.
  • 2nd token is the URI, that gives information about the location of the resource to be gotten.
  • 3rd token is the version of HTTP to be used.

48. Explain HTTP response briefly?

An HTTP response has three parts:

  • A response line
  • One or more response headers
  • A message

A response line looks like: HTTP/1.1 200 OK

  • 1st token is the HTTP version.
  • 2nd token is one of the many predefined status codes. 
  • 3rd token is an English description of the status code.

49. What are the tasks performed by web container when a request comes for a servlet?

Tasks performed by web container are:

  • It loads the servlet class.
  • It instantiates the servlet.
  • It initializes the servlet instance.
  • It passes request to the servlet instance.
  • Finally it sends response to the client.

50. What are the features of CGI?

  • CGI is developed in C, C++, VB and Perl language.
  • Resource intensive and inefficient.
  • It is difficult to maintain, non-scalable and non-manageable.
  • It is application and platform specific.

51. What are the differences between sendRedirect and forward method?

sendRedirect:

  • It is a method of HTTPServletResponse interface.
  • It sends a redirect response back to the client’s browser. The browser will normally interpret this response by initiating a new request to the redirect URL given in the response.
  • Because a new request is being submitted all previous stored in the request will be unavailable.
  • This method is useful for redirecting to pages on other servers and domains.
  • A browser reload of the second URL will not repeat the original request, but will rather fetch the second URL. The back and forward button will work.
  • Redirect is always slower than a forward, since it requires a second browser request.

Forward:

  • It is a method of RequestDispatcher interface.
  • It does not involve the client’s browser. It just takes browser’s current request, and hands it off to another servlet/JSP than they originally called.
  • Forward is performed internally by the servlet.
  • This method is useful when communicating between server resources.
  • Browser is completely unaware that it has taken place, so its original URL remains intact. So, back and forward button of web browser will not work.
  • No network round trip is required to the server, so it is faster.

52. What is a web component?

It is a software entity that runs on a web server, providing with the capabilities needed for dynamically handling client requests and generating web presentation content.

53. What is a web server?

A computer program responsible for accepting HTTP requests and serving them HTTP responses along with data, which usually are web pages such as HTML pages. They are repository of web pages.

54. What are the features of web server?

Features of web server:

  • Web server program works by accepting HTTP requests from the client, and providing an HTTP response to the client.
  • It is a host for all server side scripting like servlet,JSP,ASP,CGI etc.

55. What is an application server?

An application server is a software engine that delivers applications to clients. Moreover, an application server handles most of the business logic and data access of the application.

56. What are the features of application server?

Features of application server:

  • Application servers enable applications to intercommunicate with dependent applications, like web servers, database management systems and chart programs.
  • Portals are common application server mechanism by which a single point of entry is provided to multiple devices.
  • EJB’s are hosted in an application server.

57. What is the difference between GenericServlet and HTTPServlet class?

  • GenericServletIt is used to code generic , protocol independent servlets. It is used as the basis for protocol specific subclasses.
  • HTTPServletHTTPServlet class operates over the HTTP protocol by providing methods such as doGet and doPost for handling HTTP specific services.

58. What are the tasks performed by doGet and doPost method?

  • Extracts client side information from HTTP request.
  • Sets and gets attributes to/from scope objects.
  • Performs some business logic or access database.
  • Forwards the request to other web components such as Servlets or JSP.
  • Populates HTTP response message and sent it to client.
Page 3 of 4

Popular Videos

communication

How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

JSP Interview Questions and Answers
Servlet Interview Questions and Answers
EJB Interview Questions and Answers
Spring Interview Questions and Answers