• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to create an instance of another class in a servlet  RSS feed

 
Sneha Prasad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am new to servlets. I was going through the book Head First Servlets and JSP and in chapter 3 ...Servlet version 2 ....when I am trying to compile the BeerSelect.java it is giving error that package com.example.model does not exist.
If anyone can explain me the possible reasons for this.
I have already created the BeerSelect.java as foolows:




and the BeerExpert.java file as:




I am using tomcat7 and i m not using eclipse or any other IDE.
Please help.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right the issue is with the invocation of the compiler on the command-line. Lets start with the structure on the file system - what does that look like? Where are the .java files are located and where is the .jar file located that provides the Servlet API dependencies? We can work out how to invoke the javac compiler from there.
 
Saket Kumar Sinha
Ranch Hand
Posts: 32
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

if you are compiling from command line then probably your class file generated should go in the destination directory

For ex :

javac -d ./classes com/example/model/BeerExpert.java

This will generate the class file in classes folder. This folder will have the same hierarchy as the source folder.

For compiling BeerSelect.java

you can follow the same approach just use servlet-api.jar in classpath as you are using Servlet API's

 
Sneha Prasad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Bear Bibeault : Thanks .....I will take care of that from next time.

@Jelle Klap & Saket Kumar Sinha:

The location for BeerExpert.java file is E:\MyProjects\beerV1\src\com\example\model\
for BeerSelect.java file is E:\MyProjects\beerV1\src\com\example\web\
for servlet-api.jar is E:\tomcat7\lib
and the command I am using for compilation is javac -classpath /E:/tomcat7/lib/servlet-api.jar;classes:. -d classes src/com/example/web/BeerSelect.java.

and I am getting 3 errors: one at line3 and two at line 14.
it is saying that package com.exampe.model doesnot exist.

Thanks
 
Sneha Prasad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The BeerSelect.java file is compiling if we replace the colon(":") after classes with semi-colon(;)
javac -classpath /E:/tomcat7/lib/servlet-api.jar;classes;. -d classes src/com/example/web/BeerSelect.java

But I have a doubt.When I was compiling the servlet version 1 as given in the book,it was compiling fine with the colon(":"). So why is the servlet version 2 not compiling?


This is the servlet version 1.

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the difference between colon and semi-colon(;) is that the former is used in unix/linux systems. so i you have to specify classpath or other environement variable on those systems you have to use colons for e.g.

/home/admin:/var/lib/myfiles:. // for linux systems

however on our windows machines the corresponding character for colon is semi-colon(;). so classpath on windows machines looks like for e.g

c:/program files/java/jdk/bin;d:/myprojects // on windows machines.

 
Sneha Prasad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply....But my question is that why I didn't face any problem when compiling servlet version one class.It compiled when I was using colon.
 
Saket Kumar Sinha
Ranch Hand
Posts: 32
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sneha

In Servlet version 1 even when i am not using classes in the command it will work

See

C:\beerV1\src>javac -d ../classes com/example/model/BeerExpert.java

C:\beerV1\src>javac -d ../classes -classpath C:\servlet-api.jar; com/example/web/BeerSelect.java

here BeerSelect.java is servlet version1. It is because in Version 1 you are not importing BeerExpert .

However in Servlet version 2 you are importing BeerExpert class then you need to specify the path to find the class like this

C:\beerV1\src>javac -d ../classes -classpath C:\servlet-api.jar;classes; com/example/web/BeerSelect.java




 
Sneha Prasad
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okk.....Thanks a lot...
 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are welcome
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!