想要在jsp页面中动态填充文本框 [英] want to fill a textbox dynamically in jsp page
问题描述
想要使用jsp页面创建应用程序,以便它在文本框中从用户那里获取一个值,并基于该值从数据库中检索其他值,并使用检索到的值填充其他文本框
want to create a application using jsp pages such that it takes a value from user in a textbox and on the basis of that value it retrives other values from database and fill other textboxes with that retrived values
推荐答案
-
创建一个HTML表单并将其放入
search.jsp
:
<form action="search" method="post">
<input type="text" name="query">
<input type="submit">
</form>
创建一个Java类,该类extends HttpServlet
并执行所需的业务任务:
Create a Java class which extends HttpServlet
and does the desired business task:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String query = request.getParameter("query");
Data data = someDAOClass.find(query);
request.setAttribute("data", data); // Will be available in EL by ${data}
request.getRequestDispatcher("search.jsp").forward(request, response); // Return back to JSP page.
}
将此servlet映射到web.xml
中/search
的url-pattern
中,以便将其执行,然后提交JSP中的表单.
Map this servlet on an url-pattern
of /search
in web.xml
so that it will be executed then the form in the JSP is submitted.
用输入字段扩展表单,该输入字段应显示此数据.只需在输入元素的value
属性中填写所需的信息即可.
Extend the form with the input fields which should display this data. Just fill the value
attribute of the input elements with the desired information.
<form action="somethingelse" method="post">
<input type="text" name="id" value="${fn:escapeXml(data.id)}" />
<input type="text" name="name" value="${fn:escapeXml(data.name)}" />
<input type="text" name="email" value="${fn:escapeXml(data.email)}" />
<input type="text" name="age" value="${fn:escapeXml(data.age)}" />
...
</form>
fn:escapeXml
可以防止XSS.
这篇关于想要在jsp页面中动态填充文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!