getParameter方法获取表单信息
request中的getParameter可以获取客户端发来的客户请求的参数信息。
jsp的内置对象request封装了由客户端生成的http请求的所有细节,其中包括http的头信息、系统信息、请求方式和请求参数。
(提示:当出现乱码问题时,试试加上request.setCharacterEncoding(“UTF-8”);或者其他的编码方式。)
效果图:
首界面
提交、跳转:
index.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>
</head>
<body>
<form action="NewFile.jsp" method="post">
<p align="center">用户注册</p>
<table align="center">
<tr>
<td>用户名:</td><td><input type="text" name="name"></td>
</tr>
<tr>
<td>密码:</td><td><input type="text" name="password"></td>
</tr>
<tr>
<td>确认密码:</td><td><input type="text" name="repassword"></td>
</tr>
<tr>
<td>性别:</td><td>男<input type="radio" name="sex" value="男">女<input type="radio" name="sex" value="女"></td>
</tr>
<tr>
<td>年龄:</td><td><input type="text" name="age"></td>
</tr>
<tr>
<td title="Email">Email:</td><td><input type="text" name="email"></td>
</tr>
</table>
<p align="center"><input type="submit" align="middle" value="提交" title="提交"/></p>
</form>
</body>
</html>
NewFile.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>Insert title here</title>
</head>
<%
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("name");
String sex=request.getParameter("sex");
String age=request.getParameter("age");
String email=request.getParameter("email");%>
<body>
<p align="center">注册用户的信息</p>
<table align="center">
<tr>
<td>用户名:</td><td><input type="text" value="<%= name %>"></td>
</tr>
<tr>
<tr>
<td>性别:</td><td><input type="text" value="<%= sex %>"></td>
</tr>
<tr>
<td>年龄:</td><td><input type="text" value="<%= age %>"></td>
</tr>
<tr>
<td title="Email">Email:</td><td><input type="text" value="<%= email %>"></td>
</tr>
</table>
</body>
</html>