SlideShare a Scribd company logo
1 of 46
06/10/17 jit 1
An
Introduction to
Web Technology
Presented At:
JAHANGIRABAD INSTITUTE OF TECHNOLOGY
Some historical details
īŽ Start of standard groups
īŽ TCP
īą handles conversion between messages and streams packets
īŽ IP
īą handles addressing of packets across networks
īŽ TCP/IP
īą enables packets to be sent across multiple networks using multiple
standards
īŽ Telnet
īą One of the earliest standards for exchanging transmission, directly
connect accounts on different systems.
īŽ SMTP
īą specifies another way of direct connection
Some historical details
īŽ MIME
īą Extension to the SMTP Protocol which supports the
exchange of richer data files such as audio-, video-, and
images data.
īŽ FTP
īą (1973) supports file transfer between Internet sites
and allows a system to publish a set of files by hosting an
FTP sever
innovation īƒ  permits anonymous users to transfer files
īŽ Archie
īą Late 1980‘s distributed file system based on FTP
īŽ Gopher
īą First simple system, providing GUI
Core Web Technologies
īŽ HTTP(HyperText Transfer Protocol)
īŽ generic, stateless protocol
īŽ governs the transfer of files across a network
īŽ developed at CERN (Central European Research
Network), they also came up with the name WWW,
later W3C
īŽ supports access to SMTP,FTP and other protocols
īŽ was designed to support hypertext
Core Web Technologies
īŽ Exchanged information, can be static or dynamic
īŽ Every resource, accessible over the Web has a
URL(Uniform resource locator)
īŽ HTTP mechanism is based on client/server model
typically using TCP/IP sockets
Core Web Technologies
īŽ since Version 1.1 HTTP requires servers to support
persistent connections, to minimize overhead associated
with opening and closing connections.
īŽ Typical methods on the server side are:
īą OPTIONS
ī‚§ send information about the communication options
īą GET
ī‚§ retrieve document or document produced by a program
īą POST
ī‚§ Append or attach information
īą PUT
ī‚§ Store information
īą DELETE
ī‚§ Delete the resource indicated in the request
Core Web Technologies
īŽ Another limitationīƒ  HTTP is stateless
īą Does not provide storing of information between requests
īą No indication of any relationship between two different
requests
īƒ  cookies, small data structures that a web server
requests the HTTP client to store on the local machine,
are used to maintain state information
e.g. cookies store recently view items on a web shop
06/10/17 jit 8
HTTP
īƒ˜HTTP stands for HyperText Transport Protocol, and is the network protocol used over the web. It
runs over TCP/IP.
īƒ˜HTTP uses a request/response model.Client sends an HTTP request, then the server gives back the
HTTP response that the browser will display (depending on the content type of the answer).
īƒ˜If the response is an HTML file, then the HTML file is added to the HTTP response body.
īƒ˜An HTTP response includes : status code, the content-type (MIME type), and actual content of the
response.
īƒ˜A MIME type tells the browser what kind of data the response is holding.
īƒ˜URL stands for Uniform Resource Locator: starts with a protocol, then a server name, optionally
followed by a port number, then the path to the resource followed by the resource name. Parameters
may appear at the end, separated from the rest by a ? .
06/10/17 jit 9
HTTP METHODS
METHOD DESCRIPTION
HEAD Asks for the response identical to the one that would correspond to a GET request, but
without the response body. This is useful for retrieving meta-information written in
response headers, without having to transport the entire content.
GET means retrieve whatever data is identified by the URI
POST Submits data to be processed (e.g., from an HTML form) to the identified resource. The
data is included in the body of the request.
DELETE Deletes the specified resource.
TRACE Echoes back the received request, so that a client can see what intermediate servers are
adding or changing in the request.
OPTIONS Returns the HTTP methods that the server supports for specified URL. This can be used
to check the functionality of a web server by requesting '*' instead of a specific
resource.
CONNECT Converts the request connection to a transparent TCP/IP tunnel, usually to facilitate SSL-
encrypted communication (HTTPS) through an unencrypted HTTP proxy.[3]
PUT Uploads a representation of the specified resource.
06/10/17 jit 10
HTTP METHODS
HTTP servers are required to implement at least the GET and HEAD methods [4] and, whenever
possible, also the OPTIONS method
Safe methods
Some methods (for example, HEAD, GET, OPTIONS and TRACE) are defined as safe, which means
they are intended only for information retrieval and should not change the state of the server.
Idempotent methods
Some http methods are defined to be idempotent, meaning that multiple identical requests should
have the same effect as a single request. Methods PUT, DELETE, GET, HEAD, OPTIONS and
TRACE, being prescribed as safe, should also be idempotent. HTTP is a stateless protocol.
By contrast, the POST method is not necessarily idempotent, and therefore sending an identical
POST request multiple times may further affect state or cause further side effects.(like updating
inserting records in database multiple times).
06/10/17 jit 11
DIFFERENCE BETWEEN GET AND POST
GET:
GET parameters are passed on the URL line, after a ?. The URL size is limited.
The parameters appear in the browser URL field, hence showing any password to the
world...
GET is supposed to be used to GET things (only retrieval, no modification on the
server).
GET processing must be idempotent.
A click on a hyperlink always sends a GET request.
GET is the default method used by HTML forms. Use the method=”POST” to change
it.
POST:
POST has a body.
In POST requests, the parameters are passed in the body of the request, after the
header. There is no size-limit. Parameters are not visible from the user.
POST is supposed to be used to send data to be processed.
POST may not be idempotent and is the only one.
IDEMPOTENT : Can do the same thing over and over again, with no negative side
effect.
06/10/17 jit 12
WHAT IS WEB CONTAINER?
A Web application runs within a Web container of a Web server. The Web
container provides the runtime environment through components that
provide naming context and life cycle management. Some Web servers may
also provide additional services such as security and concurrency control.
Web applications are composed of web components and other data such as
HTML pages. Web components can be servlets, JSP pages created with the
JavaServer Pagesâ„ĸ technology, web filters, and web event listeners. These
components typically execute in a web server and may respond to HTTP
requests from web clients. Servlets, JSP pages, and filters may be used to
generate HTML pages that are an application’s user interface.
06/10/17 jit 13
WHAT IS JAVA SERVLET?
Servlets are Java classes that process the request dynamically and generate response
independent of the protocol. Servlets are defined in Java Servlet API specification. Latest
Servlet API specification available is 2.5.
Servlets are server side Java programs which extends the functionality of web server. Servlet are
protocol independent that means it can be used virtually with any protocol to process the
request and generate the response. However in practice Servlets are used to process the
HTTP requests and generate the HTML response.
06/10/17 jit 14
STRUCTURE OF A WEB APPLICATION
Web Applications use a standard directory structure defined in the
Servlet specification. When developing web applications on J2EE
platform, you must follow this structure so that application can be
deployed in any J2EE compliant web server.
A web application has the directory structure as shown in figure.
The root directory of the application is called the document root. Root
directory is mapped to the context path.
Root directory contains a directory named WEB-INF. Anything under
the root directory excepting the WEB-INF directory is publically
available, and can be accessed by URL from browser.
WEB-INF directory is a private area of the web application, any files
under WEB-INF directory cannot be accessed directly from
browser by specifying the URL like http://somesite/WEB-
INF/someresource.html. Web container will not serve the content
of this directory. However the content of the WEB-INF directory
is accessible by the classes within the application.
06/10/17 jit 15
WEB-INF DIRECTORY
WEB-INF directory contains
īŽ WEB-INF/web.xml deployment descriptor
īŽ WEB-INF/classes directory
īŽ WEB-INF/lib directory
CLASSES- directory is used to store compiled servlet and other classes of the application.
LIB - directory is used to store the jar files. If application has any bundled jar files, or if application
uses any third party libraries such as log4j which is packaged in jar file, than these jar files should
be placed in lib directory.
All unpacked classes and resources in the /WEB-INF/classes directory, plus classes and resources in
JAR files under the /WEB-INF/lib directory, are made visible to the containing web application.
06/10/17 jit 16
SERVLET LIFE CYCLE
Servlets are managed components. They are managed by web container. Of the various
responsibilities of web container, servlet life cycle management is the most important one.
A servlet is managed through a well defined life cycle that defines how it is loaded, instantiated ad
initialized, handles requests from clients and how it is taken out of service.
The servlet life cycle methods are defined in the javax.servlet.Servlet interface of the Servlet API that
all Servlets must implement directly or indirectly by extending GenericServlet or HttpServlet
abstract classes. Most of the servlet you develop will implement it by extending HttpServlet class.
The servlet life cycle methods defined in Servlet interface are init(), service() and destroy().
The signature of this methods are shown below.
public void init(ServletConfig config) throws ServletException
public void service(ServletRequest req, ServletResponse res) throws ServletException,
IOException
public void destroy()
06/10/17 jit 17
SERVLET LIFE CYCLE - STEPS
The servlet life cycle consists of four steps, instantiation, initialization, request handling and end of
service.
Loading and instantiation
During this step, web container loads the servlet class and creates a new instance of the servlet. The
container can create a servlet instance at container startup or it can delay it until the servlet is
needed to service a request.
Initialization
During initialization stage of the Servlet life cycle, the web container initializes the servlet instance by
calling the init() method. The container passes an object implementing the ServletConfig interface
via the init() method. This configuration object allows the servlet to access name-value
initialization parameters from the web application’s deployment descriptor (web.xml) file. The
container guarantees that the init() method will be called before the service() method is called.
The init() method is typically used to perform servlet initialization, creating or loading objects that are
used by the servlet in the handling of its requests.
The init() method is commonly used to perform one time activity. One of the most common use of
init() method is to setup the database connection or connection pool.
06/10/17 jit 18
SERVLET LIFE CYCLE - STEPS
Request handling
After a servlet is properly initialized, it is ready to handle the client requests. If the container has a
request for the servlet, it calls the servlet instance’s service() method. The request and response
information is wrapped in ServletRequest and ServletResponse objects respectively, which are
then passed to the servlet's service() method. In the case of an HTTP request, the objects
provided by the container are of types HttpServletRequest and HttpServletResponse.
Service() method is responsible for processing the incoming requests and generating the response.
End of service
When the servlet container determines that a servlet should be removed from service, it calls the
destroy () method of the Servlet  instance to allow the servlet to release any resources it is using.
The servlet container can destroy a servlet because it wants to conserve some memory or server
itself is shutting down.
Destroy() method is used to release any resources it is using. The  most common use of destroy()
method is to close the database connections.
06/10/17 jit 19
SETTING UP SERVLET DEVELOPMENT
ENVIRONMENT
List of required softwares:
īŽ JAVA 1.5 or 1.6
īŽ Tomcat 5.5.16
First of all you need the Java software development kit 1.5 or 1.6 (JAVA SDK) installed.
Checking if JAVA is already installed in your system
06/10/17 jit 20
SETTING UP SERVLET DEVELOPMENT
ENVIRONMENT
Set up the JAVA_HOME environment variable
Once the JAVA SDK is installed follow this step to set the JAVA_HOME environment
variable. If JAVA_HOME variable is already set, you can skip this step.
06/10/17 jit 21
SETTING UP SERVLET DEVELOPMENT
ENVIRONMENT
06/10/17 jit 22
SETTING UP SERVLET DEVELOPMENT
ENVIRONMENT
In the variable name filed, enter JAVA_HOME. Specify the path to root directory of JAVA
installation in variable value field and click OK button. Now JAVA_HOME will appear under
user variables.
Next you need to add bin directory under the root directory of JAVA installation in PATH
environment variable.Select the PATH variable from System variables and click on Edit button.
Add: ;%JAVA_HOME%bin; at the end of variable value field and click OK button.
06/10/17 jit 23
INSTALLING TOMCAT
Tomcat is an opensource web container. it is also web container reference implementation.
Download the latest version of tomcat from this URL . Download the jakarta-tomcat-
5.0.28.tar.gz and extract it to the directory of your choice.
Note: This directory is referred as TOMCAT_HOME in other tutorials.
That’s all, tomcat is installed.
Starting and shutting down Tomcat
To start the tomcat server, open the command prompt, change the directory to TOMCAT
HOME/bin and run the startup.bat file. It will start the server.
>startup To shut down the tomcat server, run the shutdown.bat file. It will stop the server.
>shutdown Verifying Tomcat installation
To verify that tomcat is installed properly, start the server as explained above, open the web
browser and access the following URL.
http://localhost:8080/index.jsp
It should show the tomcat welcome page, if tomcat is installed properly and server is running.
06/10/17 jit 24
INSTALLING TOMCAT
Setting up the CLASSPATH
Now you need to create a new environment variable CLASSPATH if it is not already set. We
need to add the servlet-api.jar into the CLASSPATH to compile the Servlets. Follow the same
steps as you did to create the JAVA_HOME variable. Create a new variable CLASSPATH under
system variables. Add TOMCAT_HOME/lib/servlet-api.jar in variable value field.
Note: here TOMCAT_HOME refers to the tomcat installation directory.
06/10/17 jit 25
SERVLET API CLASSES AND INTERFACES
Servlet API is specified in two packages: javax.servlet and javax.servlet.http. The classes and
interfaces in javax.servlet are protocol independent, while the classes and interface in
javax.servlet.http deals with specialized HTTP Servlets.
Some of the classes and interfaces in the javax.servlet.http package extend those specified in
javax.servlet package.
The javax.servlet package
The javax.servlet package defines the contract between container and the servlet class. It provides the
flexibility to container vendor to implement the API the way they want so long as they follow the
specification. To the developers, it provides the library to develop the servlet based applications.
the javax.servlet Interfaces
The javax.servlet package is composed of 14 interfaces.
Servlet interface
The Servlet Interface is the central abstraction of the Java Servlet API. It defines the life cycle
methods of a servlet. All the servlet implementations must implement it either directly or
indirectly by extending a class which implements the Servlet interface. The two classes in the
servlet API that implement the Servlet interface are GenericServlet and HttpServlet. For most
purposes, developers will extend HttpServlet to implement their Servlets while implementing web
applications employing the HTTP protocol.
06/10/17 jit 26
SERVLET API CLASSES AND INTERFACES
ServletRequest interface
īŽ ServletRequest interface provides an abstraction of client request. 
īŽ The object of ServletRequest is used to obtain the client request information such as request
parameters, content type and length, locale of the client, request protocol etc.
īŽ Developers don’t need to implement this interface. The web container provides the
implementation this interface.  The web container creates an instance of the implementation class
and passes it as an argument when calling the service() method.
ServletResponse interface
īŽ The ServletResponse interface assists a servlet in sending a response to the client.
īŽ Developers don’t need to implement this interface. Servlet container creates the ServletResponse
object and passes it as an argument to the servlet’s service() method.
06/10/17 jit 27
SERVLET API CLASSES AND INTERFACES
ServletConfig interface
īŽ The servlet container uses a ServletConfig object to pass initialization information to the
servlet.
īŽ ServletConfig object is most commonly used to read the initialization parameters.
īŽ Servlet intialialization parameters are specified in deployment descriptor (web.xml) file.
īŽ Servlet container passes the ServletConfig object as an argument when calling servlet’s init()
method.
ServletContext interface
īŽ ServletContext object is used to communicate with the servlet container.
īŽ There is only one ServletContext object per web application (per JVM).
īŽ This is initialized when the web application is started and destroyed only when the web application is
being shutdown.
īŽ ServletContext object is most commonly used to obtain the MIME type of a file, dispatching a request,
writing to server’s log file, share the data across application, obtain URL references to resources etc
06/10/17 jit 28
SERVLET API CLASSES AND INTERFACES
SingleThreadModel Interface
īŽ SingleThreadModel is a marker interface. It is used to ensure that servlet handle only one request at a
time.
īŽ Servlets can implement SingleThreadModel interface to inform the container that it should make sure
that only one thread is executing the servlet’s service() method at any given moment.
RequestDispatcher Interface
īŽ The RequestDispatcher interface is used to dispatch requests to other resources such a
servlet, HTML file or a JSP file.
Filter Interface
īŽ Filter interface declares life cycle methods of a filter. The life cycle methods include, init()
doFilter() and destroy().
FilterChain Interface
īŽ The FilterChain object provides the filter with a handle to invoke the rest of the filter chain.
Each filter gets access to a FilterChain object when its doFilter() method is invoked. Using
this object, the filter can invoke the next filter in the chain.
06/10/17 jit 29
SERVLET API CLASSES AND INTERFACES
FilterConfig interface
īŽ The FilterConfig interface is similar to ServletConfig interface. It is used to get the initialization
parameters.
ServletContextAttributeListner
īŽ Implementation of this interface receives notification whenever an attribute is added, removed or
replaced in ServletContext. To receive the notifications, the implementation class must be
configured in the deployment descriptor (web.xml file).
ServletContextListner Interface
īŽ Implementation of this interface receives notification when the context is created and destroyed.
ServletRequestAttributeListner Interface
īŽ Implementation of this interface receives notification whenever an attribute is added, removed or
replaced in ServletRequest.
ServletRequestListner Interface
īŽ Implementation of this interface receives notification whenever the request is coming in and out
of scope. A request is defined as coming into scope when it is about to enter the first servlet or
filter in each web application, as going out of scope when it exits the last servlet or the first filter
in the chain.
06/10/17 jit 30
SERVLET API CLASSES AND INTERFACES
The javax.servlet Classes
The javax.servlet package contains 9 classes.
GenericServlet class
īŽ The GenericServlet abstract class defines the generic protocol independent servlet. It can be
extended to develop our own protocol-based servlet.
ServletContextEvent class
īŽ This is the event class for notifications about changes to the servlet context of a web application.
ServletInputStream class
īŽ Provides an input stream for reading binary data from a client request.
ServletOutputStream class
īŽ Provides an output stream for sending binary data to the client.
javax.servlet Exception classes
The javax.servlet package defines 2 exception classes.
ServletException class
īŽ Defines a general exception a servlet can throw when it encounters difficulty.
UnavailableException class
īŽ Defines an exception that a servlet or filter throws to indicate that it is permanently or temporarily
unavailable.
06/10/17 jit 31
UNDERSTANDING THE GENERICSERVLET CLASS
GenericServlet class defines a generic protocol independent servlet.  It is protocol independent in the sense
it can be extended to provide implementation of any protocol like FTP, SMTP etc. 
Servlet API comes with HttpServlet class which implements HTTP protocol. 
Generally when developing web application, you will never need to write a servlet that extends
GenericServlet. Most of the Servlets in your application will extend HttpServlet class to handle web
requests.
GenericServlet class provides implementation of Servlet Interface. This is an abstract class, all the subclasses
must implement service () method.
Public abstract class GenericServlet implements Servlet,ServletConfig,Serializable
In addition to those methods defined in Servlet Interface, GenericServlet defines following additional
methods.
Public void init()
Public void log(String message)
Public void log(String message, Throwable t)
06/10/17 jit 32
UNDERSTANDING THE GENERICSERVLET
CLASS
Initializing the Servlet
Public void init(ServletConfig config)
Public void init()
Public void init(ServletConfig config) method is an implementation of init(ServletConfig config)
method defined in Servlet interface, it stores the ServletConfig object in private transient
instance variable. 
getServletConfig() method can be used to get the reference of this object. If you override  this
method, you should include a class to super.init(config) method. Alternatively, you can override
no-argument init() method.
Handling requests
Public abstract void service (ServletRequest request, ServletResponse response)
This is an abstract method, and you must override this method in your subclass to handle requests.
All the code related to request processing and response generation goes here.
Destroying the Servlet
Public void destroy()
This method provides default implementation of destroy () method defined in the Servlet interface.
You can override this method in your subclasses to do some cleanup tasks when servlet is
taken out of service.
06/10/17 jit 33
Understanding the GenericServlet class
Accessing the environment
GenericServlet class also implements ServletConfig interface so all the methods of ServletConfig
interface can be called directly without first obtaining reference to ServletConfig instance.
Public  String getInitParameter(String name) : used to obtaing value of servlet initialization
parameter.
public String getInitParameterNames() : returns names of all initialization parameters.
Public ServletContext  getServletContext() : returns reference to ServletContext object.
String getServletName() : used to obtain name of the servlet.
Writing to server log file
Public void log(String message)
Public void log(String message, Throwable t)
GenericServlet class provides two utility methods for writing to server log file. log(String message)
method writes servlet name and message to web containers log file. the other method log(string
message, Throwable t), writes servlet name, string message and the exception stack trace of the
given Throwable exception to web containers log file.
06/10/17 jit 34
GenericServlet Example
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class GenericServletExample extends GenericServlet {
public void init() {
log("inside init() method");
}
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
log("Handling request");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.write("<html><head><title>GenericServle
example</title></head>");
out.write("<body><h1>GenericServlet: Hallo world
</h1></body></html>");
out.close();
}
06/10/17 jit 35
GenericServlet Example
public void destroy() {
log("inside destroy() method");
}
}
--------------------------------------------------------------------------
Web.xml deployment descriptor
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>GenericServlet</servlet-name>
<servlet-class>com.jsptube.servlet.GenericServletExample</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GenericServlet</servlet-name>
<url-pattern>/exampleservlet</url-pattern>
</servlet-mapping>
</web-app>
06/10/17 jit 36
A VERY SIMPLE SERVLET EXAMPLE
What is covered in this Servlet example
īŽ How to write the servlet class.
īŽ How to compile the Servlet class.
īŽ How to extract the HTML form parameters from HttpServletRequest.
īŽ web.xml deployment descriptor file.
īŽ How to create the war (web application archive) file.
īŽ How to deploy and run the sample web application in tomcat web container.
This is a very simple web application containing a HTML file and a servlet. The HTML
document has a form which allows the user to enter the name, when the form is
submitted application displays the welcome message to the user.
06/10/17 jit 37
A VERY SIMPLE SERVLET EXAMPLE
Create the HTML file.
Copy the following code into form.html file and save it under servlet-example/pages directory.
<html>
<head>
<title>The servlet example </title>
</head>
<body>
<h1>A simple web application</h1>
<form method="POST" action="/WelcomeServlet">
<label for="name">Enter your name </label>
<input type="text" id="name" name="name"/><br><br>
<input type="submit" value="Submit Form"/>
<input type="reset" value="Reset Form"/>
</form>
</body>
</html>
06/10/17 jit 38
A VERY SIMPLE SERVLET EXAMPLE
The welcome Servlet class
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class WelcomeServlet extends HttpServlet {
@Override
public void init (ServletConfig config) throws ServletException {
super.init(config);
}
protected void doPost (HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
/* Get the value of form parameter */
String name = request.getParameter("name");
String welcomeMessage = "Welcome "+name;
/* Set the content type(MIME Type) of the response.*/
response.setContentType("text/html");
PrintWriter out = response.getWriter();
06/10/17 jit 39
A VERY SIMPLE SERVLET EXAMPLE
/*
* Write the HTML to the response
*/
out. println ("<html>");
out. println ("<head>");
out. println ("<title> A very simple servlet example</title>");
out. println ("</head>");
out. println ("<body>");
out. println ("<h1>"+welcomeMessage+"</h1>");
out. println ("<a href="/servletexample/pages/form.html">"+"Click here to go back
to input page "+"</a>");
out. println ("</body>");
out. println ("</html>");
out. close ();
}
public void destroy() { }
}
Compile the WelcomeServlet.java using the following command.
>javac WelcomeServlet.java
It will create the file WelcomeServlet.class in the same directory. Copy the class file to classes directory.
All the Servlets and other classes used in a web application must be kept under WEB-
INF/classes directory.
Note:  to compile a servlet you need to have servlet-api.jar file in the class path.
06/10/17 jit 40
A VERY SIMPLE SERVLET EXAMPLE
The deployment descriptor (web.xml) file.
Copy the following code into web.xml file and save it directly under servlet-example/WEB-INF
directory.
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>WelcomeServlet</servlet-name>
<servlet-class>jsptube.tutorials.servletexample.WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>WelcomeServlet</servlet-name>
<url-pattern>/WelcomeServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
/pages/form.html
</welcome-file>
</welcome-file-list>
</web-app>
06/10/17 jit 41
A VERY SIMPLE SERVLET EXAMPLE
Create the WAR (Web application archive) file.
Web applications are packaged into a WAR (web application archive). There are many different ways to
create a WAR file. You can use jar command, ant or an IDE like Eclipse. This tutorial explains
how to create the WAR file using jar tool.
Open the command prompt and change the directory to the servlet-example directory, and execute
the following command.
>jar cvf servletexample.war *
This command packs all the contents under servlet-example directory, including subdirectories, into an
archive file called servletexample.war.
We used following command-line options:
-c option to create new WAR file.
-v option to generate verbose output.
-f option to specify target WAR file name.
You can use following command to view the content of servletexample.war file.
>Jar –tvf servletexample.war.
This command lists the content of the WAR file.
06/10/17 jit 42
A VERY SIMPLE SERVLET EXAMPLE
Deploying the application to tomcat web container.
Deployment steps are different for different J2EE servers. This tutorial explains how to deploy
the sample web application to tomcat web container. If you are using any other J2EE
server, consult the documentation of the server.
A web application can be deployed in tomcat server simply by copying the war file to
<TOMCAT_HOME>/webapp directory.
Copy servletexample.war to <TOMCAT_HOME>/webapp directory.  That’s it! You have
successfully deployed the application to tomcat web server. Once you start the server,
tomcat will extract the war file into the directory with the same name as the war file.
To start the server, open the command prompt and change the directory to
<TOMCAT_HOME/bin> directory and run the startup.bat file.
Our sample application can be accessed at http://localhost:8080/servletexample/. If tomcat
server is running on port other than 8080 than you need to change the URL accordingly.
If the application has been deployed properly, you should see the screen similar to below when
you open the application in browser.
06/10/17 jit 43
A VERY SIMPLE SERVLET EXAMPLE
06/10/17 jit 44
A VERY SIMPLE SERVLET EXAMPLE
06/10/17 jit 45
A VERY SIMPLE SERVLET EXAMPLE
How the application works.
When you access the application by navigating to URL
http://localhost:8080/servletexample/
the web server serves the form.html file.
“pagesform.html” file is specified as welcome file in web.xml file so web server serves
this file by default.
When you fill the form field and click on submit form button, browser sends the HTTP
POST request with name parameter.
Based on the servlet mapping in web.xml, the web container delegates the request to
WelcomeServlet class.
When the request is received by WelcomeServlet it performs following tasks.
īŽ Extract the name parameter from HttpServletRequest object.
īŽ Generate the welcome message.
īŽ Generate the HTML document and write the response to HttpServletResponse
object.
Browser receives the HTML document as response and displays in browser window.
The next step is to understand the code.
06/10/17 jit 46
Thank You!!

More Related Content

What's hot

Http Vs Https .
Http Vs Https . Http Vs Https .
Http Vs Https . simplyharshad
 
HTTP Definition and Basics.
HTTP Definition and Basics.HTTP Definition and Basics.
HTTP Definition and Basics.Halah Salih
 
CS8651 Internet Programming - Basics of HTML, HTML5, CSS
CS8651   Internet Programming - Basics of HTML, HTML5, CSSCS8651   Internet Programming - Basics of HTML, HTML5, CSS
CS8651 Internet Programming - Basics of HTML, HTML5, CSSVigneshkumar Ponnusamy
 
HyperText Transfer Protocol
HyperText Transfer ProtocolHyperText Transfer Protocol
HyperText Transfer Protocolponduse
 
Http and its Applications
Http and its ApplicationsHttp and its Applications
Http and its ApplicationsNayan Dagliya
 
HTTP fundamentals for developers
HTTP fundamentals for developersHTTP fundamentals for developers
HTTP fundamentals for developersMario Cardinal
 
Hypertext transfer protocol (http)
Hypertext transfer protocol (http)Hypertext transfer protocol (http)
Hypertext transfer protocol (http)Shimona Agarwal
 
Hypertext Transfer Protocol
Hypertext Transfer ProtocolHypertext Transfer Protocol
Hypertext Transfer Protocolselvakumar_b1985
 
Application layer protocol
Application layer protocolApplication layer protocol
Application layer protocolDr. Amitava Nag
 
Hypertex transfer protocol
Hypertex transfer protocolHypertex transfer protocol
Hypertex transfer protocolwanangwa234
 
Hypertext Transfer Protocol
Hypertext Transfer ProtocolHypertext Transfer Protocol
Hypertext Transfer ProtocolShubham Srivastava
 
Jaimin chp-7 - application layer- 2011 batch
Jaimin   chp-7 - application layer- 2011 batchJaimin   chp-7 - application layer- 2011 batch
Jaimin chp-7 - application layer- 2011 batchJaimin Jani
 
Hypertext Transfer Protocol
Hypertext Transfer ProtocolHypertext Transfer Protocol
Hypertext Transfer ProtocolRajan Pandey
 
Group20 Dynamic Networks
Group20 Dynamic NetworksGroup20 Dynamic Networks
Group20 Dynamic Networkshariprasadnr
 
Application layer protocols
Application layer protocolsApplication layer protocols
Application layer protocolsFabMinds
 
HTTP Basic
HTTP BasicHTTP Basic
HTTP BasicJoshua Yoon
 
HyperText Transfer Protocol (HTTP)
HyperText Transfer Protocol (HTTP)HyperText Transfer Protocol (HTTP)
HyperText Transfer Protocol (HTTP)Gurjot Singh
 

What's hot (20)

Http Vs Https .
Http Vs Https . Http Vs Https .
Http Vs Https .
 
HTTP Definition and Basics.
HTTP Definition and Basics.HTTP Definition and Basics.
HTTP Definition and Basics.
 
CS8651 Internet Programming - Basics of HTML, HTML5, CSS
CS8651   Internet Programming - Basics of HTML, HTML5, CSSCS8651   Internet Programming - Basics of HTML, HTML5, CSS
CS8651 Internet Programming - Basics of HTML, HTML5, CSS
 
HyperText Transfer Protocol
HyperText Transfer ProtocolHyperText Transfer Protocol
HyperText Transfer Protocol
 
Http and its Applications
Http and its ApplicationsHttp and its Applications
Http and its Applications
 
HTTP fundamentals for developers
HTTP fundamentals for developersHTTP fundamentals for developers
HTTP fundamentals for developers
 
Hypertext transfer protocol (http)
Hypertext transfer protocol (http)Hypertext transfer protocol (http)
Hypertext transfer protocol (http)
 
Web
WebWeb
Web
 
Hypertext Transfer Protocol
Hypertext Transfer ProtocolHypertext Transfer Protocol
Hypertext Transfer Protocol
 
Application layer protocol
Application layer protocolApplication layer protocol
Application layer protocol
 
Hypertex transfer protocol
Hypertex transfer protocolHypertex transfer protocol
Hypertex transfer protocol
 
Hypertext Transfer Protocol
Hypertext Transfer ProtocolHypertext Transfer Protocol
Hypertext Transfer Protocol
 
Jaimin chp-7 - application layer- 2011 batch
Jaimin   chp-7 - application layer- 2011 batchJaimin   chp-7 - application layer- 2011 batch
Jaimin chp-7 - application layer- 2011 batch
 
Hypertext Transfer Protocol
Hypertext Transfer ProtocolHypertext Transfer Protocol
Hypertext Transfer Protocol
 
Group20 Dynamic Networks
Group20 Dynamic NetworksGroup20 Dynamic Networks
Group20 Dynamic Networks
 
Application layer protocols
Application layer protocolsApplication layer protocols
Application layer protocols
 
HTTP Basic
HTTP BasicHTTP Basic
HTTP Basic
 
HTTP Basics
HTTP BasicsHTTP Basics
HTTP Basics
 
Web technology
Web technologyWeb technology
Web technology
 
HyperText Transfer Protocol (HTTP)
HyperText Transfer Protocol (HTTP)HyperText Transfer Protocol (HTTP)
HyperText Transfer Protocol (HTTP)
 

Similar to Web Technology Introduction at JIT

Anintroductiontojavawebtechnology 090324184240-phpapp01
Anintroductiontojavawebtechnology 090324184240-phpapp01Anintroductiontojavawebtechnology 090324184240-phpapp01
Anintroductiontojavawebtechnology 090324184240-phpapp01raviIITRoorkee
 
Web Tech Java Servlet Update1
Web Tech   Java Servlet Update1Web Tech   Java Servlet Update1
Web Tech Java Servlet Update1vikram singh
 
An Introduction To Java Web Technology
An Introduction To Java Web TechnologyAn Introduction To Java Web Technology
An Introduction To Java Web Technologyvikram singh
 
Web Services 2009
Web Services 2009Web Services 2009
Web Services 2009Cathie101
 
Ajp notes-chapter-06
Ajp notes-chapter-06Ajp notes-chapter-06
Ajp notes-chapter-06Ankit Dubey
 
internet programming and java notes 5th sem mca
internet programming and java notes 5th sem mcainternet programming and java notes 5th sem mca
internet programming and java notes 5th sem mcaRenu Thakur
 
Restful web services
Restful web servicesRestful web services
Restful web servicesMD Sayem Ahmed
 
Web technology-guide
Web technology-guideWeb technology-guide
Web technology-guideSrihari
 
Servlet classnotes
Servlet classnotesServlet classnotes
Servlet classnotesVasanti Dutta
 
Lecture 1 Introduction to Web Development.pptx
Lecture 1 Introduction to Web Development.pptxLecture 1 Introduction to Web Development.pptx
Lecture 1 Introduction to Web Development.pptxKevi20
 
Web programming by Najeeb ullahAzad(1)
Web programming by Najeeb ullahAzad(1)Web programming by Najeeb ullahAzad(1)
Web programming by Najeeb ullahAzad(1)azadmcs
 
1 web technologies
1 web technologies1 web technologies
1 web technologiesJalpesh Vasa
 
internet principles of operation By ZAK
internet principles of operation By ZAKinternet principles of operation By ZAK
internet principles of operation By ZAKTabsheer Hasan
 
HTTPs Strict Transport Security
HTTPs    Strict Transport Security HTTPs    Strict Transport Security
HTTPs Strict Transport Security Gol D Roger
 
Http_Protocol.pptx
Http_Protocol.pptxHttp_Protocol.pptx
Http_Protocol.pptxAbshar Fatima
 

Similar to Web Technology Introduction at JIT (20)

Anintroductiontojavawebtechnology 090324184240-phpapp01
Anintroductiontojavawebtechnology 090324184240-phpapp01Anintroductiontojavawebtechnology 090324184240-phpapp01
Anintroductiontojavawebtechnology 090324184240-phpapp01
 
Web Tech Java Servlet Update1
Web Tech   Java Servlet Update1Web Tech   Java Servlet Update1
Web Tech Java Servlet Update1
 
An Introduction To Java Web Technology
An Introduction To Java Web TechnologyAn Introduction To Java Web Technology
An Introduction To Java Web Technology
 
Web Services 2009
Web Services 2009Web Services 2009
Web Services 2009
 
Ajp notes-chapter-06
Ajp notes-chapter-06Ajp notes-chapter-06
Ajp notes-chapter-06
 
Ftp servlet
Ftp servletFtp servlet
Ftp servlet
 
Servlet & jsp
Servlet  &  jspServlet  &  jsp
Servlet & jsp
 
internet programming and java notes 5th sem mca
internet programming and java notes 5th sem mcainternet programming and java notes 5th sem mca
internet programming and java notes 5th sem mca
 
Restful web services
Restful web servicesRestful web services
Restful web services
 
Web technology-guide
Web technology-guideWeb technology-guide
Web technology-guide
 
Servlet classnotes
Servlet classnotesServlet classnotes
Servlet classnotes
 
Lecture 1 Introduction to Web Development.pptx
Lecture 1 Introduction to Web Development.pptxLecture 1 Introduction to Web Development.pptx
Lecture 1 Introduction to Web Development.pptx
 
Web programming by Najeeb ullahAzad(1)
Web programming by Najeeb ullahAzad(1)Web programming by Najeeb ullahAzad(1)
Web programming by Najeeb ullahAzad(1)
 
1 web technologies
1 web technologies1 web technologies
1 web technologies
 
Marata
MarataMarata
Marata
 
internet principles of operation By ZAK
internet principles of operation By ZAKinternet principles of operation By ZAK
internet principles of operation By ZAK
 
HTTPs Strict Transport Security
HTTPs    Strict Transport Security HTTPs    Strict Transport Security
HTTPs Strict Transport Security
 
Servlet by Rj
Servlet by RjServlet by Rj
Servlet by Rj
 
Http_Protocol.pptx
Http_Protocol.pptxHttp_Protocol.pptx
Http_Protocol.pptx
 
Internet
InternetInternet
Internet
 

More from abhishek srivastav

More from abhishek srivastav (6)

Unit 5
Unit 5Unit 5
Unit 5
 
Unit 4
Unit 4Unit 4
Unit 4
 
Unit 3
Unit 3Unit 3
Unit 3
 
Unit 2
Unit 2Unit 2
Unit 2
 
Unit1
Unit1Unit1
Unit1
 
Abhishek Srivastava (1)original (1)
Abhishek Srivastava (1)original (1)Abhishek Srivastava (1)original (1)
Abhishek Srivastava (1)original (1)
 

Recently uploaded

EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
HáģŒC TáģT TIáēžNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIáēžT - Cáēĸ NĂ...
HáģŒC TáģT TIáēžNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIáēžT - Cáēĸ NĂ...HáģŒC TáģT TIáēžNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIáēžT - Cáēĸ NĂ...
HáģŒC TáģT TIáēžNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIáēžT - Cáēĸ NĂ...Nguyen Thanh Tu Collection
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸
call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸
call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
HáģŒC TáģT TIáēžNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIáēžT - Cáēĸ NĂ...
HáģŒC TáģT TIáēžNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIáēžT - Cáēĸ NĂ...HáģŒC TáģT TIáēžNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIáēžT - Cáēĸ NĂ...
HáģŒC TáģT TIáēžNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIáēžT - Cáēĸ NĂ...
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸
call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸
call girls in Kamla Market (DELHI) 🔝 >āŧ’9953330565🔝 genuine Escort Service 🔝✔ī¸âœ”ī¸
 

Web Technology Introduction at JIT

  • 1. 06/10/17 jit 1 An Introduction to Web Technology Presented At: JAHANGIRABAD INSTITUTE OF TECHNOLOGY
  • 2. Some historical details īŽ Start of standard groups īŽ TCP īą handles conversion between messages and streams packets īŽ IP īą handles addressing of packets across networks īŽ TCP/IP īą enables packets to be sent across multiple networks using multiple standards īŽ Telnet īą One of the earliest standards for exchanging transmission, directly connect accounts on different systems. īŽ SMTP īą specifies another way of direct connection
  • 3. Some historical details īŽ MIME īą Extension to the SMTP Protocol which supports the exchange of richer data files such as audio-, video-, and images data. īŽ FTP īą (1973) supports file transfer between Internet sites and allows a system to publish a set of files by hosting an FTP sever innovation īƒ  permits anonymous users to transfer files īŽ Archie īą Late 1980‘s distributed file system based on FTP īŽ Gopher īą First simple system, providing GUI
  • 4. Core Web Technologies īŽ HTTP(HyperText Transfer Protocol) īŽ generic, stateless protocol īŽ governs the transfer of files across a network īŽ developed at CERN (Central European Research Network), they also came up with the name WWW, later W3C īŽ supports access to SMTP,FTP and other protocols īŽ was designed to support hypertext
  • 5. Core Web Technologies īŽ Exchanged information, can be static or dynamic īŽ Every resource, accessible over the Web has a URL(Uniform resource locator) īŽ HTTP mechanism is based on client/server model typically using TCP/IP sockets
  • 6. Core Web Technologies īŽ since Version 1.1 HTTP requires servers to support persistent connections, to minimize overhead associated with opening and closing connections. īŽ Typical methods on the server side are: īą OPTIONS ī‚§ send information about the communication options īą GET ī‚§ retrieve document or document produced by a program īą POST ī‚§ Append or attach information īą PUT ī‚§ Store information īą DELETE ī‚§ Delete the resource indicated in the request
  • 7. Core Web Technologies īŽ Another limitationīƒ  HTTP is stateless īą Does not provide storing of information between requests īą No indication of any relationship between two different requests īƒ  cookies, small data structures that a web server requests the HTTP client to store on the local machine, are used to maintain state information e.g. cookies store recently view items on a web shop
  • 8. 06/10/17 jit 8 HTTP īƒ˜HTTP stands for HyperText Transport Protocol, and is the network protocol used over the web. It runs over TCP/IP. īƒ˜HTTP uses a request/response model.Client sends an HTTP request, then the server gives back the HTTP response that the browser will display (depending on the content type of the answer). īƒ˜If the response is an HTML file, then the HTML file is added to the HTTP response body. īƒ˜An HTTP response includes : status code, the content-type (MIME type), and actual content of the response. īƒ˜A MIME type tells the browser what kind of data the response is holding. īƒ˜URL stands for Uniform Resource Locator: starts with a protocol, then a server name, optionally followed by a port number, then the path to the resource followed by the resource name. Parameters may appear at the end, separated from the rest by a ? .
  • 9. 06/10/17 jit 9 HTTP METHODS METHOD DESCRIPTION HEAD Asks for the response identical to the one that would correspond to a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content. GET means retrieve whatever data is identified by the URI POST Submits data to be processed (e.g., from an HTML form) to the identified resource. The data is included in the body of the request. DELETE Deletes the specified resource. TRACE Echoes back the received request, so that a client can see what intermediate servers are adding or changing in the request. OPTIONS Returns the HTTP methods that the server supports for specified URL. This can be used to check the functionality of a web server by requesting '*' instead of a specific resource. CONNECT Converts the request connection to a transparent TCP/IP tunnel, usually to facilitate SSL- encrypted communication (HTTPS) through an unencrypted HTTP proxy.[3] PUT Uploads a representation of the specified resource.
  • 10. 06/10/17 jit 10 HTTP METHODS HTTP servers are required to implement at least the GET and HEAD methods [4] and, whenever possible, also the OPTIONS method Safe methods Some methods (for example, HEAD, GET, OPTIONS and TRACE) are defined as safe, which means they are intended only for information retrieval and should not change the state of the server. Idempotent methods Some http methods are defined to be idempotent, meaning that multiple identical requests should have the same effect as a single request. Methods PUT, DELETE, GET, HEAD, OPTIONS and TRACE, being prescribed as safe, should also be idempotent. HTTP is a stateless protocol. By contrast, the POST method is not necessarily idempotent, and therefore sending an identical POST request multiple times may further affect state or cause further side effects.(like updating inserting records in database multiple times).
  • 11. 06/10/17 jit 11 DIFFERENCE BETWEEN GET AND POST GET: GET parameters are passed on the URL line, after a ?. The URL size is limited. The parameters appear in the browser URL field, hence showing any password to the world... GET is supposed to be used to GET things (only retrieval, no modification on the server). GET processing must be idempotent. A click on a hyperlink always sends a GET request. GET is the default method used by HTML forms. Use the method=”POST” to change it. POST: POST has a body. In POST requests, the parameters are passed in the body of the request, after the header. There is no size-limit. Parameters are not visible from the user. POST is supposed to be used to send data to be processed. POST may not be idempotent and is the only one. IDEMPOTENT : Can do the same thing over and over again, with no negative side effect.
  • 12. 06/10/17 jit 12 WHAT IS WEB CONTAINER? A Web application runs within a Web container of a Web server. The Web container provides the runtime environment through components that provide naming context and life cycle management. Some Web servers may also provide additional services such as security and concurrency control. Web applications are composed of web components and other data such as HTML pages. Web components can be servlets, JSP pages created with the JavaServer Pagesâ„ĸ technology, web filters, and web event listeners. These components typically execute in a web server and may respond to HTTP requests from web clients. Servlets, JSP pages, and filters may be used to generate HTML pages that are an application’s user interface.
  • 13. 06/10/17 jit 13 WHAT IS JAVA SERVLET? Servlets are Java classes that process the request dynamically and generate response independent of the protocol. Servlets are defined in Java Servlet API specification. Latest Servlet API specification available is 2.5. Servlets are server side Java programs which extends the functionality of web server. Servlet are protocol independent that means it can be used virtually with any protocol to process the request and generate the response. However in practice Servlets are used to process the HTTP requests and generate the HTML response.
  • 14. 06/10/17 jit 14 STRUCTURE OF A WEB APPLICATION Web Applications use a standard directory structure defined in the Servlet specification. When developing web applications on J2EE platform, you must follow this structure so that application can be deployed in any J2EE compliant web server. A web application has the directory structure as shown in figure. The root directory of the application is called the document root. Root directory is mapped to the context path. Root directory contains a directory named WEB-INF. Anything under the root directory excepting the WEB-INF directory is publically available, and can be accessed by URL from browser. WEB-INF directory is a private area of the web application, any files under WEB-INF directory cannot be accessed directly from browser by specifying the URL like http://somesite/WEB- INF/someresource.html. Web container will not serve the content of this directory. However the content of the WEB-INF directory is accessible by the classes within the application.
  • 15. 06/10/17 jit 15 WEB-INF DIRECTORY WEB-INF directory contains īŽ WEB-INF/web.xml deployment descriptor īŽ WEB-INF/classes directory īŽ WEB-INF/lib directory CLASSES- directory is used to store compiled servlet and other classes of the application. LIB - directory is used to store the jar files. If application has any bundled jar files, or if application uses any third party libraries such as log4j which is packaged in jar file, than these jar files should be placed in lib directory. All unpacked classes and resources in the /WEB-INF/classes directory, plus classes and resources in JAR files under the /WEB-INF/lib directory, are made visible to the containing web application.
  • 16. 06/10/17 jit 16 SERVLET LIFE CYCLE Servlets are managed components. They are managed by web container. Of the various responsibilities of web container, servlet life cycle management is the most important one. A servlet is managed through a well defined life cycle that defines how it is loaded, instantiated ad initialized, handles requests from clients and how it is taken out of service. The servlet life cycle methods are defined in the javax.servlet.Servlet interface of the Servlet API that all Servlets must implement directly or indirectly by extending GenericServlet or HttpServlet abstract classes. Most of the servlet you develop will implement it by extending HttpServlet class. The servlet life cycle methods defined in Servlet interface are init(), service() and destroy(). The signature of this methods are shown below. public void init(ServletConfig config) throws ServletException public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException public void destroy()
  • 17. 06/10/17 jit 17 SERVLET LIFE CYCLE - STEPS The servlet life cycle consists of four steps, instantiation, initialization, request handling and end of service. Loading and instantiation During this step, web container loads the servlet class and creates a new instance of the servlet. The container can create a servlet instance at container startup or it can delay it until the servlet is needed to service a request. Initialization During initialization stage of the Servlet life cycle, the web container initializes the servlet instance by calling the init() method. The container passes an object implementing the ServletConfig interface via the init() method. This configuration object allows the servlet to access name-value initialization parameters from the web application’s deployment descriptor (web.xml) file. The container guarantees that the init() method will be called before the service() method is called. The init() method is typically used to perform servlet initialization, creating or loading objects that are used by the servlet in the handling of its requests. The init() method is commonly used to perform one time activity. One of the most common use of init() method is to setup the database connection or connection pool.
  • 18. 06/10/17 jit 18 SERVLET LIFE CYCLE - STEPS Request handling After a servlet is properly initialized, it is ready to handle the client requests. If the container has a request for the servlet, it calls the servlet instance’s service() method. The request and response information is wrapped in ServletRequest and ServletResponse objects respectively, which are then passed to the servlet's service() method. In the case of an HTTP request, the objects provided by the container are of types HttpServletRequest and HttpServletResponse. Service() method is responsible for processing the incoming requests and generating the response. End of service When the servlet container determines that a servlet should be removed from service, it calls the destroy () method of the Servlet  instance to allow the servlet to release any resources it is using. The servlet container can destroy a servlet because it wants to conserve some memory or server itself is shutting down. Destroy() method is used to release any resources it is using. The  most common use of destroy() method is to close the database connections.
  • 19. 06/10/17 jit 19 SETTING UP SERVLET DEVELOPMENT ENVIRONMENT List of required softwares: īŽ JAVA 1.5 or 1.6 īŽ Tomcat 5.5.16 First of all you need the Java software development kit 1.5 or 1.6 (JAVA SDK) installed. Checking if JAVA is already installed in your system
  • 20. 06/10/17 jit 20 SETTING UP SERVLET DEVELOPMENT ENVIRONMENT Set up the JAVA_HOME environment variable Once the JAVA SDK is installed follow this step to set the JAVA_HOME environment variable. If JAVA_HOME variable is already set, you can skip this step.
  • 21. 06/10/17 jit 21 SETTING UP SERVLET DEVELOPMENT ENVIRONMENT
  • 22. 06/10/17 jit 22 SETTING UP SERVLET DEVELOPMENT ENVIRONMENT In the variable name filed, enter JAVA_HOME. Specify the path to root directory of JAVA installation in variable value field and click OK button. Now JAVA_HOME will appear under user variables. Next you need to add bin directory under the root directory of JAVA installation in PATH environment variable.Select the PATH variable from System variables and click on Edit button. Add: ;%JAVA_HOME%bin; at the end of variable value field and click OK button.
  • 23. 06/10/17 jit 23 INSTALLING TOMCAT Tomcat is an opensource web container. it is also web container reference implementation. Download the latest version of tomcat from this URL . Download the jakarta-tomcat- 5.0.28.tar.gz and extract it to the directory of your choice. Note: This directory is referred as TOMCAT_HOME in other tutorials. That’s all, tomcat is installed. Starting and shutting down Tomcat To start the tomcat server, open the command prompt, change the directory to TOMCAT HOME/bin and run the startup.bat file. It will start the server. >startup To shut down the tomcat server, run the shutdown.bat file. It will stop the server. >shutdown Verifying Tomcat installation To verify that tomcat is installed properly, start the server as explained above, open the web browser and access the following URL. http://localhost:8080/index.jsp It should show the tomcat welcome page, if tomcat is installed properly and server is running.
  • 24. 06/10/17 jit 24 INSTALLING TOMCAT Setting up the CLASSPATH Now you need to create a new environment variable CLASSPATH if it is not already set. We need to add the servlet-api.jar into the CLASSPATH to compile the Servlets. Follow the same steps as you did to create the JAVA_HOME variable. Create a new variable CLASSPATH under system variables. Add TOMCAT_HOME/lib/servlet-api.jar in variable value field. Note: here TOMCAT_HOME refers to the tomcat installation directory.
  • 25. 06/10/17 jit 25 SERVLET API CLASSES AND INTERFACES Servlet API is specified in two packages: javax.servlet and javax.servlet.http. The classes and interfaces in javax.servlet are protocol independent, while the classes and interface in javax.servlet.http deals with specialized HTTP Servlets. Some of the classes and interfaces in the javax.servlet.http package extend those specified in javax.servlet package. The javax.servlet package The javax.servlet package defines the contract between container and the servlet class. It provides the flexibility to container vendor to implement the API the way they want so long as they follow the specification. To the developers, it provides the library to develop the servlet based applications. the javax.servlet Interfaces The javax.servlet package is composed of 14 interfaces. Servlet interface The Servlet Interface is the central abstraction of the Java Servlet API. It defines the life cycle methods of a servlet. All the servlet implementations must implement it either directly or indirectly by extending a class which implements the Servlet interface. The two classes in the servlet API that implement the Servlet interface are GenericServlet and HttpServlet. For most purposes, developers will extend HttpServlet to implement their Servlets while implementing web applications employing the HTTP protocol.
  • 26. 06/10/17 jit 26 SERVLET API CLASSES AND INTERFACES ServletRequest interface īŽ ServletRequest interface provides an abstraction of client request.  īŽ The object of ServletRequest is used to obtain the client request information such as request parameters, content type and length, locale of the client, request protocol etc. īŽ Developers don’t need to implement this interface. The web container provides the implementation this interface.  The web container creates an instance of the implementation class and passes it as an argument when calling the service() method. ServletResponse interface īŽ The ServletResponse interface assists a servlet in sending a response to the client. īŽ Developers don’t need to implement this interface. Servlet container creates the ServletResponse object and passes it as an argument to the servlet’s service() method.
  • 27. 06/10/17 jit 27 SERVLET API CLASSES AND INTERFACES ServletConfig interface īŽ The servlet container uses a ServletConfig object to pass initialization information to the servlet. īŽ ServletConfig object is most commonly used to read the initialization parameters. īŽ Servlet intialialization parameters are specified in deployment descriptor (web.xml) file. īŽ Servlet container passes the ServletConfig object as an argument when calling servlet’s init() method. ServletContext interface īŽ ServletContext object is used to communicate with the servlet container. īŽ There is only one ServletContext object per web application (per JVM). īŽ This is initialized when the web application is started and destroyed only when the web application is being shutdown. īŽ ServletContext object is most commonly used to obtain the MIME type of a file, dispatching a request, writing to server’s log file, share the data across application, obtain URL references to resources etc
  • 28. 06/10/17 jit 28 SERVLET API CLASSES AND INTERFACES SingleThreadModel Interface īŽ SingleThreadModel is a marker interface. It is used to ensure that servlet handle only one request at a time. īŽ Servlets can implement SingleThreadModel interface to inform the container that it should make sure that only one thread is executing the servlet’s service() method at any given moment. RequestDispatcher Interface īŽ The RequestDispatcher interface is used to dispatch requests to other resources such a servlet, HTML file or a JSP file. Filter Interface īŽ Filter interface declares life cycle methods of a filter. The life cycle methods include, init() doFilter() and destroy(). FilterChain Interface īŽ The FilterChain object provides the filter with a handle to invoke the rest of the filter chain. Each filter gets access to a FilterChain object when its doFilter() method is invoked. Using this object, the filter can invoke the next filter in the chain.
  • 29. 06/10/17 jit 29 SERVLET API CLASSES AND INTERFACES FilterConfig interface īŽ The FilterConfig interface is similar to ServletConfig interface. It is used to get the initialization parameters. ServletContextAttributeListner īŽ Implementation of this interface receives notification whenever an attribute is added, removed or replaced in ServletContext. To receive the notifications, the implementation class must be configured in the deployment descriptor (web.xml file). ServletContextListner Interface īŽ Implementation of this interface receives notification when the context is created and destroyed. ServletRequestAttributeListner Interface īŽ Implementation of this interface receives notification whenever an attribute is added, removed or replaced in ServletRequest. ServletRequestListner Interface īŽ Implementation of this interface receives notification whenever the request is coming in and out of scope. A request is defined as coming into scope when it is about to enter the first servlet or filter in each web application, as going out of scope when it exits the last servlet or the first filter in the chain.
  • 30. 06/10/17 jit 30 SERVLET API CLASSES AND INTERFACES The javax.servlet Classes The javax.servlet package contains 9 classes. GenericServlet class īŽ The GenericServlet abstract class defines the generic protocol independent servlet. It can be extended to develop our own protocol-based servlet. ServletContextEvent class īŽ This is the event class for notifications about changes to the servlet context of a web application. ServletInputStream class īŽ Provides an input stream for reading binary data from a client request. ServletOutputStream class īŽ Provides an output stream for sending binary data to the client. javax.servlet Exception classes The javax.servlet package defines 2 exception classes. ServletException class īŽ Defines a general exception a servlet can throw when it encounters difficulty. UnavailableException class īŽ Defines an exception that a servlet or filter throws to indicate that it is permanently or temporarily unavailable.
  • 31. 06/10/17 jit 31 UNDERSTANDING THE GENERICSERVLET CLASS GenericServlet class defines a generic protocol independent servlet.  It is protocol independent in the sense it can be extended to provide implementation of any protocol like FTP, SMTP etc.  Servlet API comes with HttpServlet class which implements HTTP protocol.  Generally when developing web application, you will never need to write a servlet that extends GenericServlet. Most of the Servlets in your application will extend HttpServlet class to handle web requests. GenericServlet class provides implementation of Servlet Interface. This is an abstract class, all the subclasses must implement service () method. Public abstract class GenericServlet implements Servlet,ServletConfig,Serializable In addition to those methods defined in Servlet Interface, GenericServlet defines following additional methods. Public void init() Public void log(String message) Public void log(String message, Throwable t)
  • 32. 06/10/17 jit 32 UNDERSTANDING THE GENERICSERVLET CLASS Initializing the Servlet Public void init(ServletConfig config) Public void init() Public void init(ServletConfig config) method is an implementation of init(ServletConfig config) method defined in Servlet interface, it stores the ServletConfig object in private transient instance variable.  getServletConfig() method can be used to get the reference of this object. If you override  this method, you should include a class to super.init(config) method. Alternatively, you can override no-argument init() method. Handling requests Public abstract void service (ServletRequest request, ServletResponse response) This is an abstract method, and you must override this method in your subclass to handle requests. All the code related to request processing and response generation goes here. Destroying the Servlet Public void destroy() This method provides default implementation of destroy () method defined in the Servlet interface. You can override this method in your subclasses to do some cleanup tasks when servlet is taken out of service.
  • 33. 06/10/17 jit 33 Understanding the GenericServlet class Accessing the environment GenericServlet class also implements ServletConfig interface so all the methods of ServletConfig interface can be called directly without first obtaining reference to ServletConfig instance. Public  String getInitParameter(String name) : used to obtaing value of servlet initialization parameter. public String getInitParameterNames() : returns names of all initialization parameters. Public ServletContext  getServletContext() : returns reference to ServletContext object. String getServletName() : used to obtain name of the servlet. Writing to server log file Public void log(String message) Public void log(String message, Throwable t) GenericServlet class provides two utility methods for writing to server log file. log(String message) method writes servlet name and message to web containers log file. the other method log(string message, Throwable t), writes servlet name, string message and the exception stack trace of the given Throwable exception to web containers log file.
  • 34. 06/10/17 jit 34 GenericServlet Example import java.io.IOException; import java.io.PrintWriter; import javax.servlet.GenericServlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; public class GenericServletExample extends GenericServlet { public void init() { log("inside init() method"); } public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { log("Handling request"); response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.write("<html><head><title>GenericServle example</title></head>"); out.write("<body><h1>GenericServlet: Hallo world </h1></body></html>"); out.close(); }
  • 35. 06/10/17 jit 35 GenericServlet Example public void destroy() { log("inside destroy() method"); } } -------------------------------------------------------------------------- Web.xml deployment descriptor <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <servlet> <servlet-name>GenericServlet</servlet-name> <servlet-class>com.jsptube.servlet.GenericServletExample</servlet-class> </servlet> <servlet-mapping> <servlet-name>GenericServlet</servlet-name> <url-pattern>/exampleservlet</url-pattern> </servlet-mapping> </web-app>
  • 36. 06/10/17 jit 36 A VERY SIMPLE SERVLET EXAMPLE What is covered in this Servlet example īŽ How to write the servlet class. īŽ How to compile the Servlet class. īŽ How to extract the HTML form parameters from HttpServletRequest. īŽ web.xml deployment descriptor file. īŽ How to create the war (web application archive) file. īŽ How to deploy and run the sample web application in tomcat web container. This is a very simple web application containing a HTML file and a servlet. The HTML document has a form which allows the user to enter the name, when the form is submitted application displays the welcome message to the user.
  • 37. 06/10/17 jit 37 A VERY SIMPLE SERVLET EXAMPLE Create the HTML file. Copy the following code into form.html file and save it under servlet-example/pages directory. <html> <head> <title>The servlet example </title> </head> <body> <h1>A simple web application</h1> <form method="POST" action="/WelcomeServlet"> <label for="name">Enter your name </label> <input type="text" id="name" name="name"/><br><br> <input type="submit" value="Submit Form"/> <input type="reset" value="Reset Form"/> </form> </body> </html>
  • 38. 06/10/17 jit 38 A VERY SIMPLE SERVLET EXAMPLE The welcome Servlet class import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class WelcomeServlet extends HttpServlet { @Override public void init (ServletConfig config) throws ServletException { super.init(config); } protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* Get the value of form parameter */ String name = request.getParameter("name"); String welcomeMessage = "Welcome "+name; /* Set the content type(MIME Type) of the response.*/ response.setContentType("text/html"); PrintWriter out = response.getWriter();
  • 39. 06/10/17 jit 39 A VERY SIMPLE SERVLET EXAMPLE /* * Write the HTML to the response */ out. println ("<html>"); out. println ("<head>"); out. println ("<title> A very simple servlet example</title>"); out. println ("</head>"); out. println ("<body>"); out. println ("<h1>"+welcomeMessage+"</h1>"); out. println ("<a href="/servletexample/pages/form.html">"+"Click here to go back to input page "+"</a>"); out. println ("</body>"); out. println ("</html>"); out. close (); } public void destroy() { } } Compile the WelcomeServlet.java using the following command. >javac WelcomeServlet.java It will create the file WelcomeServlet.class in the same directory. Copy the class file to classes directory. All the Servlets and other classes used in a web application must be kept under WEB- INF/classes directory. Note:  to compile a servlet you need to have servlet-api.jar file in the class path.
  • 40. 06/10/17 jit 40 A VERY SIMPLE SERVLET EXAMPLE The deployment descriptor (web.xml) file. Copy the following code into web.xml file and save it directly under servlet-example/WEB-INF directory. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <servlet> <servlet-name>WelcomeServlet</servlet-name> <servlet-class>jsptube.tutorials.servletexample.WelcomeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>WelcomeServlet</servlet-name> <url-pattern>/WelcomeServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file> /pages/form.html </welcome-file> </welcome-file-list> </web-app>
  • 41. 06/10/17 jit 41 A VERY SIMPLE SERVLET EXAMPLE Create the WAR (Web application archive) file. Web applications are packaged into a WAR (web application archive). There are many different ways to create a WAR file. You can use jar command, ant or an IDE like Eclipse. This tutorial explains how to create the WAR file using jar tool. Open the command prompt and change the directory to the servlet-example directory, and execute the following command. >jar cvf servletexample.war * This command packs all the contents under servlet-example directory, including subdirectories, into an archive file called servletexample.war. We used following command-line options: -c option to create new WAR file. -v option to generate verbose output. -f option to specify target WAR file name. You can use following command to view the content of servletexample.war file. >Jar –tvf servletexample.war. This command lists the content of the WAR file.
  • 42. 06/10/17 jit 42 A VERY SIMPLE SERVLET EXAMPLE Deploying the application to tomcat web container. Deployment steps are different for different J2EE servers. This tutorial explains how to deploy the sample web application to tomcat web container. If you are using any other J2EE server, consult the documentation of the server. A web application can be deployed in tomcat server simply by copying the war file to <TOMCAT_HOME>/webapp directory. Copy servletexample.war to <TOMCAT_HOME>/webapp directory.  That’s it! You have successfully deployed the application to tomcat web server. Once you start the server, tomcat will extract the war file into the directory with the same name as the war file. To start the server, open the command prompt and change the directory to <TOMCAT_HOME/bin> directory and run the startup.bat file. Our sample application can be accessed at http://localhost:8080/servletexample/. If tomcat server is running on port other than 8080 than you need to change the URL accordingly. If the application has been deployed properly, you should see the screen similar to below when you open the application in browser.
  • 43. 06/10/17 jit 43 A VERY SIMPLE SERVLET EXAMPLE
  • 44. 06/10/17 jit 44 A VERY SIMPLE SERVLET EXAMPLE
  • 45. 06/10/17 jit 45 A VERY SIMPLE SERVLET EXAMPLE How the application works. When you access the application by navigating to URL http://localhost:8080/servletexample/ the web server serves the form.html file. “pagesform.html” file is specified as welcome file in web.xml file so web server serves this file by default. When you fill the form field and click on submit form button, browser sends the HTTP POST request with name parameter. Based on the servlet mapping in web.xml, the web container delegates the request to WelcomeServlet class. When the request is received by WelcomeServlet it performs following tasks. īŽ Extract the name parameter from HttpServletRequest object. īŽ Generate the welcome message. īŽ Generate the HTML document and write the response to HttpServletResponse object. Browser receives the HTML document as response and displays in browser window. The next step is to understand the code.