How do servlets work:
We introduced the HTTP protocol  for communication between web clients  and web servers. We listed some of the important  HTTP commands, such as GET and  POST and  gave some idea  of the standard responses to these. We can  now look 'behind the scenes' at what happens on the server side  after the arrival of an HTTP request in order to construct the HTTP response.
We have  discussed static  web  pages where  the content does not change, unless the whole page is replaced. When a browser requests a static  web  page, using  a GET command, the web  server responds with HTTP header information  (see Unit 9 for details)  followed by the HTML content of the web  page, together with any associated files, such as  multimedia  files or applet code.
When we want to do something more  complex like using  the web  server to process some data from an online form or to construct a web  page  dynamically in response to some specific request, the web server can  use  one or more servlets. The servlet runs on the web  server in a software environment known as  a servlet container. The Tomcat system, which was  developed as  part  of the open source Apache Jakarta project, is freely available software that provides servlet  containers and  a web  server.
The servlet code accepts the HTTP request and  any associated data, and  constructs an appropriate HTTP response which is then  sent  back to the web  client.  If the request is successful, the response will mainly consist of a web  page, possibly constructed dynamically by the servlet.  In this way, servlets can  return web  pages that are adapted to the information  supplied by the user,  or containing information retrieved from other sources, such as  a database.
Figure shows  an example of a web  client sending an HTTP request to a web  server. The web server is running  one or more Java servlets and  also interacts with a database server and  a remote web  server. Using  the information from these sources, the servlets construct an HTTP response, consisting of header information,  normally followed by a web  page.
Coding of Java servlets has  some similarities  to the coding of applets although, of course, the environments for running  servlets and  applets respectively are  quite different.  A servlet  is written as  a class that extends the abstract HttpServlet class from the package javax.servlet.http. This package also  defines the interfaces HttpServletRequest and  HttpServletResponse. The HttpServlet class has  a number of standard methods, which can  be overridden to define the action of the servlet on receipt of various  HTTP requests. Table  3 shows some of the important  methods of this class.
To write a servlet class, you must  extend the HttpServlet class. Although  this is an abstract class, it contains implementations for all its methods, but many  of them  do nothing  or would simply cause the web server to return an error status to the web client. Hence, in most  cases, you must  override  the methods you need, to ensure that the servlet  responds to expected HTTP requests.

The init and  destroy methods are  invoked  once each during  the execution of the servlet,  to initialize and  clean  up respectively - this is similar to the corresponding methods for applets. If you need initialization or clean-up you must override  the relevant method. The destroy method would typically be  used to close any open files or streams or to terminate any running  threads.
Usually, the implementation of the service method is exactly  what is needed and  you need not override  it. Its main function  is to pass HTTP requests to one  of a series of helper  methods, each corresponding to one  of the HTTP commands. So, for example, when a GET request is received, the service method passes this, together with any other data received, to the doGet method. Similarly, the doPost method handles any POST requests, and  so on. The bad news  is that the default  implementation of most  of the various do methods simply returns  an error status as the response to be sent  to the web client. You must  override  any of these methods required to ensure that the servlet  can respond appropriately to any expected HTTP requests. An important  exception to this is the doOptions method, which is fully implemented - it constructs the response to the HTTP OPTIONS request and  causes the web  server to indicate which HTTP requests it can  respond to.
 
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your How do servlets work homework and assignments? Live How do servlets work experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer How do servlets work homework help, java assignment help and How do servlets work projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours