0

So basically I have retrieved data from a database, have the values I need stored in an array. But I am having trouble setting the values in an HTML input field. I have three fields Welsh name, English name, and gender. So I need the welsh input field in the form to hold the welsh word and so on. How would I do this?

The HTML code;

<html>
<head>
    <title>Academi Gymraeg</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <h1>Academi Gymraeg</h1>
    <h2>Modify Vocabulary</h2>
    <div class="login-form">
        <div class="login-elements">
            <br>
            <form action="instructorServlet" name="searchVocab" method="post">
                <input type="hidden" name="type" value="searchVocab" />
                Word:
                <input type="text" name="word" class="inputBox" required><br><br>
                Language:<br>
                <select name="language" class="dropDown">
                    <option value="English" selected>English</option>
                    <option value="Welsh">Welsh</option>
                </select><br><br>
                <button type="submit" class="button" name="search" value="search"
                        >Search</button><br><br>
            </form>
            <form action="instructorServlet" name="modifyVocab" method="get">   
                <input type="hidden" name="type" value="modifyVocab" />
                Welsh Name:
                <input type="text" name="welshName" class="inputBox" required><br><br>
                English Name:
                <input type="text" name="englishName" class="inputBox" required><br><br>
                Gender:<br>
                <select name="gender" class="dropDown">
                    <option value="Masculine" selected>Masculine</option>
                    <option value="Feminine">Feminine</option>
                </select><br><br>
                <button type="submit" class="subButton" name="modifyEntry" value="Modify Entry"
                        >Modify Entry</button>
                <button type="reset" class="subButton" name="modifyEntry" value="Modify Entry"
                        >Reset</button>
            </form><br>
            <form action="instructorServlet" name="backToMenu" method="post">
                <input type="hidden" name="type" value="backToMenu" />
                <button type="submit" class="button" name ="backToMenu" value="backToMenu"
                        >Back To Menu</button>
            </form>
        </div>
    </div>
</body>

The servlet code

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String form = request.getParameter("type");
 if ("searchVocab".equals(form)) {
        String searchFor = request.getParameter("search");

        if (searchFor != null) {
            String word = request.getParameter("word");
            String language = request.getParameter("language");
            String values[] = database.searchFor(word, language);
            response.getOutputStream().println("<script> window.location = \"modify-vocab.html\";</script>");
            response.getOutputStream().println("<script> document.getElementById(\"welshName\").value = " + values[0] + ";\n"
                    + "    document.getElementById(\"englishName\").value = " + values[1] + ";\n"
                    + "    document.getElementById(\"gender\").value = " + values[2] + "; </script>");
        }
    }

The form reloads, however, the values aren't put into the input fields they just load blank.

Cœur
  • 32,421
  • 21
  • 173
  • 232

1 Answers1

1

Your servlet should build the values you need and forward them to the JSP view.

@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
   // ... code to build your values

   // Set the values
   request.setAttribute("welshName", welshName);
   request.setAttribute("englishName", englishName);
   request.setAttribute("gender", gender);

   // Ask the view to take care of the values
   request.getRequestDispatcher("view.jsp").forward(request, response);
}

Your view.

 <%

 final String welshName = (String) request.getAttribute("welshName");
 final String englishName = (String) request.getAttribute("englishName");
 final String gender = (String) request.getAttribute("gender");

 %>

 Welsh Name: <input type="text" name="welshName" class="inputBox" value="<% out.print(welshName); %>"/>
 English Name: <input type="text" name="englishName" class="inputBox" value="<% out.print(englishName); %>"/>

 Gender:
 <select name="gender" class="dropDown">
     <option value="Masculine" <% out.print("masculine".equals(gender) ? "selected" : ""); %>>Masculine</option>
     <option value="Feminine" <% out.print("feminine".equals(gender) ? "selected" : ""); %>>Feminine</option>
 </select>
LppEdd
  • 16,731
  • 6
  • 53
  • 100