register.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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 'register.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>
<center>
<form action="jsp/regist_check.jsp" method="post">
<table border="1" width="600" height="200" background="red">
<tr>
<td colspan="2" align="center"><font class="red">用户注册:</font></td>
</tr>
<tr>
<td>用户ID:</td>
<td><input name="id" type="text"/></td>
</tr>
<tr>
<td>用户姓名:</td>
<td><input name="name" type="text"/></td>
</tr>
<tr>
<td>用户密码:</td>
<td><input name="password" type="password"/></td>
</tr>
<tr>
<td>
<input name="提交" value="提交" type="submit">
</td>
<td>
<input value="重置" type="submit">
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
regist_check.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="GB18030"%>
<%
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 'regist_check.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>
<center>
<h1><font color="red">注册操作</font></h1>
<%!
public static final String DBDRIVER="com.mysql.jdbc.Driver";
public static final String DBURL=
"jdbc:mysql://localhost:3306/songwenhong";
public static final String DBUSER="root";
public static final String DBPASS="123456";
%>
<%
Connection conn=null;
PreparedStatement ps=null;
boolean flag=false;
%>
<%try{
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
String sql="insert into user(userid,name,password) values(?,?,?)";
ps=conn.prepareStatement(sql);
ps.setString(1,request.getParameter("id"));
ps.setString(2,request.getParameter("name"));
ps.setString(3,request.getParameter("password"));
ps.executeUpdate();
flag=true;
}catch(Exception e){
e.printStackTrace();
}finally{
try{
ps.close();
conn.close();
}catch(Exception e){
e.printStackTrace();
}
}
%>
<%if(flag){ %>
<jsp:forward page="login.jsp"></jsp:forward>
<%
}else{
%>
<jsp:forward page="login_fail.jsp"></jsp:forward>
<%
}
%>
</center>
</body>
</html>
login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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 'login.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>
<form action="jsp/login_check.jsp" method="post">
<table border="0">
<tr>
<td colspan="2">用户登录</td>
</tr>
<tr>
<td>登录ID:</td>
<td>
<input type="text" name="id">
</td>
</tr>
<tr>
<td>密码:</td>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td><input type="submit" value="提交"/></td>
<td>
<input type="reset" name="ID" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>
login_check.jsp
<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="GB18030"%>
<%
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 'login_check.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>
<center>
<h1>登录操作</h1>
<%!
public static final String DBDRIVER="com.mysql.jdbc.Driver";
public static final String DBURL=
"jdbc:mysql://localhost:3306/songwenhong";
public static final String DBUSER="root";
public static final String DBPASS="123456";
%>
<%
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
boolean flag=false;
String name=null;
%>
<%
try{
Class.forName(DBDRIVER);
conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
String sql="select name from user where userid =? and password=?";
ps=conn.prepareStatement(sql);
ps.setString(1,request.getParameter("id"));
ps.setString(2,request.getParameter("password"));
rs=ps.executeQuery();
if(rs.next()){
flag=true;
name=rs.getString(1);
}
}catch(Exception e){
e.printStackTrace();
}finally{
try{
rs.close();
ps.close();
conn.close();
}catch(Exception e){
}
}
%>
<%
if(flag){
%>
<jsp:forward page="login_success.jsp">
<jsp:param value="<%=name %>" name="uname"/>
</jsp:forward>
<%
}else{
%>
<jsp:forward page="login_fail.jsp"></jsp:forward>
<%
}
%>
</center>
</body>
<html>