This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes Servlet can't find other servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet can Watch "Servlet can New topic
Author

Servlet can't find other servlet

james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32
I have 3 servlets, S1, S2, S3. and a java class Data.

S3 can't find S1 and its symbols.

All these servlets, classes and build.xml are in a same folder ( WEB-INF/src/
( is this the reason that package S1; won't work ) Then how?


Compiler says that S1 and S2 are ok, but S3; there are 4 errors.

And when I try ( S2 works fine ) to open url S3 a web browser says;

The requested resource.... is not available.


S1



S2





S3
// I believe the problem is JUST in S3. I think that S1 and S2 are ok.








Compiler says 4 errors;


S3.java:15: cannot find symbol
[javac] symbol: class S1
[javac] public class S3 extends S1 {
[javac] ^

S3.java:64: cannot find symbol
[javac] symbol : variable db
[javac] location: class S2.S3
[javac] db = Data.getConnection();
[javac] ^

S3.java:64: cannot find symbol
[javac] symbol : variable Data
[javac] location: class S2.S3
[javac] db = Data.getConnection();
^

S3.java:65: cannot find symbol
[javac] symbol : variable db
[javac] location: class S2.S3
[javac] Statement stmt6 = db.createStatement();
[javac] ^
[javac] 4 errors




And I have a class Data and method getConnection is in it;





Please, help. Can someone solve this? Why S3 can't find S1 and its symbols?? What on earth is wrong??

What if I put these in WEB-INF/src/newFolder?? - does it have any meaning?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

Welcome to the Ranch.

There are two things you can do to help you get better answers here:

1) Be sure to take the time to compose descriptive subjects for your posts; read this for more information.

A title such as "can you please help" is worse than useless. What would happen if all posts had such a title?

Please go back and change your post to add a more meaningful subject by clicking the button on your post.

2) Be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

While this probably isn't the cause of any problems, why is your source in WEB-INF? That's really odd. Your source shouldn't even be part of the deployed web application.

If you are trying to make your source public, there are better means of distribution.

With regards to your compilation problems, once you've reformatted your post with code tags, maybe someone will spot the issue. But one thing that's evident right off the bat: your classes should be in a package other than the default package.
Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
Hi,

S3.java:15: cannot find symbol
[javac] symbol: class S1
[javac] public class S3 extends S1 {
[javac] ^

While S3 is in package S1, others have no package declared ... or is it just a typo?
How about the source structure in WEB-INF?

S3.java:64: cannot find symbol
[javac] symbol : variable db
[javac] location: class S2.S3
[javac] db = Data.getConnection();
[javac] ^

S3.java:64: cannot find symbol
[javac] symbol : variable Data
[javac] location: class S2.S3
[javac] db = Data.getConnection();
^

S3.java:65: cannot find symbol
[javac] symbol : variable db
[javac] location: class S2.S3
[javac] Statement stmt6 = db.createStatement();
[javac] ^
[javac] 4 errors

Well, if you would like to get an answer, you should also provide problematic lines of code.


Regards,
Rok


SCJP, SCWCD
james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32
Thanks. I see. Well, if it doesn't ( ? ) matter, I would keep these all in WEB-INF/src

I had just one package. I wasn't sure about packages...

should I put package also in servlet S2, like this;




And here's almost the whole code of S1;

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

You seem to be confused between packages and classes. Theses are core Java concepts that you must understand before diving into advanced topics like servlets.

And as of JDK 1.4 the default package cannot be imported. So put all your classes in a named package, always.

james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32
Thanks!

I see. This was new to me.

So like this, for example, I create a folder named MyPackage in WEB-INF/src.

And then I put all my classes in WEB-INF/src/MyPackage ?


And then I write MyPackage; in S2 and S3? or also in S1


Anything else, code ok?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

james smitho wrote:And then I put all my classes in WEB-INF/src/MyPackage ?

Depends what you mean by "classes". Class files? No. Those go in the package hierarchy in WEB-INF classes.

Java source files? Again, why are they under WEB-INF at all?
james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32


Thanks again for answering and telling me.
Sorry, I meant Java source files. under WEB-INF ; - I just thought... Because of Tomcat...
I don't have much an experience, and I just thought easy way.

Well, what about tomcat/webapps/myfolder - would this be ok, at least better?

Should I change the codes?, I guess they are ok.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

james smitho wrote:Well, what about tomcat/webapps/myfolder - would this be ok, at least better?

No. In fact, much worse.

Your sources have nothing to do with Tomcat and shouldn't be anywhere near Tomcat. Only the final .class files are relevant to the deployment.
james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32
so is WEB-INF/src the reason ( and that package- thing ) why S3 can't find S1? they both or package?

Any suggestions, where I might put these java-files??

I want to learn these things.
james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32
Yes, much better now! Thank you! But just one error;

S3 can't find a symbol db in S1?


cannot find symbol
[javac] symbol : variable db
[javac] location: class mypackage.S3

[javac] db = Data.getConnection();
^


I put these java-files and build.xml under the package mypackage;

and I wrote package mypackage; in every file.

AND when I ( this problem was before, I forgot to say ) try to open a S3's webpage, a web browser says:


The requested resource (/tomcat....servlet/S3) is not available.

But that's surely because of that db, right? How do I correct that last error? Please.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

james smitho wrote:Any suggestions, where I might put these java-files?

You might want to start a topic in the Beginning Java forum asking how people set up their projects.

Me, I simply have a folder that servers as my development root. I create folders in it for each project.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

james smitho wrote:But that's surely because of that db, right? How do I correct that last error?

Well, yeah. If the servlet doesn't compile, it sure as heck isn't going to be able to run.

Can't tell what the issue is from what you've posted.
PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
Hai First if you are using tomcat this is how you must deploy your webb application..I will take you thorugh these steps okay

1) you have webapps directory.......Create a folder with your project name
2) inside the project you have a WEB-INF directory which contains a folder named classes and a file web.xml(i am sure you know how to use the dd file)
3) all your sevlet codes must go the classes folder........
4)there you may have as many packages as you like....

