Solved

Running simple servlet

Posted on 2004-09-26
33
733 Views
Last Modified: 2013-11-24
Hi Experts,

     I have installed "j2eesdk-1_4_01-windows.exe" and I have setup the path in Autoexe.bat like given below

 set path =  C:\WINDOWS;C:\WINDOWS\COMMAND;D:\WINDOWS\COMMAND;C:\Sun\AppServer\jdk\bin;C:\Sun\AppServer\bin

set classpath = .;C:\Sun\AppServer\jdk\lib;C:\Sun\AppServer\lib\j2ee.jar

set JAVA_HOME = C:\Sun\AppServer\jdk\bin
set J2EE_HOME = C:\Sun\AppServer\bin

I can compile and run a java program but I am not able to compile a simple servlet program.

Sample :

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

public class HelloWorld extends HttpServlet
 {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Hello World!</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Hello World!</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

I am getting the following Error message
import javax.servlet.http.*;
^
HelloWorld.java:5: cannot resolve symbol
symbol  : class HttpServlet
location: class HelloWorld
public class HelloWorld extends HttpServlet
                                ^
HelloWorld.java:8: cannot resolve symbol
symbol  : class HttpServletRequest
location: class HelloWorld
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                      ^
HelloWorld.java:8: cannot resolve symbol
symbol  : class HttpServletResponse
location: class HelloWorld
    public void doGet(HttpServletRequest request, HttpServletResponse response)
                                                  ^
HelloWorld.java:9: cannot resolve symbol
symbol  : class ServletException
location: class HelloWorld
    throws IOException, ServletException
                        ^
6 errors

Pls guys help me to sort out ....

Thanks in advance




0
Comment
Question by:dhana77
  • 12
  • 10
  • 6
  • +1
33 Comments
 
LVL 12

Expert Comment

by:Giant2
Comment Utility
It seems you do not have the correct jar (of J2EE) in your classpath.
0
 
LVL 12

Expert Comment

by:Giant2
Comment Utility
put the servlet-api.jar in your classpath.
Bye, Giant.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
Comment Utility
>>
set JAVA_HOME = C:\Sun\AppServer\jdk\bin
set J2EE_HOME = C:\Sun\AppServer\bin
>>

Those should probably be

set JAVA_HOME = C:\Sun\AppServer\jdk
set J2EE_HOME = C:\Sun\AppServer

(check the docs)

and then

set CLASSPATH=.;%J2EE_HOME%\lib\j2ee.jar

(and whatever else is needed)

Check the the class(es) needed *are* in that jar with

cd %J2EE_HOME%\lib
jar tvf j2ee.jar | find "HttpServlet"
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Copy the j2ee jar into your JDK's 'ext' directory.
That way it will be automatically included in your classpath.
0
 

Author Comment

by:dhana77
Comment Utility
hi CEHJ ,

    It is not working ,I dont know why?Is there any other way to do...

    please do reply.

   Thanks in advance
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
If it doesn't work then why did you accept the answer?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
What isn't working? What's the problem?
0
 

Author Comment

by:dhana77
Comment Utility


I have done according to ur procedure ,I reboot the machine and I compiled the program.

I got the same error message
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Did u try what I suggested?

What servlet container are you using?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
your container will come with a jar containing the classes you requjire, which you can just copy to your jdk's ext directory (<jdk>/lib/jre/ext)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>I reboot the machine and I compiled the program.

Well unless you put those commands in a script (batch file) then of course the environment variables will be gone after reboot
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
e.g. what's the output of

%JAVA_HOME%\bin\java

?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility

My suggestion would require no additional scripts or changes to the environment.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
The value of JAVA_HOME has nothing to do with your problem.
0
 

Author Comment

by:dhana77
Comment Utility

>>your container will come with a jar containing the classes you requjire, which you can just copy to your jdk's ext directory (<jdk>/lib/jre/ext)

from where to where I have to copy
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
depends what you want to use, I'd suggest using the servlet jar from your container eg. if using tomcat use servlet.jar from tomcat.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 92

Expert Comment

by:objects
Comment Utility
to use the J2EE jar(s), copy them from J2EE_HOME%\lib.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Presumably someone or something told you to create those environment variables? Simply ensuring you can compile by moving jars around will only solve the problem temporarily. You will probably get other dependency problems later
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
What's the output of the command i mentioned dhana?
0
 

Author Comment

by:dhana77
Comment Utility
sorry I Dont know...
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> Simply ensuring you can compile by moving jars around will only solve the problem temporarily.

Incorrect.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>sorry I Dont know...

? Why not?

>>Incorrect.

So what's going to happen when the program that relies on J2EE_HOME being set finds that it isn't?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> So what's going to happen when the program that relies on J2EE_HOME being set finds that it isn't?

Compiling does not rely on J2EE_HOME
The problem is with the required classes not being available to the class loader.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>Compiling does not rely on J2EE_HOME

Do you imagine i'm not aware of that? Other things will rely on it, or it wouldn't have been required to be set
0
 
LVL 12

Expert Comment

by:Giant2
Comment Utility
This post is closed, isn't it? And the solution was NOT:
>It seems you do not have the correct jar (of J2EE) in your classpath

Why continue in discussion?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
the problem hasn't been solved, I have no idea why that comment was accepted.
0
 
LVL 12

Expert Comment

by:Giant2
Comment Utility
put the jars (j2ee.jar, servlet-api.jar and others) in the common library of the appServer
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
The solution, as i stated, is to set *all* the environment variables correctly or dhana77 will have problems
0
 

Author Comment

by:dhana77
Comment Utility
Hi experts,

          I sloved the problem..I can complile without any problem.
       
          Now I am using TOMCAT.Could you tell me how to deploy the servlet

         Thank you.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> I sloved the problem..I can complile without any problem.
 
what was the problem?
     
>  Now I am using TOMCAT.Could you tell me how to deploy the servlet

http://www.theserverside.net/developmentor/articles/tomcat/article.html#A2
0
 

Author Comment

by:dhana77
Comment Utility
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now