Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 585465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:00:21+00:00 2026-05-13T15:00:21+00:00

I’ve been told that the use of scriptlets (<%= … %>) in my JSP

  • 0

I’ve been told that the use of scriptlets (<%= … %>) in my JSP pages isn’t such a great idea.

Can someone with a bit more java/jsp experience please give me some pointers as to how to change this code so its more ‘best practice’, whatever that may be?

This JSP is actually my sitemesh main decorator page. Basically my web design has a tab strip and a submenu, and i wish to somehow highlight the current tab and show the correct submenu by looking at the current request URI.

<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %>

<html>
<head>
  <title>My Events - <decorator:title /></title>
  <link href="<%= request.getContextPath() %>/assets/styles.css" rel="stylesheet" type="text/css" />
</head>
<body>

<div class="tabs">
  <a 
    <%= request.getRequestURI().contains("/events/") ? "class='selected'" : "" %>
    href='<%= request.getContextPath() %>/events/Listing.action'>Events</a>
  <a 
    <%= request.getRequestURI().contains("/people/") ? "class='selected'" : "" %>
    href='<%= request.getContextPath() %>/people/Listing.action'>People</a>
</div>

<div class="submenu">
  <% if(request.getRequestURI().contains("/events/")) { %>
    <a href="Listing.action">List of Events</a>
    |<a href="New.action">New Event</a>
  <% } %>
  <% if(request.getRequestURI().contains("/people/")) { %>
    <a href="Listing.action">List of People</a>
    |<a href="New.action">New Person</a>
  <% } %>  
  &nbsp;
</div>

<div class="body">
  <decorator:body />
</div>

</body>
</html>

Thanks all

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T15:00:21+00:00Added an answer on May 13, 2026 at 3:00 pm

    I think it helps more if you see with your own eyes that it can actually be done entirely without scriptlets.

    Here’s a 1 on 1 rewrite with help of among others JSTL (just drop jstl-1.2.jar in /WEB-INF/lib) core and functions taglib:

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
    
    <html>
    <head>
      <title>My Events - <decorator:title /></title>
      <link href="${pageContext.request.contextPath}/assets/styles.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
    
    <div class="tabs">
      <a 
        ${fn:contains(pageContext.request.requestURI, '/events/') ? 'class="selected"' : ''}
        href="${pageContext.request.contextPath}/events/Listing.action">Events</a>
      <a 
        ${fn:contains(pageContext.request.requestURI, '/people/') ? 'class="selected"' : ''}
        href="${pageContext.request.contextPath}/people/Listing.action">People</a>
    </div>
    
    <div class="submenu">
      <c:if test="${fn:contains(pageContext.request.requestURI, '/events/')}">
        <a href="Listing.action">List of Events</a>
        |<a href="New.action">New Event</a>
      </c:if>
      <c:if test="${fn:contains(pageContext.request.requestURI, '/people/')}">
        <a href="Listing.action">List of People</a>
        |<a href="New.action">New Person</a>
      </c:if>
      &nbsp;
    </div>
    

    Here’s a more optimized rewrite, note that I used c:set to “cache” expression results for reuse and that I use HTML <base> tag to avoid putting the context path in every link (just make all relative URL’s in your webpage relative to it –without the leading slash!):

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
    
    <c:set var="isEvents" value="${fn:contains(pageContext.request.requestURI, '/events/')}" />
    <c:set var="isPeople" value="${fn:contains(pageContext.request.requestURI, '/people/')}" />
    
    <html>
    <head>
      <title>My Events - <decorator:title /></title>
      <base href="${pageContext.request.contextPath}">
      <link href="assets/styles.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
    
    <div class="tabs">
      <a ${isEvents ? 'class="selected"' : ''} href="events/Listing.action">Events</a>
      <a ${isPeople ? 'class="selected"' : ''} href="people/Listing.action">People</a>
    </div>
    
    <div class="submenu">
      <c:if test="${isEvents}">
        <a href="Listing.action">List of Events</a>|<a href="New.action">New Event</a>
      </c:if>
      <c:if test="${isPeople}">
        <a href="Listing.action">List of People</a>|<a href="New.action">New Person</a>
      </c:if>
      &nbsp;
    </div>
    

    It can actually be optimized more if you collect all those “hardcoded” values like events and people and link texts in a Map in the application scope and use under each the JSTL <c:forEach> to display the tabs.

    As to your actual question, you can disable scriptlets (and get runtime errors about using it) by adding the following entry in webapp’s web.xml. It may help to spot overseen scriptlets.

    <jsp-config>
        <jsp-property-group>
            <url-pattern>*.jsp</url-pattern>
            <scripting-invalid>true</scripting-invalid>
        </jsp-property-group>
    </jsp-config>
    

    To learn more about EL, check the Java EE tutorial part II chapter 5. Implicit EL objects, such as ${pageContext} are described here. To learn more about JSTL, check the Java EE tutorial part II chapter 7. Note that JSTL and EL are two separate things. JSTL is a standard taglib and EL just enables to access backend data programmatically. Although it is normally used in taglibs like JSTL, it can also be used standalone in template text.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 287k
  • Answers 287k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Ben Ryan has the regkeys you seek on his blog:… May 13, 2026 at 5:09 pm
  • Editorial Team
    Editorial Team added an answer Its not possible to programmatically create the content of a… May 13, 2026 at 5:09 pm
  • Editorial Team
    Editorial Team added an answer GWT is designed to reuse the same html page. This… May 13, 2026 at 5:09 pm

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.