Hello Every one,
please tell me how to use dotproperty file in jsp?
like i m trying to code a login page and i want if any user input wrong or null value so on the same page msg should come like please enter correct username or something like that.....


please help me.....

Dani AI

Generated

A concise, practical approach that complements ’s client-side suggestion and ’s localization direction: validate on the client for UX, but always perform authoritative checks on the server. On a failed login, the server should put a (possibly localized) message into the request and forward back to the same JSP so the message appears on the same page. That keeps the form values and lets the JSP render a single inline error area without a redirect.

Example server-side pattern (Servlet/controller):

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    String user = req.getParameter("username");
    String pass = req.getParameter("password");

    if (user == null || user.trim().isEmpty() || pass == null || pass.trim().isEmpty()) {
        req.setAttribute("error", "Username and password are required.");
        req.getRequestDispatcher("/login.jsp").forward(req, resp);
        return;
    }

    boolean ok = authenticate(user, pass); // implement securely
    if (!ok) {
        ResourceBundle bundle = ResourceBundle.getBundle("messages", req.getLocale());
        req.setAttribute("error", bundle.getString("login.invalid"));
        req.getRequestDispatcher("/login.jsp").forward(req, resp);
        return;
    }

    resp.sendRedirect(req.getContextPath() + "/home");
}

Minimal JSP fragment to display the message safely:

<c:if test="${not empty error}">
  <div class="error"><c:out value="${error}" /></div>
</c:if>
<!-- then the login form -->

Troubleshooting and cautions: keep messages files on the webapp classpath (for example, under WEB-INF/classes or inside a JAR), and use consistent base names (e.g., messages.properties + locale variants). ResourceBundle historically expects ISO-8859-1; to store UTF-8 text either convert with native2ascii or use a UTF-8-aware loader (ResourceBundle.Control). Use forward (not redirect) to preserve request attributes, always escape output to avoid XSS (see c:out above), and never log raw passwords. If a framework is in use (Spring, Struts), prefer its built-in validation/message APIs. This pattern provides clear UX, proper localization, and secure server-side validation.

Recommended Answers

All 5 Replies

And why do you need properties file for this. Why don't you try javascript?

Put .properties files into WEB-INF/classes folder. Use JSTL to read properties values.

Please tell me site from where i can get example code so ithat i can get it easily...
please help me.

localization.jsp - put this file in public folder (anywhere but outside the WEB-INF).

<%@ page language="java" pageEncoding="ISO-8859-1" %>
<%@ taglib uri="/WEB-INF/c-rt.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/x-rt.tld" prefix="x" %>
<%@ taglib uri="/WEB-INF/fmt-rt.tld" prefix="fmt" %>
<html>
<body>
<form>
  Language <select name="cnt">
               <option value="hi_IN">Hindi</option>
               <option value="en_GB">English</option>
           </select>
           <input type="submit" value="Select">
</form>
<fmt:setLocale value="${param.cnt}"/>
    <fmt:setBundle basename="labels"/>
    <h2>Survey</h2>
    <form action="">
    <table>
      <tr>
        <td><fmt:message key="name"/></td>
        <td><input type="text" size="16"></td>
      </tr>
      <tr>
        <td><fmt:message key="age"/></td>
        <td><input type="text" size="16"></td>
      </tr>
      <tr>
        <td><fmt:message key="loc"/></td>
        <td><input type="text" size="16"></td>
      </tr>
      <tr>
        <td><input type="submit" value='<fmt:message key="submit"/>'></td>
      </tr>
    </table>
    </form>
  </body>
</html>

Create .properties file into WEB-INF/classes folder. Name of property file is user-defined. In this example, I have used "labels".
See this code,
<fmt:setBundle basename="labels"/>

for english culture a property file name should be,
labels_en.properties
for other cultures change the suffix en and create a new property file,
for example, hindi (indian) culture,
labels_hi.properties

Content of labels_en.properties

name=What is your name?
age=How old are you?
loc=Where do you live?
submit=Send

Thanks for reply!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.