aspose file tools*
The moose likes JSP and the fly likes Put scriplet info in class file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Put scriplet info in class file" Watch "Put scriplet info in class file" New topic
Author

Put scriplet info in class file

Dan Parsons
Ranch Hand

Joined: May 30, 2007
Posts: 70
I have this scriptlet working in a JSP and was wondering how I can put it in a class file and call it in my JSP.



Would this be close because I am not sure how to create the method?


Then call it like this in JSP?



I dont have JSTL or Struts on this Tomcat 4.1.27 container that I am working on.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You're missing the class declaration.



Also, this wouldn't be 'class' file.
It is a source file.
You need to compile it with javac (which will output a ".class" file.


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

Joined: May 30, 2007
Posts: 70
Thanks, I got it to work using a source file and calling it in the JSP.
Now I wanI to do the same with a for loop block in my JSP:

Another part of my JSP outputs 10 links and it works great but want to cut down on the scriptlet lines in my JSP.

Here is what my current JSP looks like where it outputs the 10 links:


Now my attempt to put it in a class outputs only 1 link instead of 10.

Source code for the Java class:


JSP scriptlet calling the static method:



Please advise.
[ August 30, 2007: Message edited by: Dan Parsons ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You can only return once from a Java method.

If you're interested in moving away from scriptlets (and I applaud you for doing so), this probably isn't the best approach.
Building up HTML markup in a Java class is as messy if not messier than scriptlets.

Instead, find a good tutorial on the Model View Controller (MVC) pattern.

In a nutshell:
Write your business logic in plain old Java classes (Model).
Write a servlet that handles web requests. (Controller)
Have the controller instanciate any model classes and call their methods.
Have the model classes return the data in the form of JavaBeans and bind them to request (or, if need be, session or context) scope.
Forward to a JSP (View) and use JSTL and EL for formatting the data.

We have an example application in our CodeBarn that shows this in a form that (I think) is simple enough to understand without any further explanation.
Look for SimpleMVC:
http://www.javaranch.com/codebarn/codebarn-servlets.jsp
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

There's also this article for a perspective on web app patterns.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tom Madson
Greenhorn

Joined: Aug 31, 2007
Posts: 2
I suggest you use a String and StringBuffer and return a String:


[ August 31, 2007: Message edited by: Tom Madson ]

[Moderator edit: removed discussion of approach taken by other posters. If you have issues to air, the JavaRanch discussion forum is the place to do so. Thanks.]
[ August 31, 2007: Message edited by: Bear Bibeault ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Dan Parsons:
.. I dont have JSTL or Struts on this Tomcat 4.1.27 container that I am working on.


Dan,
I didn't notice that you had said that until someone else pointed it out.
Any sign of an upgrade in the near future?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Put scriplet info in class file