0% found this document useful (0 votes)
5 views5 pages

Overview of Servlet API and web.xml

Uploaded by

Deep mathukiya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Overview of Servlet API and web.xml

Uploaded by

Deep mathukiya
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Servlet API

The [Link] and [Link] packages represent interfaces and classes for
servlet api.

The [Link] package contains many interfaces and classes that are used by the
servlet or web container. These are not specific to any protocol.

The [Link] package contains interfaces and classes that are responsible
for http requests only.

Let's see what are the interfaces of [Link] package.

Interfaces in [Link] package


There are many interfaces in [Link] package. They are as follows:

1. Servlet
2. ServletRequest
3. ServletResponse
4. RequestDispatcher
5. ServletConfig
6. ServletContext
7. SingleThreadModel
8. Filter
9. FilterConfig
10. FilterChain
11. ServletRequestListener
12. ServletRequestAttributeListener
13. ServletContextListener
14. ServletContextAttributeListener

Classes in [Link] package


There are many classes in [Link] package. They are as follows:

1. GenericServlet
2. ServletInputStream
3. ServletOutputStream
4. ServletRequestWrapper
5. ServletResponseWrapper
6. ServletRequestEvent
7. ServletContextEvent
8. ServletRequestAttributeEvent
9. ServletContextAttributeEvent
10. ServletException
11. UnavailableException

Interfaces in [Link] package


There are many interfaces in [Link] package. They are as follows:

1. HttpServletRequest
2. HttpServletResponse
3. HttpSession
4. HttpSessionListener
5. HttpSessionAttributeListener
6. HttpSessionBindingListener
7. HttpSessionActivationListener
8. HttpSessionContext (deprecated now)

Classes in [Link] package


There are many classes in [Link] package. They are as follows:

1. HttpServlet
2. Cookie
3. HttpServletRequestWrapper
4. HttpServletResponseWrapper
5. HttpSessionEvent
6. HttpSessionBindingEvent
7. HttpUtils (deprecated now)
Deployment Descriptor:
In a java web application a file named [Link] is known as
deployment descriptor. It is a xml file and <web-app> is the root
element for it. When a request comes web server uses [Link] file
to map the URL of the request to the specific code that handle the
request.

Sample code of [Link] file:


<web-app>

<servlet>
<servlet-name>servletName</servlet-name>
<servlet-class>servletClass</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>servletName</servlet-name>
<url-pattern>*.*</url-pattern>
</servlet-mapping>

</web-app>
How [Link] works:
When a request comes it is matched with url pattern in servlet
mapping attribute. In the above example all urls mapped with the
servlet. You can specify a url pattern according to your need. When
url matched with url pattern web server try to find the servlet name
in servlet attributes same as in servlet mapping attribute. When
match found control is goes to the associated servlet class.

Servlet “Hello World” example by


extending HttpServlet class.

[Link]
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

/**
* This servlet program is used to print "Hello World"
* on client browser using HttpServlet class.
* @author w3spoint
*/
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;

//no-argument constructor.
public HelloWorld() {

protected void doGet(HttpServletRequest request,


HttpServletResponse
response) throws ServletException, IOException {
[Link]("text/html");
PrintWriter out = [Link]();

[Link]("<h1>Hello World using HttpServlet


class.</h1>");
[Link]();
}
}
[Link]
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="[Link]
xmlns:xsi="[Link]
xsi:schemaLocation="[Link]
[Link]

<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>
[Link]
</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>

</web-app>
Output:

You might also like