- What is Servlet?
Servlet is a server-side Java program written as per Servlet API specifications invoked against a client request on the Web using HTTP protocol.
- Who executes a Servlet?
Servlet is executed by a Web server.
- What is a Web server?
Any server connected to Internet is known as Web server. Similarly, a Web client also. A Web server can execute any Web communication programs like Servlets, JSP, ASP, CGI/PERL etc.
- Then, what is a Web container?
Web container is a software working on the Web server to execute Web related programs.
- What is Servlet container?
A servlet container is a Web container which executes only Servlets. Likewise, a JSP container executes only JSP programs.
- What are the responsibilities of a Servlet container?
- 1. It is responsible to execute the servlet from loading stage to garbage collection.
- It must call the callback methods whenever required for the smooth execution of the Servlet like service() or doGet() etc.
- It must maintain the life cycle of a servlet by calling init(), service() and destroy() (known as life cycle methods) implicitly.
- To get a Servlet container load a Web server like Tomcat, Weblogic or WebSphere etc.
- What is difference between web server and application server?
Web server, like Tomcat, executes only Web applications like Servlets, JSP, ASP or CGI/PERL. Application server executes applications of any sort like EJBs, JMS, Web services etc. Sometimes an application server, like Weblogic, may execute Web programs also like Servlets. Here, application server is also a Web server. A very small difference exists between them. To put it otherway, a Web server is an application server that executes only Web applications. Similarly, a database server executes a database.
- What is GenericServlet and HttpServlet?
To write a servlet, the Programmer should extend either GenericServlet or HttpServlet.
- What is web.xml file and its importance?
What is deployment descriptor?
When a Servlet is loaded against a Web client request, the container creates a Servlet object, reads the data from web.xml file and as per the data, configures the Servlet.
- web.xml file is a configuration file where a Servlet’s configuration properties can be set.
- web.xml file is known as deployment descriptor as it describes the deployment particulars of a Servlet.
- Programmer can write the initialization parameters (properties specific for a servlet) in the web.xml file. Writing the data in
- Writing data in XML file is known as declarative programming which is very easier than writing in a text file and is mostly used now-a-days by many frameworks and tools.
- Mainly, Programmer uses web.xml file to give alias name to a servlet.
- What is HttpServletRequest and HttpServletResponse in servlets?
HttpServletRequest and HttpServletResponse are two interfaces objects passed to service() method by container. HttpServletRequest job is to receive data from the client and HttpServletResponse job is to send data to client. Know more at HttpServletRequest and HttpServletResponse?
- How sendRedirect() is useful in servlets?
sendRedirect(String file) is a method of HttpServletResponse used to send a file to client.
- What is <servlet-mapping> tag?
It is an XML tag written in web.xml file. It is used to map the actual name of the servlet with alias name. Client is never given the actual name to invoke the servlet, instead given the alias name for security reasons. In the following entry, roses is the alias name for Validation servlet.Java123456789<servlet><servlet-name>abcd</servlet-name><servlet-class>Validation</servlet-class></servlet><servlet-mapping><servlet-name>abcd</servlet-name><url-pattern>/roses</url-pattern></servlet-mapping>
- What is the difference between GET and POST of method attribute of <form> tag?
Observe the usage first.
<form method="get" action="something">
The data sent through using get is not secret as it can be seen in browser’s history by others. It is not same with POST. With GET, the maximum data can be sent is 1 KB only and with POST any amount of data.
Read for more differences 12 Differences – GET vs POST – Which to prefer?.
- What is ServletConfig?
For each Servlet under execution, a ServletConfig object is created by the Servlet container and is used by the Programmer to read the Servlet specific data declared (written) in web.xml in the form of
- ServletConfig is an interface from javax.servlet package.
- The getServletConfig() method of GenericServlet returns an object of ServletConfig.
- The ServletConfig object created by the Web container for a specific Servlet cannot read other servlet’s init-param data.
- ServletConfig object is specific for a servlet. Other way to say, if 100 servlet objects are being executed in the container, 100 ServletConfig objects are also created implicitly used by the container to communicate with the servlet.
- When the container destroys the Servlet object, along with it its corresponding ServletConfig object also destroyed.
- Programmer can make use ServletConfig object to read
tagsof the Servlet.
- <init-param> tag data is known as initialization parameters and are used to initialize the Servlet with some special properties.
- The method of ServletConfig object used to read is "String getInitParameter(String)".
- The ServletConfig data is written within <servlet> tag.
- What is ServletContext?
How to do inter-servlet communication?
We know earlier, the ServletConfig object is specific (or local) to a single servlet and in contrary the ServletContext represents all the servlets. Other way to say, the ServletContext object is a global object that can be accessed by all the servlets under execution of an application (say, banking application). Remember, an application does not represent a single program. An application has a task and to achieve the task number of programs are required systematically integrated.
- ServletContext is an interface from javax.servlet package and its object can be obtained with getServletContext() method of ServletConfig.
- That is, if a servlet would like to create an object of ServletContext, it must come through ServletConfig object only as follows.
Java12ServletConfig config1 = getServletConfig(); // getServletConfig() is a method of GenericServlet inherited by HttpServletServletContext context1 = config1.getServletContext();
- A ServletContext object being global, any servlet can access the data or modify the data or place the data.
- The ServletContext object comes into existence when the container starts and destroyed when the container stops execution.
- The ServletContext object is used to communicate between all servlets (inter-servlet communication)
- Data can be placed with the ServletContext object with "void setAttribute(String, Object)" method and can be retrieved later with "Object getAttribute(String)" method defined in ServletContext itself.
- Another style of placing data is with <context-param> tag declared in web.xml file outside any <servlet> tag. Remember, that <init-param> is written within <servlet> tag.
- What is RequestDispatcher?
- RequestDispatcher is an interface from javax.servlet package.
RequestDispatcher is used to communicate between two servlets (one-to-one) to exchange data in between.
- It comes with only two methods of include() and forward().
- Its object can be obtained either from ServletRequest or ServletContext with the method getRequestDispatcher().
- RequestDispatcher is an interface from javax.servlet package.
- What is the difference between include() and forward() methods?
Both are the methods defined in RequestDispatcher interface. They differ in their functionality (job). include() method gets the response of both the servlets (of calling and called) to the client and the forward() gets only that of called. Client gets the response from the same servlet he requested but in forward(), he gets from a different servlet and is transparent to client. It is clearly discussed in 16 differences between include and forward – Decide which when to use?
- What is the difference of obtaining RequestDispatcher object from ServletRequest and ServletContext?
getRequestDispatcher() method exists in both the interfaces of ServletRequest and ServletContext that returns an object of RequestDispatcher. The RequestDispatcher object obtained differ very slightly in the way we give the address of the other servlet. It is clearly discussed in Difference between RequestDispatcher from ServletRequest and ServletContext
- What is stateless and connectionless protocol?
HTTP protocol is a stateless protocol for the reason it does not maintain a database of client names vs responses delivered. It is so made to save hard disk space on the server.
At the same time, it is also connectionless for the reason when the response is delivered, the connection established earlier is closed. If the same client would like to send another request, it should establish a new connection again. This is so developed to distribute equally the server time and resources to all the clients. More discussed in Stateless and Connectionless HTTP Protocol.
- What is the problem with stateless and connectionless protocol like HTTP?
On a single connection, number of times data cannot be exchanged between Web client and Web server. This is an especially needed phenomenon in e-commerce (online business Web sites) using shopping carts.
- What is session?
The interactive time between client and server within one connection is known as session.
- What is session tracking?
What is session management?
Preserving the data exchanged that can be used in the later part of the program is known as session tracking or session management.
- How many ways session management can be done in Servlets?
There are 4 ways.
- Hidden fields (now-a-days, ignored due to increased network traffic and heavy processing on the server)
- URL rewriting (now-a-days, ignored due to increased network traffic and heavy processing on the server)
- Using HttpSession interface (used in an application other than e-commerce)
- Using Cookies (used especially in e-commerce applications)
- What is HttpSession?
- It is an interface from javax.servlet.http package.
- Overloaded getSession() method of HttpServletRequest returns an object of HttpSession.
- HttpSession interface is used by Programmer to maintain session tracking in an application like Hit count.
- What is a Cookie and how does Cookies work in Servlets?
- In servlets, Cookie is a class from javax.servlet.http package.
- A Cookie constructor can store two string values like item name Lux and quantity 10.
- Two important methods of Cookie are getName() and getValue() to retrieve the two string values.
- Cookie is created on server-side and sent to client for storage. The server can get back the cookies from the client at anytime.
- Another two methods used with cookie programming are addCookie() of HttpServletResponse and getCookies() of HttpServletRequest. Read a shopping cart example.
- What is life cycle of Servlet?
Different states in which a servlet exists between its object creation (by container) and object garbage collection is known as life cycle.
- What are servlet life cycle methods?
The life cycle includes three methods – init(), service() and destroy(). These three methods are known as life cycle methods and servlet should exist in one of these methods in its all the time of execution.
- How to use the Servlet life cycle methods?
Programmer can create objects and initialize variables in init() method, use them in service() method and close the them in destroy() method. For example, open database connection (create Connection and Statement objects) in init(), use the connection in service() and close the connection in destroy() method. See a program on life cycle methods Life Cycle Example with JDBC.
- What is WAR file?
WAR stands for Web Application ARchive file. It is simply a JAR file but contains only Web components like Servlets, JSP, web.xml file, Java Beans for database connections etc.
- What is SingleThreadModel in Servlets?
Java is multithreaded and thus also Servlets. One servlet process loaded can honor number client requests where each client is treated as a separate thread. If you would like the servlet to honor only one client, then implement SingleThreadModel interface to your servlet signature. Know more on What is SingleThreadModel in Servlets?
- What is setContentType() method?
It is a method of ServletResponse inherited by HttpServletResponse. Using this method, the servlet informs the client browser in what format the data sent by the server should be formatted. Fully discussed in response.setContentType() Example.
- What is MIME type?
MIME stands for "Multipurpose Internet Mail Extensions", originally developed for Internet usage. Later the MIME types are used for different purposes by the Programmers like informing the client browser how to dispplay the data sent by server hereto. Know more at What is MIME type in Servlets?
- How the Servlet knows what MIME type is used by client?
Use getMimeType() method defined in ServletContext interface to find what MIME type is used by client.
Pass your comments and suggestions to improve this Servlets Tutorial.