We help IT Professionals succeed at work.

Servlet is calling the wrong class

I have attached my project, my problem is my project is calling my servlet from the wrong path, Project/html/AddData, for example this however is wrong.

I was intially calling my servlet in the default package without a web.xml file, the form gave the address /Project/servlet/AddData. This worked.

I then changed it to the package to com.project.controller, it then stopped working. I assumed this was due to the lack of a deployment descriptor "web.xml" file. I added this file, changed the form address to "AddData". This still isnt working, I am missing a step or have I made a mistake somewhere. I attached the relevant files
<HTML>
<HEAD>
<TITLE>Registration Form</TITLE>
<link rel="stylesheet" type="text/css" href="/Project/html/Style.css"/>
</HEAD>
 
<BODY>

<FORM METHOD="POST" 
   ACTION="AddData" 
   name="myform"> 

<H3>Choose and Insert GPSData:</H3>
 

<SELECT NAME = "gpsdata">
        <OPTION>$GPRMC,194932.530,A,5049.5330,N,00109.9218,W,0.00,131.16,170206,,,D*7A</OPTION>
        <OPTION>$GPRMC,194934.530,A,5049.5330,N,00109.9218,W,0.00,131.16,170206,,,D*7C</OPTION>
        <OPTION>$GPRMC,194936.530,A,5049.5330,N,00109.9218,W,0.00,131.16,170206,,,D*7E</OPTION>
        <OPTION>$GPRMC,194938.529,A,5049.5330,N,00109.9218,W,0.00,131.16,170206,,,D*78</OPTION>
        <OPTION>$GPRMC,194940.529,A,5049.5330,N,00109.9218,W,0.00,131.16,170206,,,D*77</OPTION>
        <OPTION>$GPRMC,194942.528,A,5049.5330,N,00109.9218,W,0.00,131.16,170206,,,D*74</OPTION>
        <OPTION>$GPRMC,194944.528,A,5049.5330,N,00109.9218,W,0.00,131.16,170206,,,D*72</OPTION>
        <OPTION>$GPRMC,194946.528,A,5049.5330,N,00109.9218,W,0.00,131.16,170206,,,D*70</OPTION>
        <OPTION>$GPRMC,194948.527,A,5049.5330,N,00109.9218,W,0.00,131.16,170206,,,D*71</OPTION>
        <OPTION>$GPRMC,194950.527,A,5049.5330,N,00109.9217,W,0.00,131.16,170206,,,D*77</OPTION>
        <OPTION>$GPRMC,194952.527,A,5049.5329,N,00109.9217,W,0.00,131.16,170206,,,D*7D</OPTION>
        <OPTION>$GPRMC,194954.526,A,5049.5329,N,00109.9217,W,0.00,131.16,170206,,,D*7A</OPTION>
        <OPTION>$GPRMC,194956.526,A,5049.5329,N,00109.9217,W,0.00,131.16,170206,,,D*78</OPTION>
        </SELECT>
        
  <BR><INPUT TYPE="submit" value="Submit Form"> 
  <INPUT TYPE="reset" value="Clear Form">
 
 
 
</FORM>
</BODY>
</HTML>

Open in new window

<?xml version = '1.0'?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, 
Inc.//DTD Web Application 2.2//EN" 
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
   <display-name>Deployment Descriptor</display-name>
   <description>Servlet Examples for EE557</description>
	 <servlet>
           <servlet-name>AddData</servlet-name>
           <servlet-class>com.project.controller.AddData</servlet-class>
	 </servlet>
	 <servlet>
           <servlet-name>SelectTable</servlet-name>
           <servlet-class>com.project.controller.SelectTable</servlet-class>
	 </servlet>
	 <servlet>
           <servlet-name>ViewTable</servlet-name>
           <servlet-class>com.project.controller.ViewTable</servlet-class>
	 </servlet>
 </web-app>

Open in new window

package com.project.controller;
import java.io.*;
import java.util.Vector;
import javax.servlet.*;
import javax.servlet.http.*;

public class AddData extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	DBManager a = new DBManager();

	public void doPost(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException 
	{
		res.setContentType("text/html");
		String NMEAData = req.getParameter("gpsdata");
		GPSData b = a.ParseData(NMEAData);
		System.out.println(b.getLong_Dir());
		a.Connect();
		a.Insert(b);
		a.Disconnect();
		res.sendRedirect("/StudentRegistration/index.html");
		
		//System.out.println(b.getLat());
			
		}

	}

Open in new window

Comment
Watch Question

you have missed the servlet mapping in web.xml, without which, the servlet will not be accessible. Please correct as below:

web.xml
========

<?xml version = '1.0'?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems,
Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
   <display-name>Deployment Descriptor</display-name>
   <description>Servlet Examples for EE557</description>
       <servlet>
           <servlet-name>AddData</servlet-name>
           <servlet-class>com.project.controller.AddData</servlet-class>
       </servlet>

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


       <servlet>
           <servlet-name>SelectTable</servlet-name>
           <servlet-class>com.project.controller.SelectTable</servlet-class>
       </servlet>
       <servlet>
           <servlet-name>ViewTable</servlet-name>
           <servlet-class>com.project.controller.ViewTable</servlet-class>
       </servlet>
 </web-app>

