Skip to content

Latest commit

 

History

History
90 lines (61 loc) · 4.54 KB

3-j2ee-servlet-handson.md

File metadata and controls

90 lines (61 loc) · 4.54 KB

Go to Home Page


Servlet Hands On


The servlet can be created using

  • Implementing Servlet Interface
  • extending GenericServlet class or
  • extending HttpServlet

We mostly used HttpServlet class, it provides http request specific methods

Directory Structure

Defines where need to put different types of file so that web container get information.

Directory Structure

Ref: https://www.cis.upenn.edu/~matuszek/cit597-2006/Pages/file_structures.html

The Below Example based on creating servlet without any IDE Refer Project Folder


Servlet Creation

Example Code Here Hello.java

Compiling & Deploying

To compile servlet different server provide different jar files

Compile & load class path

Create Deployment Descriptor

  • Deployment Descriptor is an xml file, web container get information about the servlet to be invoked
  • it named web.xml

More Detalis on Deployment Descriptor

Deploying

  • Start the downloaded tomcat server in command line

  • to change the port number, in apache folder conf/server.xml, Change the Connector port = 8080 and replace 8080 by any four digit number instead of 8080

  • copy the project folder to tomcat web apps directory

  • start the tomcat and access the url in browser http://localhost:8080/0-no-ide-servlet/hello

  • Other ways of deploying

    • create war file and paste it into web app directory To create war file run command in project folder

    jar cvf warproject.war *

    Here, -c is used to create file, -v to generate the verbose output and -if to specify the arhive file name.The * (asterisk) symbol signifies that all the files of this directory (including sub directory).


You can use eclipse or any other IDE to do above things easily, you can refer Basic Servlet Project in folder using eclipse IDE

Eclipse Sample Servlet Project


How Servlet Works

As we studied in Servlet Life Cycle if the server requested first time web container load the servlet class, instantiate it, call the init method, other wise call the service method directly. The web container call the destroy method when it need to remove the servlet (time of stopping server, undeploy project)

WebContainer Handling Request
  • maps the request with the servlet in the web.xml file.
  • creates request and response objects for this request
  • calls the service method on the thread
  • The public service method internally calls the protected service method
  • The protected service method calls the doGet method depending on the type of request.
  • The doGet method generates the response and it is passed to the client.
  • After sending the response, the web container deletes the request and response objects. The thread is contained in the thread pool or deleted depends on the server implementation.

Ref: Src code of HttpServlet.java

Tips - to extract war file use jar -xvf warproject.war


Go to Home Page