if you have to know more about packages read java2 complete reference it helped me a lot.......

from your explanation i believe your deployment itself is wrong

i think this may help


To err is human,
To forgive is not company policy
Samrat Debroy
Greenhorn

Joined: Oct 23, 2009
Posts: 13
james smitho wrote:
But that's surely because of that db, right? How do I correct that last error? Please.


I believe if you are a beginner then it will be helpful if you use some j2ee IDE(like Eclipse Galileo) for the structure.
Also just check whether you configured your servlet in deployment descriptor (/WEB-INF/web.xml).
james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32
Thank you all! You were very friendly. I must read what you said.

variable db has found, I just defined it...


S3 compiles now!!


But Just one error, from a web browser;

The requested resource (/tomcat....servlet/S3) is not available. S2 is available.

Yes, I had and have folder MyProject and inside that is WEB-INF.

I had also some old classes in WEB-INF/classes. I have a S1.class in there,
BUT NOT S2.

Now I must put these classes that I want in WEB-INF/classes.

And WEB-INF/src/mypackage

contains web.xml and java-files.


Something in web.xml;




I believe this a small problem, I must have S3 available with my web browser. Indeed, S2 is available.


james smitho
Ranch Hand

Joined: Nov 16, 2009
Posts: 32

Hi again.

now I have also S3 in WEB-INF/classes.

Now web browser says about S3;


The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Cannot allocate servlet instance for path /tomcat//MyProject/servlet/S3
org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:388)
org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:133)
....
root cause

java.lang.NoClassDefFoundError: S3 (wrong name: S2/S3)


How should I correct this? Please.
PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
Actually i dont know anything about src and others

I did my first project in tomcat in the address webapps/(projectname)/web-inf/classes/(all my servlet classses)

all your servlet classes must be mapped to the web.xml file

If you have ant doubts regaring that please check this link

http://www.roseindia.net/servlets/HelloWorld.shtml

Here if you look at the web.xml file the servlet is mapped under <servlet> tag ... Similarly you must map for all the servlets........

Another important information

it is not good design practice to have more than one servlet in a project....

Imagine which is more secure(a house with single door or house with 3 doors..which is easy to provide security)

Similarly have a single servlet for your entire project.............

Not now but later......(that is a really good programming practice)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

PrasannaKumar Sathiyanantham wrote:it is not good design practice to have more than one servlet in a project....

Huh? I never heard that, and completely disagree. You can have as many servlets as you want, and no good practices have been violated.

That said, use of a Front Controller can help structure a web app and keep the number of servlets under control, but you can still have as many servlets as you like.
PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
What i taught was that there must be only one action controller ..................

Hence you may have as many servlets as you want but all must be called only from one servlet......

I am extremely sorry about the previous message
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

While the Front Controller pattern is a good one to follow -- and can prevent a proliferation of too many servlets -- it's not the only type of servlet in a web application.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Servlet can't find other servlet
 
Similar Threads
How to store a MessageDigest object in Oracle Database
unable to compile servlet
how to resolve the problem with Deploying this war file ?
cannot find symbol error
can't define a variable