jsp之用户注册
reg.jsp(注册页面)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<html>
<head>
<title>注册</title>
</head>
<body>
<!-- 注册 -->
<form action="reg2.jsp" method="post">
<div>ID:</div><input type="test" name="id" id="id">
<div>Password:</div><input type="password" name="password" id="password">
<input type="submit" value="提交">
</form>
<a href="login.jsp">已有账号,前往登陆</a>
</body>
</html>
reg2.jsp(数据库插入)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>获取数据库</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>
<%
request.setCharacterEncoding("utf-8");
String ID=(String)request.getParameter("id");
String Pwd=(String)request.getParameter("password");
Connection conn;
try {
PreparedStatement stmt;
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8";
String user = "root";
String password = "123456";
String sql = "insert into login values (?,?)";
Class.forName(driver);
conn = DriverManager.getConnection(url, user, password);
stmt = (PreparedStatement) conn.prepareStatement(sql);
stmt.setString(1, ID);
stmt.setString(2, Pwd);
int count=stmt.executeUpdate();
if(count>0){
%>
<!-- 跳转到注册成功的页面 -->
<jsp:forward page="reg3.jsp"></jsp:forward>
<%
}else{
%>
<!-- 抛到主页面 -->
<jsp:forward page="frame.jsp" />
<%
}
conn.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
%>
<!-- 用户名重复 抛出页面 -->
<jsp:forward page="error.jsp" />
<%
}
finally{
}
%>
</body>
</html>
reg3.jsp (登陆成功页面)
<%@ page language="java" import="java.util.*" pageEncoding="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>注册成功 提示信息</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>
注册成功 <br>
<a href="login.jsp">前往登陆</a>
</body>
</html>
error.jsp(用户名重复 提示页面)
<%@ page language="java" import="java.util.*" pageEncoding="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>注册成功 提示信息</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>
注册成功 <br>
<a href="login.jsp">前往登陆</a>
</body>
</html>
数据库名 127.0.0.1
数据库用户名 root
数据库密码 123456
数据表名 test
表名 login
表内数据 id varchar(10)
password varchar(10)