Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Questions about Import

 
Mike McMahon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey There,
I'm trying to use my first ever extended class, but I don't know if I'm importing it correctly. The error I'm getting is "Unable to compile class for JSP". I'm quite the noob, hence my posting in this forum. Thanks a lot!

Cheers, Mike

My class "newPolygon" is as follows:


And my jsp is:


and my class is located at examples\WEB-INF\classes\GoogleDir\newPolygon.class
and my jsp lives at
examples\
 
Abhinav Srivastava
Ranch Hand
Posts: 354
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot use AWT in JSPs. You should use Applets if at all you want to use Java for drawing the front-end.
 
Mike McMahon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Abhinav,

Thanks for the note - and I'm hardly the expert, but I don't think you're totally correct. I have sucessfully imported and called methods from the Polygon class from a JSP - so I'm sure that the AWT package can be used.

I think the problem is that I'm now extending that polygon class.

Cheers,
Mike
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is in this line in your JSP:

<%@ page import="classes.GoogleDir.*"%>

What you write after "import" is not the name of a directory, but of a Java package under WEB-INF/classes. To make this work, you should change this to:

<%@ page import="GoogleDir.*"%>

And you should put your newPolygon class in a package named GoogleDir by adding the following line at the top of the Java source file:

package GoogleDir;

(Note that "GoogleDir" is a strange name for a package; normally you would use a "reversed URL" form with lower-case letters, for example "com.mycompany.mypackage").

See: The Java Tutorial - Creating and Using Packages
 
Abhinav Srivastava
Ranch Hand
Posts: 354
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your are right, awt can be used in servlets/jsps.

any calls to the AWT toolkit in versions of Java 2 SDK before 1.4 require that the machine running the code have a valid display context, namely X Windows. In the Java 1.4 Platform, Sun made our lives much easier and addressed the lack of headless AWT support. Instead of setting up a dummy X server like Xvfb, just put the parameter "-Djava.awt.headless=true"
 
Mike McMahon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys,
I got it working.
The polygon class is very close to what I need, but the addPoint(int x, int y) method needs to accept doubles. I tried to address this by overriding the method "public void addPoint(Double x, Double y){}".

The class compiles just fine and I can access it's methods. As you can see in the jsp, I can use addPoints with doubles just fine, and polys are created. However, when I try to test the contains(method), it only seems to work when addPoints is loaded with integers - if the polygon uses doubles, contains() always returns false.

Any thoughts why?

Thanks guys,
Mike

Java class

jsp code:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic