提交HTML表单时如何将数据从JSP传输到servlet [英] How to transfer data from JSP to servlet when submitting HTML form
问题描述
我有一个带有HTML表单的JSP页面:
I have a JSP page with an HTML form:
<form action="SERVLET">
<input type="text" name="name"/><br>
<input type="text" name="group"/>
<input type="text" name="pass"/>
<input type="submit" value="submit">
</form>
如何在servlet中获取这些数据并将其添加到数据库中?
How to obtain these data in a servlet and add them to database?
推荐答案
创建扩展 @WebServlet
批注,其中包含servlet应该侦听的所需URL.
Create a class which extends HttpServlet
and put @WebServlet
annotation on it containing the desired URL the servlet should listen on.
@WebServlet("/yourServletURL")
public class YourServlet extends HttpServlet {}
然后让<form action>
指向此URL.我也推荐对非幂等请求使用POST方法.您应该确保已指定HTML表单输入字段(<input>
,<select>
,<textarea>
和<button>
)的name
属性.这表示HTTP请求参数名称.最后,您还需要确保将感兴趣的输入字段包含在所需表单的内部,而不是外部.
And just let <form action>
point to this URL. I would also recommend to use POST method for non-idempotent requests. You should make sure that you have specified the name
attribute of the HTML form input fields (<input>
, <select>
, <textarea>
and <button>
). This represents the HTTP request parameter name. Finally, you also need to make sure that the input fields of interest are enclosed inside the desired form and thus not outside.
以下是各种 HTML表单输入字段的一些示例:
<form action="${pageContext.request.contextPath}/yourServletURL" method="post">
<p>Normal text field.
<input type="text" name="name" /></p>
<p>Secret text field.
<input type="password" name="pass" /></p>
<p>Single-selection radiobuttons.
<input type="radio" name="gender" value="M" /> Male
<input type="radio" name="gender" value="F" /> Female</p>
<p>Single-selection checkbox.
<input type="checkbox" name="agree" /> Agree?</p>
<p>Multi-selection checkboxes.
<input type="checkbox" name="role" value="USER" /> User
<input type="checkbox" name="role" value="ADMIN" /> Admin</p>
<p>Single-selection dropdown.
<select name="countryCode">
<option value="NL">Netherlands</option>
<option value="US">United States</option>
</select></p>
<p>Multi-selection listbox.
<select name="animalId" multiple="true" size="2">
<option value="1">Cat</option>
<option value="2">Dog</option>
</select></p>
<p>Text area.
<textarea name="message"></textarea></p>
<p>Submit button.
<input type="submit" name="submit" value="submit" /></p>
</form>
Create a doPost()
method in your servlet which grabs the submitted input values as request parameters keyed by the input field's name
(not id
!). You can use request.getParameter()
to get submitted value from single-value fields and request.getParameterValues()
to get submitted values from multi-value fields.
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String pass = request.getParameter("pass");
String gender = request.getParameter("gender");
boolean agree = request.getParameter("agree") != null;
String[] roles = request.getParameterValues("role");
String countryCode = request.getParameter("countryCode");
String[] animalIds = request.getParameterValues("animalId");
String message = request.getParameter("message");
boolean submitButtonPressed = request.getParameter("submit") != null;
// ...
}
如有必要,请进行一些验证,最后将其持久保存在数据库中,通常使用 JDBC / DAO 方式.
Do if necessary some validation and finally persist it in the DB the usual JDBC/DAO way.
User user = new User(name, pass, roles);
userDAO.save(user);
另请参见:
- HTML初学者教程
- 我们的Servlets Wiki页面
- 在Servlet中执行doGet和doPost
- 如何执行验证和在JSP中以相同的形式显示错误消息?
- 在将表单提交给Servlet之后,如何在JSP中保留HTML表单字段值?
- 如何使用JSP/Servlet将文件上传到服务器?
- 以HTML显示JDBC结果集使用MVC和DAO模式在JSP页面中
- Servlet返回"HTTP状态404请求的资源(/Servlet)不可用"
- HTML beginner tutorial
- Our Servlets wiki page
- doGet and doPost in Servlets
- How do I call a specific Java method on click/submit event of specific button in JSP?
- How perform validation and display error message in same form in JSP?
- How can I retain HTML form field values in JSP after submitting form to Servlet?
- How to upload files to server using JSP/Servlet?
- Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern
- Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
See also:
这篇关于提交HTML表单时如何将数据从JSP传输到servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!