Author

Commented:
Ok I have edited my web.xml file to the attached code snippet I still get the below error. I have  a feeling I am doing something elementary wrong somewhere

HTTP Status 404 - /Project/html/AddData

type Status report

message /Project/html/AddData

description The requested resource (/Project/html/AddData) is not available.
Apache Tomcat/6.0.24
<?xml version = '1.0'?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, 
Inc.//DTD Web Application 2.2//EN" 
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
   <display-name>Deployment Descriptor</display-name>
   <description>Servlet Examples for EE557</description>
	
	 <servlet>
           <servlet-name>AddData</servlet-name>
           <servlet-class>com.project.controller.AddData</servlet-class>
	 </servlet>
	 <servlet-mapping>
             <servlet-name>AddData</servlet-name>
             <url-pattern>/AddData</url-pattern>
              </servlet-mapping>
              
	 <servlet>
           <servlet-name>SelectTable</servlet-name>
           <servlet-class>com.project.controller.SelectTable</servlet-class>
	 </servlet>
	  <servlet-mapping>
             <servlet-name>SelectTable</servlet-name>
             <url-pattern>/SelectTable</url-pattern>
	    </servlet-mapping>
	    
	 <servlet>
           <servlet-name>ViewTable</servlet-name>
           <servlet-class>com.project.controller.ViewTable</servlet-class>
	 </servlet>
           <servlet-mapping>
             <servlet-name>ViewTable</servlet-name>
             <url-pattern>/ViewTable</url-pattern>
           </servlet-mapping>
	 
 </web-app>

Open in new window

folder1.png

Author

Commented:
Sorry second image of the folder layout didnt attach
folder2.png
Please check your project output (deployed) folder and it should match with the attached image
proj-out.JPG
lib folder should contain all jar used.

Author

Commented:
Yeah that seems to match what you have there
folder3.png

Author

Commented:
I have corrected that there, doesnt make a difference
folder4.png

Author

Commented:
After god knows how long messing around with my classes, servlets changing names checking everything trying countless combinations I noticed that the path wasnt changing. Now I was restarting Tomcat but for some reason I just chanced deleting the browser history and bingo it magically started to work. Is this a possible reason I have been tearing my hair out.

Anyway im crediting the success of this to the web.xml file you sent me earlier as being the reason it now works just for some reason it didnt at the time :(

I am going to post the form, servlet, and web.xml in their final form just in case anyone wants to see them.
Thanks guys for the help I appreciate the patience.
Servlet,

package com.project.controller;
import java.io.*;
import java.util.Vector;
import javax.servlet.*;
import javax.servlet.http.*;

import com.project.model.DBManager;
import com.project.model.GPSData;

public class ViewCurrentLocation extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	DBManager a = new DBManager();

	public void doPost(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException 
	{
		res.setContentType("text/html");
		a.Connect();
		GPSData GetData = new GPSData();
		GetData = a.getCurrentData();
		req.setAttribute("GetData",GetData);
		req.getRequestDispatcher("/html/CurrentLocation.jsp").forward(req, res);

			
		}
	}
		
Form,

<HTML>
<HEAD>
<TITLE>Registration Form</TITLE>
<link rel="stylesheet" type="text/css" href="/Project/html/Style.css"/>
</HEAD>
 
<BODY>

<FORM METHOD="POST" 
   ACTION="/Project/ViewCurrentLocation" 
   name="ViewCurrentLocation"> 

<H3>Choose and Insert GPSData:</H3>
 
        
  <BR><INPUT TYPE="submit" value="Submit Form"> 
  <INPUT TYPE="reset" value="Clear Form">
 
 
 
</FORM>
</BODY>
</HTML>

web.xml,

<?xml version = '1.0'?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, 
Inc.//DTD Web Application 2.2//EN" 
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
   <display-name>Deployment Descriptor</display-name>
   <description>Servlet Deployment</description>
	
	 <servlet>
           <servlet-name>AddData</servlet-name>
           <servlet-class>com.project.controller.AddData</servlet-class>
	 </servlet>
	 <servlet-mapping>
             <servlet-name>AddData</servlet-name>
             <url-pattern>/AddData</url-pattern>
              </servlet-mapping>
	    
	 <servlet>
           <servlet-name>ViewCurrentLocation</servlet-name>
           <servlet-class>com.project.controller.ViewCurrentLocation</servlet-class>
	 </servlet>
           <servlet-mapping>
             <servlet-name>ViewCurrentLocation</servlet-name>
             <url-pattern>/ViewCurrentLocation</url-pattern>
           </servlet-mapping>
	 
 </web-app>

Open in new window

Author

Commented:
See my last comment. Thanks again.