0

I have a jsp page that looks like the following:

What the intent is, is to get the current environment via a system variable and then generate the correct url for that environment.

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %>

<%
String sysEnv = "";
if(System.getProperty("env.name").equals("test")) {
    sysEnv = "test";
} 
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html>
<head>
    <%@ include file="/includes/common.jsp" %>
    <script type="text/javascript" src="<c:out value="${requestScope.scriptsLocation}"/>/scripts/general.js"></script>

    <c:if test="${requestScope.isFeed == true}">
        <jsp:include page="/includes/someInclude.jsp"/>
        <script src="http://www.myothersite.com/feed/d/some.js" type="text/javascript"></script>

        <script type="text/javascript">

       var environ = <%=sysEnv%>;

        if(environ == 'test'){
                var theUrl = 'http://mywebsite.com?isFeed=true&env=TEST';
        } else {
        var theUrl = 'http://mywebsite.com?isFeed=true';
        }

       ...

    </script>

...

I think I'm supposed to have the tags-logic import to do the looping that I'm trying to achieve in Java, but this is not working.

1 Answer 1

2

Since you're already in a JSP why not just do (roughly) this:

<script>
  var url = 'http://mywebsite.com?isFeed=true';
  if ('<%= System.getProperty("env.name") %>' === 'test') {
    url += '&env=TEST';
  }
  // etc.

Personally I'd move this logic out of the view and create a request-scoped attribute in a filter, as part of your Struts 1 request processor, etc. since scriptlets are almost always bad. It also gives you the opportunity to set it at runtime.

In addition, the URL generation should also likely be moved out of the view layer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.