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; } }
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
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: 35257
7
posted
0
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 ]
* 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: 35257
7
posted
0
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.