Java Application Properties File

I need to write a standalone Java application that will have an embedded HTTP server. I need to call an HTML page deployed locally with the application. The HTML page should display the properties listed in the * .properties file deployed with the application. I also have to change the value of the properties on the HTML page. Is there any way to do this?

Am i clear

+3
source share
4 answers

Yes. Use the built-in Jetty .

+4
source

, , , , . , , , .

: java.util.Properties( ), InputStream ( FileInputStream, , ClassLoader.getResourceAsStream, JAR). , Properties.load() .

, . , , - , JSP, HTML.

, , Jetty .

+3

1) , doGet() Properties#load(), HttpServletRequest#setAttribute(), JSP RequestDispatcher#forward(). , web.xml URL-, /propertieseditor.

Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("file.properties"));
request.setAttribute("properties", properties);
request.getRequestDispatcher("propertieseditor.jsp").forward(request, response);

2) JSP , JSTL c:forEach - , HTML input type="text".

<form action="propertieseditor" method="post">
    <c:forEach items="${properties}" var="property">
        ${property.key} <input type="text" name="${property.key}" value="${property.value}"><br>
    </c:forEach>
    <input type="submit">
</form>

3) doPost() , 1), , - .

Properties properties = new Properties();
Map<String, Object> parameterMap = request.getParameterMap();
for (Entry<String, Object> entry : parameterMap.entrySet()) {
    properties.setProperty(entry.getKey(), entry.getValue());
}
properties.store(new FileOutputStream(new File(
    Thread.currentThread().getContextClassLoader().getResource("file.properties").toURI())));
response.sendRedirect("propertieseditor.jsp");

, thisitor http://localhost/webapp/propertyeditor. .

+2

Source: https://habr.com/ru/post/1721868/


All Articles