Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 777
  • Last Modified:

Running simple servlet

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
dhana77
Asked:
dhana77
  • 12
  • 10
  • 6
  • +1
1 Solution
 
Giant2Commented:
It seems you do not have the correct jar (of J2EE) in your classpath.
0
 
Giant2Commented:
put the servlet-api.jar in your classpath.
Bye, Giant.
0
 
CEHJCommented:
>>
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
objectsCommented:
Copy the j2ee jar into your JDK's 'ext' directory.
That way it will be automatically included in your classpath.
0
 
dhana77Author Commented:
hi CEHJ ,

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

    please do reply.

   Thanks in advance
0
 
objectsCommented:
If it doesn't work then why did you accept the answer?
0
 
CEHJCommented:
What isn't working? What's the problem?
0
 
dhana77Author Commented:


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

I got the same error message
0
 
objectsCommented:
Did u try what I suggested?

What servlet container are you using?
0
 
objectsCommented:
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
 
CEHJCommented:
>>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
 
CEHJCommented:
e.g. what's the output of

%JAVA_HOME%\bin\java

?
0
 
objectsCommented:

My suggestion would require no additional scripts or changes to the environment.
0
 
objectsCommented:
The value of JAVA_HOME has nothing to do with your problem.
0
 
dhana77Author Commented:

>>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
 
objectsCommented:
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
 
objectsCommented:
to use the J2EE jar(s), copy them from J2EE_HOME%\lib.
0
 
CEHJCommented:
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
 
CEHJCommented:
What's the output of the command i mentioned dhana?
0
 
dhana77Author Commented:
sorry I Dont know...
0
 
objectsCommented:
> Simply ensuring you can compile by moving jars around will only solve the problem temporarily.

Incorrect.
0
 
CEHJCommented:
>>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
 
objectsCommented:
> 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
 
CEHJCommented:
>>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
 
Giant2Commented:
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
 
objectsCommented:
the problem hasn't been solved, I have no idea why that comment was accepted.
0
 
Giant2Commented:
put the jars (j2ee.jar, servlet-api.jar and others) in the common library of the appServer
0
 
CEHJCommented:
The solution, as i stated, is to set *all* the environment variables correctly or dhana77 will have problems
0
 
dhana77Author Commented:
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
 
objectsCommented:
> 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
 
dhana77Author Commented:
0
 
CEHJCommented:
:-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 12
  • 10
  • 6
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now