Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Put scriplet info in class file

 
Dan Parsons
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dan Parsons
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64838
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's also this article for a perspective on web app patterns.
 
Tom Madson
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic