JSP 07: 开发注册页面
示例:
1. 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>Insert title here</title> </head> <body> <a>登录</a> <a href = "register.jsp">注册</a> </body> </html>
2. register.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> <body> <form action ="register_do.jsp" method ="post"> <!-- register.jsp 用来展示注册页面, register_do.jsp 页面用来处理注册请求 --> 用户名: <input type="text" name="username"/><br/> 密码: <input type="password" name="password"/><br/> 年龄: <input type="text" name="age"/><br/> 性别: 男<input type="radio" name="gender" value="死男人"/> 女<input type="radio" name="gender" value="臭女人"/><br/> <input type = "submit" value="我要注册成为新用户!"/> </form> </body> </html>
效果:
3. 创建register_do.jsp 来处理注册请求
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
int age = Integer.parseInt(request.getParameter("age")); // request.getParameter("age") 返回的是string类型, 需要转化为int类型
String gender = request.getParameter("gender");
%>
4. 在web02/Java_Resources/src 文档下, 创建操作数据的类
创建存放账号数据的User类
5.
6. 效果
注册第一个用户
使用相同的名字, 进行第二次注册