标签:
Servlet与JSP九大内置对象对应关系:
Servlet如何获取表单数据呢?
我们就不再一一讲解,具体的可以参考:http://www.runoob.com/servlet/servlet-form-data.html
实例:获取表单数据
实体类Users代码:
package com.entity;
import java.util.Date;
/**
* 用户实体类
* @author Administrator
* @date 2016年7月25日
*/
public class Users {
private String username; //用户名
private String password; //密码
private String email; //电子邮箱
private String gender; //性别
private Date birthday; //出生日期
private String[] favorites; //爱好
private String introduce; //自我介绍
private boolean flag; //是否接受协议
public Users() {
}
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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String[] getFavorites() {
return favorites;
}
public void setFavorites(String[] favorites) {
this.favorites = favorites;
}
public String getIntroduce() {
return introduce;
}
public void setIntroduce(String introduce) {
this.introduce = introduce;
}
public boolean getFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
}
Servlet层的RegServlet代码:
package com.servlet;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.entity.Users;
/**
* Servlet implementation class RegServlet
*/
@WebServlet("/RegServlet")
public class RegServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RegServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see Servlet#init(ServletConfig)
*/
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
}
/**
* @see Servlet#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//防止中文乱码
request.setCharacterEncoding("UTF-8");
Users user = new Users();
String username;
String password;
String gender;
String email;
String introduce;
String flag;//是否接受协议
Date birthday;
String[] favorites;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try{
username = request.getParameter("username");
password = request.getParameter("password");
gender = request.getParameter("gender");
email = request.getParameter("email");
introduce = request.getParameter("introduce");
//需要注意类型转化
birthday = sdf.parse(request.getParameter("birthday"));
//这个也比较特殊,需要注意
if(request.getParameterValues("flag")!=null){
flag = request.getParameter("flag");
}else{
flag = "false";
}
//用来获取多个复选按钮的值
favorites = request.getParameterValues("favorite");
user.setUsername(username);
user.setPassword(password);
user.setGender(gender);
user.setEmail(email);
user.setFavorites(favorites);
user.setIntroduce(introduce);
//特殊
if(flag.equals("true")){
user.setFlag(true);
}else{
user.setFlag(false);
}
user.setBirthday(birthday);
//把注册成功的用户对象保存在session对象中
request.getSession().setAttribute("regUser", user);
//跳转到注册成功页面
request.getRequestDispatcher("userinfo.jsp").forward(request,response);
}catch(Exception e){
e.printStackTrace();
}
}
}
注册页面reg.jsp页面代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
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>
<style type="text/css">
.label {
width: 20%
}
.controler {
width: 80%
}
</style>
<script type="text/javascript" src="js/Calendar3.js"></script>
</head>
<body>
<h1>用户注册</h1>
<hr>
<form name="regForm" action="RegServlet" method="post">
<table border="0" width="800" cellspacing="0" cellpadding="0">
<tr>
<td class="lalel">用户名:</td>
<td class="controler">
<input type="text" name="username" />
</td>
</tr>
<tr>
<td class="label">密码:</td>
<td class="controler">
<input type="password" name="password" />
</td>
</tr>
<tr>
<td class="label">确认密码:</td>
<td class="controler">
<input type="password" name="confirmpass" />
</td>
</tr>
<tr>
<td class="label">电子邮箱:</td>
<td class="controler">
<input type="text" name="email" />
</td>
</tr>
<tr>
<td class="label">性别:</td>
<td class="controler">
<input type="radio" name="gender" checked="checked" value="Male" />男
<input type="radio" name="gender" value="Female" />女
</td>
</tr>
<tr>
<td class="label">出生日期:</td>
<td class="controler">
<input name="birthday" type="text" id="control_date" size="10" maxlength="10" onclick="new Calendar().show(this);" readonly="readonly" />
</td>
</tr>
<tr>
<td class="label">爱好:</td>
<td class="controler">
<input type="checkbox" name="favorite" value="nba" /> NBA
<input type="checkbox" name="favorite" value="music" /> 音乐
<input type="checkbox" name="favorite" value="movie" /> 电影
<input type="checkbox" name="favorite" value="internet" /> 上网
</td>
</tr>
<tr>
<td class="label">自我介绍:</td>
<td class="controler">
<textarea name="introduce" rows="10" cols="40"></textarea>
</td>
</tr>
<tr>
<td class="label">接受协议:</td>
<td class="controler">
<input type="checkbox" name="flag" value="true" />是否接受霸王条款
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="注册" />
<input type="reset" value="取消" />
</td>
</tr>
</table>
</form>
</body>
</html>
用户信息界面userinfo.jsp页面代码:
<%@page import="java.text.SimpleDateFormat"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
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>
<style type="text/css">
.title {
width: 30%;
background-color: #CCC;
font-weight: bold;
}
.content {
width: 70%;
background-color: #CBCFE5;
}
</style>
</head>
<body>
<h1>用户信息</h1>
<hr>
<center>
<jsp:useBean id="regUser" class="com.entity.Users" scope="session" />
<table width="600" cellpadding="0" cellspacing="0" border="1">
<tr>
<td class="title">用户名:</td>
<td class="content"> <jsp:getProperty name="regUser" property="username" />
</td>
</tr>
<tr>
<td class="title">密码:</td>
<td class="content"> <jsp:getProperty name="regUser" property="password" />
</td>
</tr>
<tr>
<td class="title">性别:</td>
<td class="content"> <jsp:getProperty name="regUser" property="gender" />
</td>
</tr>
<tr>
<td class="title">E-mail:</td>
<td class="content"> <jsp:getProperty name="regUser" property="email" />
</td>
</tr>
<tr>
<td class="title">出生日期:</td>
<td class="content">
<%
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
String date = sdf.format(regUser.getBirthday());
%>
<%=date%>
</td>
</tr>
<tr>
<td class="title">爱好:</td>
<td class="content">
<%
String[] favorites = regUser.getFavorites();
for(String fav:favorites){
%>
<%=fav%>
<%
}
%>
</td>
</tr>
<tr>
<td class="title">自我介绍:</td>
<td class="content"> <jsp:getProperty name="regUser" property="introduce" /></td>
</tr>
<tr>
<td class="title">是否介绍协议:</td>
<td class="content"> <jsp:getProperty name="regUser" property="flag" /></td>
</tr>
</table>
</center>
</body>
</html>
运行结果:
用户注册页面:
用户信息界面:
标签:
原文地址:http://blog.csdn.net/erlian1992/article/details/52089336