将页面的数据封装到表单中
register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘register.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="/app/servlet/requestDemo3" method="post"> 姓名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> 确认密码:<input type="password" name="password"><br> 性别:<input type="radio" name="gander" value="男" >男 <input type="radio" name="gander" value="女">女<br> 爱好:<input type="checkbox" name="loves" value="打篮球">打篮球 <input type="checkbox" name="loves" value="踢足球">踢足球 <input type="checkbox" name="loves" value="抱孩子">抱孩子<br> 国籍:<select name="country"> <option value="中国">中国</option> <option value="美国">美国</option> <option value="加拿大">加拿大</option> <option value="日本">日本</option> <option value="韩国">韩国</option> <option value="俄罗斯">俄罗斯</option><br> </select> 备注:<textarea rows="5" cols="20" name="description">大家一起去请两天假</textarea><br> <input type="submit" value="提交"><br> </form> </body> </html>
User.java
package bean; import java.io.Serializable; import java.util.Arrays; /** * javabean * @author kj * */ public class User implements Serializable{ private String username; private String[] password; private String gander; private String[] loves; private String country; private String description; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String[] getPassword() { return password; } public void setPassword(String[] password) { this.password = password; } public String getGander() { return gander; } public void setGander(String gander) { this.gander = gander; } public String[] getLoves() { return loves; } public void setLoves(String[] loves) { this.loves = loves; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public String toString() { return "User [username=" + username + ", password=" + Arrays.toString(password) + ", gander=" + gander + ", loves=" + Arrays.toString(loves) + ", country=" + country + ", description=" + description + "]"; } }
requestDeom3.java (Servlet类)
public class requestDemo3 extends HttpServlet { /** * 演示封装表单中的数据 * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); User user = new User(); //将表单数据封装到user对象中 try { BeanUtils.populate(user, request.getParameterMap()); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("-----封装后-----" + user); }
时间: 2024-11-07 11:36:22