0

I have a requirement where I need to populate the <p> tag with value from a file(to allow configurable option) based on receiving query parameter.File will be selected based on a query param receiving. I have tried two approaches and both are working fine. But I want to know which method has better performance.

Method 1 - Read from properties file

<%@page import="java.io.IOException" %>
<%@page import="java.util.Properties" %>
<%
     String userDir = request.getParameter("user");
    InputStream in = null;
    String propVal = null
     try{
        in = application.getResourceAsStream("/userDir/custom.properties");
        Properties props = new Properties();
        props.load(in);
        propVal = props.getProperty("prop1");
    } catch(IOException io) {
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {

            }
        }
    }

%>

<p><%=propVal%></p>

Method 2 - using <jsp:include where test.txt has text value

<%
String filePath = "userDir/test.txt";
%>

<p><jsp:include page="<%=filePath%>" /></p>
Jenananthan
  • 1,381
  • 2
  • 10
  • 20
  • Could you please tell us: How often should the properties be loaded? Will the value change at runtime? When the value does not change, then it would be better to load the properties only once. So both example are not optimal. Better read out the Properties only once. – Tobias Otto Sep 27 '16 at 13:16
  • 2
    don't use scriptlets at all, you have to load properties inside the view. [link](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files) – WildDev Sep 27 '16 at 13:23
  • @TobiasOtto I have updated my question. Actually file (properties file /text file) will be selected based on a query parameter received. For different users this query parameter will be different. So I think file reading will be dynamic. What I want to know is, in hte above two approaches which is better. if there is any other better solution please let me know.appreciate your inputs. – Jenananthan Sep 28 '16 at 03:35

0 Answers0