用户输入界面
----------------------------------------------------------------------------------------
<%@ page language="java" import="java.util.*"
pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>用户注册页面</title></head><body> <!-- 等待用户输入值 进行提到到UserServlet实现类进行处理 --> <form id="fom" action="UserServlet" method="post"> <table> <tr> <td>用户名:</td> <td><input type="text" name="userName"></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password"></td> </tr> <tr> <td>业余爱好:</td> <td> <input type="checkbox" name="habit" value="Reading">看书 <input type="checkbox" name="habit" value="Game">玩游戏 <input type="checkbox" name="habit" value="Travrlling">旅游 <input type="checkbox" name="habit" value="Swimming">游泳 <input type="checkbox" name="habit" value="TV">看电视 </td> </tr> <tr> <td>业余爱好:</td> <td> <input type="submit" value="提交"> </td> </tr> </table> </form></body></html>
用户类
----------------------------------------------------------------------------------------------------------
package com.bdqn.entity;
import java.io.Serializable;
import java.util.List;
public class User implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private String password; private List<String>habits; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public List<String> getHabits() { return habits; } public void setHabits(List<String> habits) { this.habits = habits; }}
用户实现类
--------------------------------------------------------------------------------------------------
package com.bdqn.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.bdqn.entity.User;
public class UserServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException { //其你去获得用户名 String name=request.getParameter("userName"); //获得密码 String pwd=request.getParameter("password"); String[] habit=request.getParameterValues("habit"); //此处生成一个user对象,user为自定义实例类 User user= new User(); user.setName(name); //设置对象属性值 user.setPassword(pwd); user.setHabits(java.util.Arrays.asList(habit));//转化为List<string> request.setAttribute("userObj", user); //把对象储存到request里面 //得到用户输入的信息转发到regSuccess.jsp页面输出 request.getRequestDispatcher("/regSuccess.jsp").forward(request, resp); }@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); //设置请求代码格式 this.doGet(req, resp);//调用本类doget方法 }}
打印页面
---------------------------------------------------------------------------------------
<%@ page language="java" import="java.util.*"
pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>用el展示注册信息</title></head><body> 你的注册信息如下:<br/> <!-- EL表达式获取属性值并输出到浏览器 --> 用户名:${requestScope.userObj.name} <br/> 密码: ${requestScope.userObj.password} <br/> 爱好: ${requestScope.userObj.habits}</body></html>