Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Running simple servlet

Posted on 2004-09-26
33
Medium Priority
?
775 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 10
  • 6
  • +1
33 Comments
 
LVL 12

Expert Comment

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

Expert Comment

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

Accepted Solution

by:
CEHJ earned 1500 total points
ID: 12158190
>>
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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
LVL 92

Expert Comment

by:objects
ID: 12158446
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
ID: 12165373
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
ID: 12165386
If it doesn't work then why did you accept the answer?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12165414
What isn't working? What's the problem?
0
 

Author Comment

by:dhana77
ID: 12165420


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
ID: 12165423
Did u try what I suggested?

What servlet container are you using?
0
 
LVL 92

Expert Comment

by:objects
ID: 12165429
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
ID: 12165436
>>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
ID: 12165446
e.g. what's the output of

%JAVA_HOME%\bin\java

?
0
 
LVL 92

Expert Comment

by:objects
ID: 12165454

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

Expert Comment

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

Author Comment

by:dhana77
ID: 12165461

>>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
ID: 12165476
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
 
LVL 92

Expert Comment

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

Expert Comment

by:CEHJ
ID: 12165493
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
ID: 12165497
What's the output of the command i mentioned dhana?
0
 

Author Comment

by:dhana77
ID: 12165511
sorry I Dont know...
0
 
LVL 92

Expert Comment

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

Incorrect.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 12165538
>>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
ID: 12165547
> 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
ID: 12165560
>>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
ID: 12166814
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
ID: 12166889
the problem hasn't been solved, I have no idea why that comment was accepted.
0
 
LVL 12

Expert Comment

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

Expert Comment

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

Author Comment

by:dhana77
ID: 12176297
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
ID: 12176319
> 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 12177696
:-)
0

Featured Post

Vim Reference Guide

Vim is a powerful text editor favored by many sysadmins and developers - here are some commands that you'll want to keep in your back pocket!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
A solution for Fortify Path Manipulation.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

688 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