• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using classes in jsp (not servlets )

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
i was writing some classes in jsp inside of <%! %>
but there is some disadvantages , i like to take this page with the classes and
make from them real compiled classes so i could use them in my jsp pages
the question is how ?
i tried to make simple class and put it inside of the classes directory under WEB-INF



in the jsp page when i try to call the method like this :
[code][
<body>

<%

printName.getName();

%>

</body>

/code]

im getting error that says :

An error occurred at line: 10 in the jsp file: /test.jsp
Generated servlet error:
printName cannot be resolved


what im doing wrong?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Meir Yan:
i was writing some classes in jsp inside of <%! %>
but there is some disadvantages


I'll say!

This construct is so fraught with problems and pitfalls that it's use within JSP pages has become strongly discouraged. It still exists in JSP for backward compatibility, but you should not be using it in new code.

Why would want to declare your classes in a JSP rather than compiling them as separate units and placing them in the WEB-INF/classes folder? You're just asking for trouble.
[ August 12, 2006: Message edited by: Bear Bibeault ]
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello and thanks for the reply
maybe i wasn't clear but this is what i want to do and its not working for me
i dont want to keep the class's inside the <%! %> i do want to make units of classes
but without success
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Meir Yan:
i do want to make units of classes
but without success


You would write and compile the classes just like any other Java class. When done, make sure that the class files, in the appopriate package hierachy, are placed in WEB-INF/classes.

Unless you tell use precisely what "without success" means, there's not much we can do to help.

If you do not know how to complie Java classes, please check in with the Java in General (beginner) forum.
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
where i can define in the configuration from where the web application will
find the classes?
is WEB-INF/classes is default directory?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, WEB-INF/classes is automatically aded to the classpath.

At this point I'd suggest that you grab a copy of the servlets specification and read through it. It will answer questions like this and give you a much better understanding of the structure of web applications.
[ August 13, 2006: Message edited by: Bear Bibeault ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Make sure your auxiliary class is in a package and the location under classes reflects that package.
2. Import it to your JSP correctly using the package name - see the spec.
Bill
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok got you there partlly i must say when for example i made this packege
my.test.print
then im my jsp page im accssing my class like this :
<%
my.test.print.printName.getName()
%>
it is working great But when i try to import the packge name and then to use the class.method it is not working im doing the import like this:

and using im my jsp code like this:
<%
printName.getName()
%>
this way it does not work , why?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean "does not work"?
What happens?
 
Rusty Smythe
Ranch Hand
Posts: 93
Mac Objective C Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the import failing or the method call?
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is working now thanks for your help folks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic