1.Servlet获取表单数据
(1)servlet的dopost方法代码:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
//创建一个用户实体类对象
Users u = new Users();
String username,mypassword,gender,email,introduce,isAccept;
Date birthday;
String[] favorites;
//格式化时间格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try
{
username = request.getParameter("username");
mypassword = request.getParameter("mypassword");
gender = request.getParameter("gender");
email = request.getParameter("email");
introduce = request.getParameter("introduce");
birthday = sdf.parse(request.getParameter("birthday"));
if(request.getParameterValues("isAccpet")!=null)
{
isAccept = request.getParameter("isAccept");
}
else
{
isAccept = "false";
}
//用来获取多个复选按钮的值
favorites = request.getParameterValues("favorite");
//将获取的值赋给上面创建的用户实体类对象
u.setUsername(username);
u.setMypassword(mypassword);
u.setGender(gender);
u.setEmail(email);
u.setFavorites(favorites);
u.setIntroduce(introduce);
if(isAccept.equals("true"))
{
u.setFlag(true);
}
else
{
u.setFlag(false);
}
u.setBirthday(birthday);
//把注册成功的用户对象保存在session中,regUser主要是与后面userinfo中的scope、getProperty相对应。
request.getSession().setAttribute("regUser", u);
//跳转到注册成功页面
request.getRequestDispatcher("../userinfo.jsp").forward(request,response);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
(2)获取填写数据的页面userinfo
<h1>用户信息</h1>
<hr>
<center>
<jsp:useBean id="regUser" class="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="mypassword"/></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 f:favorites)
{
%>
<%=f%>
<%
}
%>
</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>
(3)用户填写界面
<body>
<h1>用户注册</h1>
<hr>
<form name="regForm" action="servlet/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="mypassword" ></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="isAccept" value="true">是否接受霸王条款
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="注册"/>
<input type="reset" value="取消"/>
</td>
</tr>
</table>
</form>
</body>
2.Servlet路径跳转:
(1)JSP页面的代码:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=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 'index.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>
<h1>Servlet路径跳转</h1>
<hr>
//index.jsp跳转到servlet
<!--使用相对路径访问HelloServlet -->
**<!-- /servlet/HelloServlet 第一个/表示服务器的根目录 -->**
<a href="servlet/HelloServlet">访问HelloServlet!</a><br>
**//String path = request.getContextPath();**
**<!-- 使用绝对路径 访问HelloServlet,可以使用path变量:path变量表示项目的根目录-->**
<a href="<%=path%>/servlet/HelloServlet">访问HelloServlet!</a><br>
<!--表单中action的URL地址写法,与超链接方式完全相同。 -->
<a href="servlet/TestServlet">访问TestServlet,跳转到Test.jsp</a>
</body>
</html>
网友评论