aspose file tools*
The moose likes Servlets and the fly likes What's wrong with my basic servlet code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "What Watch "What New topic
Author

What's wrong with my basic servlet code

Gangadhar Reddy
Greenhorn

Joined: Aug 13, 2007
Posts: 25
Hi every one, I have a code as follows:

web.xml
-------

<web-app>

<servlet>
<servlet-name>index</servlet-name>
<servlet-class>com.example.FirstServlet</servlet-class>

<init-param>
<param-name>NAME</param-name>
<param-value>VALUE</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern>/form01</url-pattern>
</servlet-mapping>

<contex-param>
<param-name>breed</param-name>
<param-value>Dober man</param-value>
</contex-param>

<listener>
<listener-class>com.example.MyServletContextListener</listener-class>
</listener>

</web-app>

Dog.java
---------
package com.example;

public class Dog
{
private String breed = "";
public Dog(String breed)
{
this.breed = breed;
System.out.println(" The value of breed is :" + breed);
}
public String getBreed()
{
return breed;
}
}

MyServletContextListener.java
-----------------------------
package com.example;

import javax.servlet.*;
import javax.servlet.http.*;
import com.example.Dog;

public class MyServletContextListener implements ServletContextListener
{
public void contextInitialized(ServletContextEvent event)
{
System.out.println("line 01");
ServletContext sc = event.getServletContext();
String dogBreed = sc.getInitParameter("breed");
System.out.println("The breed is" + dogBreed);
Dog d = new Dog(dogBreed);
sc.setAttribute("dog",d);
}
public void contextDestroyed(ServletContextEvent event)
{

}
}

FirstServlet.java
-----------------
package com.example;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import com.example.Dog;

public class FirstServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Checking init parameters " + getServletConfig().getInitParameter("NAME"));
Dog dog = (Dog)getServletContext().getAttribute("dog");
out.println("Dogs breed is " + dog.getBreed());
}
}

I had set my classpath and path as follows on my windows XP machine as
My computer>properties>Advanced>Environment Variables

VARIABLE VALUE
classpath C:\Tomcat5.0\common\lib\servlet-api.jar;.
PATH C:\j2sdk1.4.2_13\bin

The problem what I'm finding is as follows:

When I compile Dog.java from directory C:\Tomcat5.0\webapps\SamplePgms\WEB-INF with following command it will compile

javac -d classes src\Dog.java

But when i try to compile MyServletContextListener.java from directory
C:\Tomcat5.0\webapps\SamplePgms\WEB-INF with following command, it is generating an error

javac -d classes src\MyServletContextListener.java

It's generating following error

"can not resolve symbol class Dog"

However if I try to compile all files at a time with

javac -d classses *.java

then all files will be compiled.

WHAT'S WRONG WITH MY CODE?

If not, My second question is, why do I will get output as follows when I deployed my code on Tomcat {consider all above files were working)

Output:

line 01
The breed is null
The value of breed is null

PLEASE HELP ME OUT
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
Please UseCodeTags when posting code of any length. It is very hard to read, and make sense of, as it is.

There's nothing wrong with your code. Since the MyServletContextListener depends on Dog, Dog needs to be available to the compiler when MyServletContextListener is compiled. With the command line you're using (which does not include Dog in the classpath), it is not.

As to the other problem, you have a typo in the element called "contex-param" in the web.xml file.
[ December 02, 2007: Message edited by: Ulf Dittmer ]

Ping & DNS - updated with new look and Ping home screen widget
Puneet Mathur
Greenhorn

Joined: Dec 02, 2007
Posts: 3
No problem with the code.
You simply need to compile all three files in one go.
com/example/*.java
Gangadhar Reddy
Greenhorn

Joined: Aug 13, 2007
Posts: 25
Thank you very much Ulf Dittmer.

You were saying that "MyServletContextListener depends on Dog, Dog needs to be available to the compiler when MyServletContextListener is compiled".

Does you mean that Dog class has to be available or Dog object has to be available when MyServletContextListener is compiled?

Ulf Dittmer, could you please make some time to clarify even this doubt too

javac -d classes src\MyServletContextListener.java

This is what I understood from above statment.

* Go to MyServletContextListener.java in src folder/directory and create com.example packages in classes directory/folder
* Now compile MyServletContextListener.java FROM src folder & THEN keep .class file in classes\com\example

Here the doubt what I have is:

Is MyServletContextListener.java compiled FROM src or is it compiled FROM classes\com\example ?

If this is true, then I think Dog.class has to be available?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
Does you mean that Dog class has to be available or Dog object has to be available when MyServletContextListener is compiled?

The former; the compiler does not create objects of the classes it compiles.

Is MyServletContextListener.java compiled FROM src or is it compiled FROM classes\com\example ?

It doesn't matter where the source file is, and where it is compiled. The class needs to available during compilation, so if it is in WEB-INF/classes/com/example, then the "classes" directory must be part of the classpath.

As suggested above, you can avoid this by compiling both classes at the same time.
Gangadhar Reddy
Greenhorn

Joined: Aug 13, 2007
Posts: 25
Excellent Ulf Dittmer. Very impressive solution
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What's wrong with my basic servlet code
 
Similar Threads
Problem importing Dog class
ContextListener
ListenerTester
about error code
Hi, package compiling question...