3 回答
TA贡献1784条经验 获得超8个赞
HttpServlet
@WebServlet
@WebServlet("/yourServletURL")public class YourServlet extends HttpServlet {}
<form action>
name
<input>
, <select>
, <textarea>
<button>
<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>
doPost()
name
(不 id
request.getParameter()
request.getParameterValues()
@Overrideprotected 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; // ...}
User user = new User(name, pass, roles);userDAO.save(user);
另见:
TA贡献1833条经验 获得超4个赞
HttpServlet
public void doPost(HttpServletRequest req, HttpServletResponse resp){}
public void doGet(HttpServletRequest req, HttpServletResponse resp){}
GET
POST
HttpServletRequest
String name = req.getParameter("name");
添加回答
举报