You are on page 1of 4

Question 21): Differentiate between Get and Post method?

Answer:21
Get request sends the data to the server by appending it to URL as part of
query string
For e.g.: http://localhost:8080/HelloWorld/Hello?
name=Nama&surname=marathe
Post request sends the data as par of message body.

!
Use GET:
To retrieve an HTML file or an image file, because only the filename needs
to be sent.
Use POST:
To send a lot of data; for example, POST is well suited for an online
survey, since the length of the query string may exceed 255 characters.
To upload a file, because the file size may exceed 255 characters, and
moreover, the file may be a binary file.
To capture the username and password, because we want to prevent users
from seeing the password as a part of the URL.
Question 22) What are methods of HttpServlet?
Answer:22 The methods of HttpServlet class are :
* doGet() is used to handle the GET, conditional GET, and HEAD
requests
* doPost() is used to handle POST requests
* doPut() is used to handle PUT requests
* doDelete() is used to handle DELETE requests
* doOptions() is used to handle the OPTIONS requests and
* doTrace() is used to handle the TRACE requests
Question 23) What are the advantages of Servlets over CGI programs?
Answer:23 Java Servlets have a number of advantages over CGI and
other API's. They are:
Platform Independence
Java Servlets are 100% pure Java, so it is platform independence. It can
run on any Servlet enabled web server. For example if you develop an web
application in windows machine running Java web server. You can easily
run the same on apache web server (if Apache Serve is installed) without
modification or compilation of code. Platform independency of servlets
provide a great advantages over alternatives of servlets.
Performance
Due to interpreted nature of java, programs written in java are slow. But
the java servlets runs very fast. These are due to the way servlets run on
web server. For any program initialization takes significant amount of
time. But in case of servlets initialization takes place very first time it
receives a request and remains in memory till times out or server shut
downs. After servlet is loaded, to handle a new request it simply creates a
new thread and runs service method of servlet. In comparison to
traditional CGI scripts which creates a new process to serve the request.
This intuitive method of servlets could be use to develop high speed data
driven web sites.
Extensibility
Java Servlets are developed in java which is robust, well-designed and
object oriented language which can be extended or polymorphed into new
objects. So the java servlets takes all these advantages and can be
extended from existing class the provide the ideal solutions.
Safety
Java provides a very good safety features like memory management,
exception handling etc. Servlets inherits all these features and emerged as
a very powerful web server extension.
Secure
Servlets are server side components, so it inherits the security provided by
the web server. Servlets are also benefited with Java Security Manager.
24) What are the lifecycle methods of Servlet?
Answer:24 The interface javax.servlet.Servlet, defines the three life-cycle
methods. These are:
public void init(ServletConfig config) throws ServletException
public void service( ServletRequest req, ServletResponse res) throws
ServletException, IOException
public void destroy()
The container manages the lifecycle of the Servlet. When a new request
come to a Servlet, the container performs the following steps.
1. If an instance of the servlet does not exist, the web container
* Loads the servlet class.
* Creates an instance of the servlet class.
* Initializes the servlet instance by calling the init method. Initialization
is covered in Initializing a Servlet.
2. The container invokes the service method, passing request and response
objects.
3. To remove the servlet, container finalizes the servlet by calling the
servlet's destroy method.
25)What is ServletContext?
Answer:25 ServletContext is an Interface that defines a set of methods that
a servlet uses to communicate with its servlet container, for example, to
get the MIME type of a file, dispatch requests, or write to a log file. There
is one context per "web application" per Java Virtual Machine. (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.)
26) What is the directory Structure of Web Application?
Answer: 26Web components follow the standard directory structure
defined in the J2EE specification.
27) What is meant by Pre-initialization of Servlet?
Answer: 27 When servlet container is loaded, all the servlets defined in
the web.xml file does not initialized by default. But the container receives
the request it loads the servlet. But in some cases if you want your servlet
to be initialized when context is loaded, you have to use a concept called
pre-initialization of Servlet. In case of Pre-initialization, the servlet is
loaded when context is loaded. You can specify <load-on-startup>1</
load-on-startup>
in between the <servlet></servlet> tag.
28) What mechanisms are used by a Servlet Container to maintain
session information?
Answer:28 Servlet Container uses Cookies, URL rewriting, and HTTPS
protocol information to maintain the session.
29) What do you understand by servlet mapping?
Answer:29 Servlet mapping defines an association between a URL pattern
and a servlet. You can use one servlet to process a number of url pattern
(request pattern). For example in case of Struts *.do url patterns are
processed by Struts Controller Servlet.
30) What must be implemented by all Servlets?
Answer:30The Servlet Interface must be implemented by all servlets.
31) What are the objects that are received when a servlets accepts call
from client?
Answer:31 The objects are ServeltRequest and ServletResponse. The
ServeltRequest encapsulates the communication from the client to the
server. While ServletResponse encapsulates the communication from the
Servlet back to the client.

This is just the first part of a complete 6 part serie.

You might also like