运行环境:eclipse+tomcat+postgres
声明:用纯JSP就可以一:login.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ include file="lianjie.jsp"%>
<%
request.setCharacterEncoding("utf-8");
String uname = request.getParameter("uname");
String pwd = request.getParameter("pwd");
if(uname!=null && !"".equals(uname)){
out.print("<script>alert('登陆成功')</script>");
String sql = "select * from table_test where name=? and password=? ";
try{
stmt = conn.prepareStatement(sql);
stmt.setString(1,uname);
stmt.setString(2,pwd);
rs = stmt.executeQuery();
if(rs.next()){
session.setAttribute("user",uname);
session.setAttribute("pwd",pwd);
response.sendRedirect("index.jsp");
}else{
out.print("<script>alert('用户名或密码有误,请重新输入!')</script>");
}
}catch(Exception ex){
out.print(ex.getMessage());
}
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TestLogin登陆页</title>
<style type="text/css">
<!--
.STYLE1 {font-size: 14px}
-->
</style>
<script type="text/javascript">
function button2() {
window.location.href=("zhuce.jsp");
}
</script>
</head>
<body>
<center>
<form name="form1" method="post" action="">
<table align="center" width="553" height="179" border="1" cellpadding="0" cellspacing="0" align="center" >
<tr>
<td width="47"> </td>
<td width="109"><span class="STYLE1">账号:</span></td>
<td width="389">
<label>
<input type="text" name="uname" id="textfield" />
</label>
</td>
</tr>
<tr>
<td> </td>
<td><span class="STYLE1">密码:</span></td>
<td>
<label>
<input type="text" name="pwd" id="textfield2" />
</label>
</td>
</tr>
<tr>
<td> </td>
<td>
<label>
<input type="submit" id="button" name="button" value="登陆" />
</label>
</td>
<td>
<div align="center">
<label>
<input type="button" > <a href="zhuce.jsp">注册</a></input>
</label>
</div>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
---------------------------------------------------
2:zhuce.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ include file="lianjie.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script Language="JavaScript">
onclick="form1()"
function form1(){
if(confirm('确认提交')){
window.open('update.jsp');
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>注册信息</title>
<style type="text/css">
<!--
.STYLE3 {font-size: 12px}
-->
</style>
</head><body>
<form id="form1" name="form1" method="post" action="index.jsp">
<table width="700" border="1" cellspacing="0" cellpadding="0" align="center">
<tr>
<td width="136"> </td>
<td width="558"><span class="STYLE3">请输入您的基本信息</span></td>
</tr>
<tr>
<td>姓名:</td>
<td>
<label>
<input type="text" name="textfield" id="textfield" />
</label>
</td>
</tr>
<tr>
<td>密码:</td>
<td>
<label>
<input type="text" name="textfield5" id="textfield5" />
</label>
</td>
</tr>
<tr>
<td>性别:</td>
<td>
11
</td>
</tr>
<tr>
<td>年龄:</td>
<td>
<label>
<input type="text" name="textfield3" id="textfield3" />
</label>
</td>
</tr>
<tr>
<td>地址:</td>
<td>
<label>
<input type="text" name="textfield4" id="textfield4" />
</label>
</td>
</tr>
<tr>
<td> </td>
<td>
<label>
<input type="button" value="注册" onclick="document.getElementById('form1').submit();" />
</label>
</td>
</tr>
</table>
</form>
</body>
</html>
----------------------------------------------
3:lianjie.jsp
<%@ page contentType="text/html; charset=utf-8" import="java.sql.*"%>
<%
Connection conn = null;
Statement smt = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
Class.forName("org.postgresql.Driver").newInstance();
}catch(Exception ex){
ex.printStackTrace();
System.out.print(ex.getMessage());
}
try{
conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/TestLogin","postgres","root");
smt = conn.createStatement();
}catch(Exception ec){
ec.printStackTrace();
}%>
----------------------------------------
4:index.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@include file="lianjie.jsp"%>
<%
if(session.getAttribute("user")==null){
response.sendRedirect("login.jsp");
}
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
<!--
.STYLE1 {font-size: 14px}
-->
</style>
</head><body>
<div align="center">
<h3>
<%=session.getAttribute("user")%>你好</h3>
</div>
<table width="700" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td colspan="8"><span class="STYLE1">用户信息:</span></td>
</tr>
<tr>
<td>ID</td>
<td>姓名</td>
<td>密码</td>
<td>年龄</td>
<td>性别</td>
<td>地址</td>
<td>删除</td>
<td>编辑</td>
</tr>
<tr>
<td>1</td>
<td><%=session.getAttribute("user")%></td>
<td><%=session.getAttribute("pwd")%></td>
<td><%=session.getAttribute("password")%></td>
<td><%=session.getAttribute("sex")%></td>
<td><%=session.getAttribute("address")%></td>
<td><a href="update.jsp?id=<%=session.getAttribute("id")%>">删除</a></td>
<td><a href="update.jsp?id=<%=session.getAttribute("id")%>">编辑</a></td>
</tr>
</table>
</body>
</html>
---------------------------------------------
5:update.jsp
<%@ page contentType="text/html; charset=utf-8" import="java.sql.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<%@ include file="lianjie.jsp"%>
<%
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String age = request.getParameter("age");
String sex = request.getParameter("sex");
String address = request.getParameter("address");
String password = request.getParameter("password");
String id = request.getParameter("id");
String sql = "update zy_users set name=?,password=?,sex=?,age=?,address=? where id=?"; try{
int a = Integer.parseInt(age);
int ids = Integer.parseInt(id);
stmt = conn.prepareStatement(sql);
stmt.setString(1,name);
stmt.setString(2,password);
stmt.setString(3,sex);
stmt.setInt(4,a);
stmt.setString(5,address);
stmt.setInt(6,ids);
int rows = stmt.executeUpdate();
if(rows>0){
response.sendRedirect("index.jsp");
}else{
out.print("<script>alert('注册失败')</script>");
}
}catch(Exception ex){
System.out.print(ex.getMessage());
}
%>
--------------------- 到此结束。
原本有个close.jsp,感觉没用,我给删除了
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" %><%
try{
if(rs!=null) rs.close();
if(smt!=null) smt.close();
if(conn!=null && !conn.isClosed()){
conn.close();
}
}catch(Exception ex){
System.out.println(ex.getMessage());
}
%>
声明:用纯JSP就可以一:login.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ include file="lianjie.jsp"%>
<%
request.setCharacterEncoding("utf-8");
String uname = request.getParameter("uname");
String pwd = request.getParameter("pwd");
if(uname!=null && !"".equals(uname)){
out.print("<script>alert('登陆成功')</script>");
String sql = "select * from table_test where name=? and password=? ";
try{
stmt = conn.prepareStatement(sql);
stmt.setString(1,uname);
stmt.setString(2,pwd);
rs = stmt.executeQuery();
if(rs.next()){
session.setAttribute("user",uname);
session.setAttribute("pwd",pwd);
response.sendRedirect("index.jsp");
}else{
out.print("<script>alert('用户名或密码有误,请重新输入!')</script>");
}
}catch(Exception ex){
out.print(ex.getMessage());
}
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TestLogin登陆页</title>
<style type="text/css">
<!--
.STYLE1 {font-size: 14px}
-->
</style>
<script type="text/javascript">
function button2() {
window.location.href=("zhuce.jsp");
}
</script>
</head>
<body>
<center>
<form name="form1" method="post" action="">
<table align="center" width="553" height="179" border="1" cellpadding="0" cellspacing="0" align="center" >
<tr>
<td width="47"> </td>
<td width="109"><span class="STYLE1">账号:</span></td>
<td width="389">
<label>
<input type="text" name="uname" id="textfield" />
</label>
</td>
</tr>
<tr>
<td> </td>
<td><span class="STYLE1">密码:</span></td>
<td>
<label>
<input type="text" name="pwd" id="textfield2" />
</label>
</td>
</tr>
<tr>
<td> </td>
<td>
<label>
<input type="submit" id="button" name="button" value="登陆" />
</label>
</td>
<td>
<div align="center">
<label>
<input type="button" > <a href="zhuce.jsp">注册</a></input>
</label>
</div>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
---------------------------------------------------
2:zhuce.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<%@ include file="lianjie.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script Language="JavaScript">
onclick="form1()"
function form1(){
if(confirm('确认提交')){
window.open('update.jsp');
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>注册信息</title>
<style type="text/css">
<!--
.STYLE3 {font-size: 12px}
-->
</style>
</head><body>
<form id="form1" name="form1" method="post" action="index.jsp">
<table width="700" border="1" cellspacing="0" cellpadding="0" align="center">
<tr>
<td width="136"> </td>
<td width="558"><span class="STYLE3">请输入您的基本信息</span></td>
</tr>
<tr>
<td>姓名:</td>
<td>
<label>
<input type="text" name="textfield" id="textfield" />
</label>
</td>
</tr>
<tr>
<td>密码:</td>
<td>
<label>
<input type="text" name="textfield5" id="textfield5" />
</label>
</td>
</tr>
<tr>
<td>性别:</td>
<td>
11
</td>
</tr>
<tr>
<td>年龄:</td>
<td>
<label>
<input type="text" name="textfield3" id="textfield3" />
</label>
</td>
</tr>
<tr>
<td>地址:</td>
<td>
<label>
<input type="text" name="textfield4" id="textfield4" />
</label>
</td>
</tr>
<tr>
<td> </td>
<td>
<label>
<input type="button" value="注册" onclick="document.getElementById('form1').submit();" />
</label>
</td>
</tr>
</table>
</form>
</body>
</html>
----------------------------------------------
3:lianjie.jsp
<%@ page contentType="text/html; charset=utf-8" import="java.sql.*"%>
<%
Connection conn = null;
Statement smt = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
Class.forName("org.postgresql.Driver").newInstance();
}catch(Exception ex){
ex.printStackTrace();
System.out.print(ex.getMessage());
}
try{
conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/TestLogin","postgres","root");
smt = conn.createStatement();
}catch(Exception ec){
ec.printStackTrace();
}%>
----------------------------------------
4:index.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@include file="lianjie.jsp"%>
<%
if(session.getAttribute("user")==null){
response.sendRedirect("login.jsp");
}
%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
<!--
.STYLE1 {font-size: 14px}
-->
</style>
</head><body>
<div align="center">
<h3>
<%=session.getAttribute("user")%>你好</h3>
</div>
<table width="700" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td colspan="8"><span class="STYLE1">用户信息:</span></td>
</tr>
<tr>
<td>ID</td>
<td>姓名</td>
<td>密码</td>
<td>年龄</td>
<td>性别</td>
<td>地址</td>
<td>删除</td>
<td>编辑</td>
</tr>
<tr>
<td>1</td>
<td><%=session.getAttribute("user")%></td>
<td><%=session.getAttribute("pwd")%></td>
<td><%=session.getAttribute("password")%></td>
<td><%=session.getAttribute("sex")%></td>
<td><%=session.getAttribute("address")%></td>
<td><a href="update.jsp?id=<%=session.getAttribute("id")%>">删除</a></td>
<td><a href="update.jsp?id=<%=session.getAttribute("id")%>">编辑</a></td>
</tr>
</table>
</body>
</html>
---------------------------------------------
5:update.jsp
<%@ page contentType="text/html; charset=utf-8" import="java.sql.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<%@ include file="lianjie.jsp"%>
<%
request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String age = request.getParameter("age");
String sex = request.getParameter("sex");
String address = request.getParameter("address");
String password = request.getParameter("password");
String id = request.getParameter("id");
String sql = "update zy_users set name=?,password=?,sex=?,age=?,address=? where id=?"; try{
int a = Integer.parseInt(age);
int ids = Integer.parseInt(id);
stmt = conn.prepareStatement(sql);
stmt.setString(1,name);
stmt.setString(2,password);
stmt.setString(3,sex);
stmt.setInt(4,a);
stmt.setString(5,address);
stmt.setInt(6,ids);
int rows = stmt.executeUpdate();
if(rows>0){
response.sendRedirect("index.jsp");
}else{
out.print("<script>alert('注册失败')</script>");
}
}catch(Exception ex){
System.out.print(ex.getMessage());
}
%>
--------------------- 到此结束。
原本有个close.jsp,感觉没用,我给删除了
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" %><%
try{
if(rs!=null) rs.close();
if(smt!=null) smt.close();
if(conn!=null && !conn.isClosed()){
conn.close();
}
}catch(Exception ex){
System.out.println(ex.getMessage());
}
%>
<input type="button" > <a href="zhuce.jsp">注册</a></input> 改为
<input type="button" onclick="location.href='zhuce.jsp'"></input>
<%@ include file="lianjie.jsp"%>
<%
request.setCharacterEncoding("utf-8");
String uname = request.getParameter("uname");
String pwd = request.getParameter("pwd");
if(uname!=null && !"".equals(uname)){
out.print("<script>alert('登陆成功')</script>");
String sql = "select * from table_test where name=? and password=? ";
try{
stmt = conn.prepareStatement(sql);
stmt.setString(1,uname);
stmt.setString(2,pwd);
rs = stmt.executeQuery();
if(rs.next()){
session.setAttribute("user",uname);
session.setAttribute("pwd",pwd);
response.sendRedirect("index.jsp");
}else{
out.print("<script>alert('用户名或密码有误,请重新输入!')</script>");
}
}catch(Exception ex){
out.print(ex.getMessage());
}
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TestLogin登陆页</title>
<style type="text/css">
<!--
.STYLE1 {font-size: 14px}
-->
</style>
<script type="text/javascript">
function button2() {
window.location.href=("zhuce.jsp");
}
</script>
</head>
<body>
<center>
<form name="form1" method="post" action="">
<table align="center" width="553" height="179" border="1" cellpadding="0" cellspacing="0" align="center" >
<tr>
<td width="47"> </td>
<td width="109"><span class="STYLE1">账号:</span></td>
<td width="389">
<label>
<input type="text" name="uname" id="textfield" />
</label>
</td>
</tr>
<tr>
<td> </td>
<td><span class="STYLE1">密码:</span></td>
<td>
<label>
<input type="text" name="pwd" id="textfield2" />
</label>
</td>
</tr>
<tr>
<td> </td>
<td>
<label>
<input type="submit" id="button" name="button" value="登陆" />
</label>
</td>
<td>
<div align="center">
<label>
<input type="button" value="注册" onclick="location.href='zhuce.jsp'"></input>
</label>
</div>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>