Saska
Saska

Reputation: 1021

How to add classes in web.xml?

I made a login form with this lesson: http://bit.ly/eEcVrE

I install tomcat and mysql, create database and tables. But i cant understanding web.xml. Advise how to fill it, or give references to tutorials about web.xml

Login.jsp starts, but an error when i fill field username and password: The requested resource (/project1/LoginServlet) is not available.

sorry for very-very stupid question, but i really dont know what do. and sorry for bad english. Thanks.

Upvotes: 1

Views: 10352

Answers (2)

Edwin Buck
Edwin Buck

Reputation: 70909

You don't actually add classes to the web.xml, nor do you need to indicate their locations within the web.xml file.

Add them to the WEB-INF/classes sub-directory of your war file (in their appropriate hierchial directory nesting), or if the classes are already packed into JAR files, add them to the WEB-INF/lib sub-directory of your war file.

The web.xml file contains two very important maps.

  1. It maps the URL pattern to a Servlet Name.
  2. It maps the Servlet Name to a class.

Once you are sure the class is within the WAR file, you need to verify that you are calling the correct URL. An entry like

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

means that calling

http://myserver:port/webappName/HelloServlet

will be redirected to the "HelloServlet" servlet. An entry like

 <servlet>
     <servlet-name>HelloServlet</servlet-name>
     <servlet-class>mypackage.HelloServlet</servlet-class>
 </servlet>

will make sure that all requests to the "HelloServlet" servlet are directed to the class mypackage.HelloServlet, which might be located in the WAR's internal WEB-INF\classes\mypackage\HelloServlet.class. Alternatively it might be located in the WAR's internal WEB-INF\lib\myjar.jar file, provided the myjar.jar file contains a mypackage\HelloServlet.class file.

Good luck!

Upvotes: 5

Jigar Joshi
Jigar Joshi

Reputation: 240908

web.xml is web - application descriptor.

The web.xml file provides configuration and deployment information for the Web components that comprise a Web application.


Login.jsp starts, but an error when i fill field username and password: The requested resource (/project1/LoginServlet) is not available.

It is because you haven't mapped this URL pattern in web.xml

Upvotes: 1

Related Questions