Life Cycle of a Servelet
a) A server loads and initializes the servlet by init () method.
b) The servlet handles zero or more client’s requests through service() method.
c) The server removes the servlet through destroy() method.
What is session tracking and how do you track a user session in servlets?- Session tracking is a mechanism that servlets use to maintain state about a series requests from the same user across some period of time. The methods used for session tracking are:
a) User Authentication - occurs when a web server restricts access to some of its resources to only those clients that log in using a recognized username and password.
b) Hidden form fields - fields are added to an HTML form that are not displayed in the client’s browser. When the form containing the fields is submitted, the fields are sent back to the server.
c) URL rewriting - every URL that the user clicks on is dynamically modified or rewritten to include extra information. The extra information can be in the form of extra path information, added parameters or some custom, server-specific URL change.
d) Cookies - a bit of information that is sent by a web server to a browser and which can later be read back from that browser.
What is Server-Side Includes (SSI)?-
Server-Side Includes allows embedding servlets within HTML pages using a special servlet tag. In many servlets that support servlets, a page can be processed by the server to include output from servlets at certain points inside the HTML page. This is accomplished using a special internal SSINCLUDE, which processes the servlet tags. SSINCLUDE servlet will be invoked whenever a file with an. shtml extension is requested. So HTML files that include server-side includes must be stored with an . shtml extension.
e) HttpSession- places a limit on the number of sessions that can exist in memory. This limit is set in the session. maxresidents property.
Q: | What are the common mechanisms used for session tracking? |
A: | Cookies SSL sessions URL- rewriting |
Q: | What is the difference between HttpServlet and GenericServlet? |
A: | A GenericServlet has a service() method aimed to handle requests. HttpServlet extends GenericServlet and adds support for doGet(), doPost(), doHead() methods (HTTP 1.0) plus doPut(), doOptions(), doDelete(), doTrace() methods (HTTP 1.1). Both these classes are abstract. |
|
[ Received from Amit Bhoir ]
|
|
Q: | What is the difference between ServletContext and ServletConfig? |
A: | ServletContext: 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.The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized
ServletConfig: The object created after a servlet is instantiated and its default constructor is read. It is created to pass initialization information to the servlet.
|
What is the difference between doGet() and doPost()?
#
| doGet() | doPost() |
1
| In doGet() the parameters are appended to the URL and sent along with header information. | In doPost(), on the other hand will (typically) send the information through a socket back to the webserver and it won't show up in the URL bar. |
2
| The amount of information you can send back using a GET is restricted as URLs can only be 1024 characters. | You can send much more information to the server this way - and it's not restricted to textual data either. It is possible to send files and even binary data such as serialized Java objects! |
3
| doGet() is a request for information; it does not (or should not) change anything on the server. (doGet() should be idempotent) | doPost() provides information (such as placing an order for merchandise) that the server is expected to remember |
4
| Parameters are not encrypted | Parameters are encrypted |
5
| doGet() is faster if we set the response content length since the same connection is used. Thus increasing the performance | doPost() is generally used to update or post some information to the server.doPost is slower compared to doGet since doPost does not write the content length |
6
| doGet() should be idempotent. i.e. doget should be able to be repeated safely many times | This method does not need to be idempotent. Operations requested through POST can have side effects for which the user can be held accountable. |
7
| doGet() should be safe without any side effects for which user is held responsible | This method does not need to be either safe |
8
| It allows bookmarks. | It disallows bookmarks. |
Creating a servelet :
1.Servlet needs to extend HttpServlet
2. Constructor calls super()
3. Has doGet() and doPost() methods
4. Web.xml has to updated with an entry for the servlet
ex :
<servlet>
<description></description>
<display-name>TestServeletOne</display-name>
<servlet-name>TestServeletOne</servlet-name>
<servlet-class>com.cisco.capacityplanning.servelet.TestServeletOne</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestServeletOne</servlet-name>
<url-pattern>/TestServeletOne</url-pattern>
</servlet-mapping>
url for testing this servlet:
http://localhost:8080/CapacityPlanning/TestServeletOne