Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Eliminate Scriplet conditions  RSS feed

 
Mike Jenkins
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • 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?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36463
464
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!