1

Consider me basically brand new to JSPs and what's possible. I'm working on a suite of 4 webapp dashboards, each standalone, with separate URLs and index.(jsp|html) pages. The backend of the dashboards, written in Java, can be built in a variety of flavors, and can be deployed to a variety of environments. Through some wizardry, there happens to be a .properties file in WEB-INF/classes that I can use to get system information like build flavor and deployment environment -- things the UI needs to know.

Current solution to get system information to UI

In shared UI Javascript, we're making a blocking, synchronous AJAX call on the main thread to a JSP, which reads the properties file, and returns the information in JSON to the caller (file below). I'm trying to eliminate this synchronous AJAX call on the main thread, and instead just have that information already available to the Javascript when it runs, without AJAX.

Question

How can I instead use JSPs and servlets to have this information already available on the page for Javascript to access, without having to make an AJAX call to get it? And how can I make that solution shared across all of my dashboards, ideally without having to duplicate JSP code across all of the index.jsp files?

For reference, the JSP we're currently using is below. Again, we make an AJAX call to this JSP, then store information from its JSON response in global state variables for access by the dashboard.

<%@page language="java" contentType="application/json; charset=UTF-8" pageEncoding="UTF-8" %>
<%@page import="java.io.InputStream" %>
<%@page import="java.util.*" %>
<%@page import="com.google.gson.Gson" %>

<%
    // Read properties file
    Properties p = new Properties();
    p.load(application.getResourceAsStream("/WEB-INF/classes/system.properties"));

    Map<String, String> m = new HashMap<~>();
    m.put("id", p.getProperty("system.id", "unknown"));
    m.put("type", p.getProperty("system.type", "unknown"));

    // Write JSON response
    out.write(new Gson().toJson(m));
    out.flush();
%>

Thanks very much! Go easy on me... I'm new.

Matt
  • 23,363
  • 39
  • 111
  • 152

1 Answers1

1

You can assign your java property value to java script variable and use that java-script variable ,

try below code in jsp file where you want to use your property value

in Jsp Java code always run first then javascript code as java code executes on server where javascript runs on browser

<%@page language="java" contentType="application/json; charset=UTF-8"    pageEncoding="UTF-8" %>
<%@page import="java.io.InputStream" %>
<%@page import="java.util.*" %>


// javascript block
<script language="javascript">
 <%
   // Read properties file
   Properties p = new Properties();
   p.load(application.getResourceAsStream("/WEB-INF/classes/system.properties"));
 %>
  var sysId="<%=p.getProperty("system.id", "unknown")%>";
  var sysType="<%=p.getProperty("system.type", "unknown")%>";
  // user javascript variable sysId, sysType where you want 
</script>
Keval
  • 1,857
  • 16
  • 26