• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP Scriplets  RSS feed

 
Arjun Reddy
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I keep hearing that scriplets are bad way of coding.Is it true?If it is, then where do I write the code?

Thanks.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code that is typically crammed in a JSP page is either (a) presentation logic or (b) business logic.

In the case of presentation logic, this can be coded in a custom JSP tag class, or a Java servlet.

In the case of business logic, this can be coded in a plain old business object or a Session EJB, or a Web service.

Keep in mind that a Java Server Page is a Java servlet.
 
Peter Johnson
author
Bartender
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put your code in a servlet, pass beans that contain things you want on your page to a JSP, and use JSTL and the Expression Langauge (EL) in the JSP to display the data.

Quick example. Assume this code in the doGet/doPost:



Then index.jsp would look like:



You can, of course, set multiple attributes, and they can be more complicated than strings (example, a collection of objects/JavaBeans obtained from a database). The JSTL and EL provide mechanism to extract data from collections and any JavaBean. By doing this you can avoid server-side script within JSPs.
[ October 08, 2008: Message edited by: Peter Johnson ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!