Chapter4 Installation - and Servlets
Chapter4 Installation - and Servlets
select install
This is
how we
web.xml >>
The web.xml file contains information about the web application, which is used by the Java web
server / servlet container in order to properly deploy and execute the web application. For instance,
the web.xml contains information about which servlets a web application should deploy, and what
URL's they should be mapped to.
The classes directory >>
The classes directory contains all compiled Java classes that are part of your web application. The
classes should be located in a directory structure matching their package structure.
<servlet-name>: This is the sub element of servlet and used to represents the name of servlet
<servlet-class>: This is the sub element of servlet and used to represents the class of servlet
<servlet-mapping>: This is the sub element of and used to map the servlet
<url-pattern>: This is the sub element of servlet-mapping and used to client side to invoke the
servlet
4.4.2
Web.xml Tags >>>
Welcome-file-list tag >>
This tag is used to specify the default page of web application if none is specified.
Example:
[code lang=”xml”]
<welcome-file-list>
<welcome-file>hello.html</welcome-file>
</welcome-file-list>
[/code]
In the above example index.jsp used as web page for web application
Example:
[code lang=”xml”]
<session-config>
<session-timeout>
15
</session-timeout>
</session-config>[/code]
In the above example session-config tag contains another tag session-timeout which specifies the
http session timeout. The time specify in minutes.
bin
BUILDING.txt
conf
CONTRIBUTING.md
lib
LICENSE
logs
NOTICE
README.md
RELEASE-NOTES
RUNNING.txt
temp
webapps
work
In our syllabus we will use netbeans IDE to start and stop the server.Before using tomcat in
netbeans make sure to shutdown server in cmd to avoid confict.
Servlet >>
Servlet technology is used to create a web application. It resides at server side and generates a
dynamic web page).
Servlet is a class that extends the capabilities of the servers and responds to the incoming requests.
It can respond to any requests.
Servlet is a web component that is deployed on the server to create a dynamic web page.
A web container is the component of a web server that interacts with Java servlets. A web container
manages the life cycle of servlets; it maps a URL to a particular servlet
The web
container creates threads for handling the multiple requests to the Servlet. Threads have many
benefits over the Processes such as they share a common memory area, lightweight, cost of
communication between the threads are low.
Life cycle of servlet >>
The web container maintains the life cycle of a servlet instance. Let's see the life cycle of the
servlet:
After invoking the init() method, Servlet comes in the ready state. In the ready state, servlet
performs all the tasks.
When the web container invokes the destroy() method, it shifts to the end state.
4.7 Creating and Initializing Servlet >>
Servlet API >>
The javax.servlet and javax.servlet.http packages represent interfaces and classes for servlet api.
javax.servlet >>
The javax.servlet package contains many interfaces and classes that are used by the servlet or web
container. These are not specific to any protocol.
javax.servlet.http >>
The javax.servlet.http package contains interfaces and classes that are responsible for http requests
only.
The servlet example can be created by three ways >>
• By implementing Servlet interface
• By inheriting GenericServlet class
• By inheriting HttpServlet class
The mostly used approach is by extending HttpServlet because it provides http request specific
method such as doGet(), doPost(), doHead() etc.
It will
index.html >
<!DOCTYPE html>
<html>
<body>
<form method="post" action="DemoServlet">
Name <input type="text" name="user" >
<input type="submit" value="submit">
</form>
</body>
</html>
web.xml >
DemoServlet.java >
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
DemoServlet.java >>
HttpServletResponse class methods >>
Format of http response >>