This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Classpath problem.

 
Andrew Broussard
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have the following entries in my enviornment variables.

"JAVA_HOME" value= "c:\j2sdk1.4.2_11"
"PATH" value= "c:\j2sdk1.4.2_11\bin"

for Java.
For servlets,

"Servlet CLASSPATH" value="C: \Tomcat5.5\common\lib\servlet-api.jar"

And now I want to compile a small servlet application with following three classes.My problem is that i don't know where to put the files and do I have to make further changes to class path.I have tried hard but without success.Thanks alot guys,please help me out,regards
Andrew



here are the three classes and web.xml file.

package com.example;
import javax.servlet.*;


public class MyServletContextListener implements ServletContextListener
{
public void contextInitialized(ServletContextEvent event)
{
ServletContext sc=event.getServletContext();
String dogBreed=sc.getServletContext("breed");
Dog d=new Dog(dogBreed);
sc.setAttribute("dog",d);
}


public void contextDestroyed(ServletContextEvent event)
{
}
}





package com.example;


public class Dog
{
private String breed;

public Dog(String breed)
{
this.breed;
}

public String getBreed()
{
return breed;
}
}




package com.example;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;


public class ListenerTester extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("test context attributes set by listener<br>");
out.println("<br>");
Dog dog=(Dog) getServletContext().getAttribute("dog");
out.println("Dog's breed is: " + dog.getBreed());
}
}




web.xml


<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/web-app_2_4.xsd"
version="2.4"

<servlet>
<servlet-name>ListenerTester</servlet-class>
<servlet-class>com.example.ListenerTester</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>ListenerTester</servlet-name>
<url-pattern>/ListenerTest.do</url-pattern>
</servlet-mapping>

<context-param>
<param-name>breed</param-name>
<param-value>Great Dane</param-value>
</context-param>

<listener>
<listener-class>
com.example.MyServletContextListener
</listener-class>
</listener>
</web-app>
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andrew Broussard:
I have the following entries in my enviornment variables.

"JAVA_HOME" value= "c:\j2sdk1.4.2_11"
"PATH" value= "c:\j2sdk1.4.2_11\bin"

for Java.
For servlets,

"Servlet CLASSPATH" value="C: \Tomcat5.5\common\lib\servlet-api.jar"

And now I want to compile a small servlet application with following three classes.My problem is that i don't know where to put the files and do I have to make further changes to class path.I have tried hard but without success.Thanks alot guys,please help me out,regards
Andrew

Have you compiled a "HelloWorld" plain old java application yet? It sounds like you're trying to jump into the deep water before learning to float.

I'm not familiar with "Servlet CLASSPATH". How does it relate to your (already existing?) CLASSPATH?
 
Andrew Broussard
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have successfully done that helloWorld and alot of plain java programs.My problem is how we need to adjust the class path variables for servlets and where to keep the different files of servlet code.Thankyou.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are saying that you need different classpaths when compiling different projects, then don't use the CLASSPATH environment variable. Instead use the -classpath option of javac.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally, you don't worry at all about the CLASSPATH for servlets. Or, rather, that CLASSPATH is dependent on the Server.

A web application is usually stored in a WAR, but the exploded structure is the same:

/WEB-INF/classes/<This is where your classes go>
/WEB-INF/lib/<This is where your .jar files go>

Search for WAR files for examples.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this thread will feel more at home in the Tomcat forum...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic