Showing posts with label Servlet. Show all posts
Showing posts with label Servlet. Show all posts

Wednesday, 20 March 2013

'package javax.servlet.http does not exist' Solution

This is a common error which occurs when trying to compile a web project. This package is usually provided by the container. However, it is not (always) available at compile time and one should not include it in the final .war too.

If you are using Maven, the solution is simple. Add the following dependency with the provided scope:
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>
Your IDE will be able to compile your project and won't include the library in your .war.

You can also use the following dependency:
<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>6.0</version>
    <type>jar</type>
    <scope>provided</scope>
</dependency>

Monday, 13 August 2012

What is web.xml (Deployment Descriptor) in Java?

In Java, all web applications have a web.xml file, also called deployment descriptor, in the \META-INF directory. The main purpose of this file is to provide information about the web application to deploy in the container.

It can be used to declare and configure servlets belonging to the application via descriptor elements. For example, tt helps setting filters, icons or define default welcome pages too.

Here is a web.xml example:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app 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"
 version="2.4">

  <display-name>My Application</display-name> 
  <description>My application description</description>

  <servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>somepackage.MyServletClass</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/MyServletMapping</url-pattern>
  </servlet-mapping>

</web-app>
It defines a web application with one servlet called MyServlet. User requests to:

  http://www.mywebserver.com/MyAppPath/MyServletMapping/...

are sent to MyServlet. The MyAppPath application path is typically defined in the context.xml file. Both files serve different purpose and should not be confused with each other.