Servlet完成运行后,Java Servlet将内容放入HTML的输入字段

时间:2017-03-25 18:53:03

标签: java html servlets

所以基本上我从数据库中检索数据,将我需要的值存储在数组中。但我在HTML输入字段中设置值时遇到问题。我有三个字段威尔士名字,英文名字和性别。所以我需要表单中的威尔士输入字段来保存威尔士语等等。我该怎么做?

HTML代码;

<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>

servlet代码

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>");
        }
    }

表单会重新加载,但是这些值不会输入到只是加载空白的输入字段中。

1 个答案:

答案 0 :(得分:1)

您的servlet应该构建您需要的值并将它们转发到JSP视图。

@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);
}

您的观点。

 <%

 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>