• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Eliminate Scriplet conditions

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Servlet that checks for information and if there is an issue it forwards the message to presentation page (JSP). Now I want to stop using conditions in scriptlets in the JSP. Please advise how I can do it in this situation in my Tomcat 4.1.27 container:

Servlet that forwards to JSP:


JSP

I was thinking maybe a bean or regular Java class to handle this but not sure how. Here would be my method in a Java class:


Then I would put the method in a bean or what in JSP?
The Servlet would stay the same?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mike,
Why not put the actual message to display in the request rather than a code? In addition to eliminating the scriptlet condition, it would remove duplication. Right now you need to edit the servlet and JSP if an error code is added.

I think this is preferable to strictly translating the JSP to use EL or logic tags verbatim.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeanne is correct. Think about your original plan for a moment. You define magic codes in a servlet, that pass these codes to a JSP, that passes this code to a bean, that translates the code to a message, that gets passed to the JSP...

Doesn't this strike you as overly complex? The more moving parts a machine has, the easier it is to break down.

If you don't want to hard-code messages in Java code, you could continue to use a message code, but make this code a key into a properties file that contains the messages. The JSTL fmt tags makes translation of key to message a snap.
 
Mike Jenkins
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I dont understand what you mean by putting it in the request?

I dont have EL or JSTL in my Tomcat 4.1.27 due to restrictions but hoping to get it sometime next year.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before you write another line of JSP you need to learn how to create and manage scoped variables.

Start by looking at the getAttribute() and setAttribute() methods of the request.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic