*
The moose likes JSP and the fly likes using classes in jsp (not servlets ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "using classes in jsp (not servlets )" Watch "using classes in jsp (not servlets )" New topic
Author

using classes in jsp (not servlets )

Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
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

Joined: Jan 10, 2002
Posts: 60752
    
  65

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
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

Joined: Jan 10, 2002
Posts: 60752
    
  65

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

Joined: Apr 27, 2006
Posts: 597
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

Joined: Jan 10, 2002
Posts: 60752
    
  65

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

Joined: Mar 22, 2000
Posts: 12759
    
    5
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

Joined: Apr 27, 2006
Posts: 597
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

Joined: Dec 11, 2004
Posts: 13410

What do you mean "does not work"?
What happens?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Rusty Smythe
Ranch Hand

Joined: Aug 09, 2006
Posts: 93
Is the import failing or the method call?
Meir Yan
Ranch Hand

Joined: Apr 27, 2006
Posts: 597
it is working now thanks for your help folks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using classes in jsp (not servlets )
 
Similar Threads
Use of out.write() method
Using a JavaBean tags in a JSP page
unknown JSP translation error
Implicit variable "exception" is null
tagSupport and synchronization issues