一套自己写的JSP注册登录页面及后台数据库

本人刚学JAVA, 记下学习的点点滴滴, 里面涉及到JAVA的很多内容,初学者可以借鉴
不过要花时间仔细看!

第一个Zhuce.jsp

<%@ page language="java" contentType="text/html; charset=gb2312" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script language="javascript">
function check(form)
{
if(form.username.value==null || form.username.value==""){
alert('对不起!账号不能为空!');
return false;
}
else if(form.password.value==null || form.password.value==""){
alert('对不起!请输入密码!');
return false;
}
else if(form.cofpassword.value=="" || form.password.value==null){
alert('对不起!请再次输入密码!');
return false;
}
else if(form.password.value!=form.cofpassword.value){
alert('对不起!两次输入的密码不一样!');
return false;
}
else if(form.email.value=="" || form.email.value==null || form.email.value.indexOf("@")==-1 || form.email.value.indexOf(".")==-1){
alert("邮箱不能为空或格式不正确!");
return false;
}else{
return true;
}
}

</script>
<title>注册页面</title>
</head>
<body bgcolor="ffffcc">
<div align="center">
<h1>
<font face="黑体" color="#8080ff">注册页面</font>
</h1>
</div>
<form name="logform" method="post" action="Zhucecof.jsp"onSubmit="return check(this);">
<table width="100%" border="0">
<tbody>
<tr>
<td>
 
</td>
<td>
 标记
<font color="#ff0000">**</font>为必填项
</td>
</tr>
<tr>
<td align="right">
 
<font color="#ff0000">**</font>申请账号:
</td>
<td>
 
<input type="text" name="username">
</td>
</tr>
<tr>
<td align="right">
 
<font color="#ff0000">**</font>密码:
</td>
<td>
 
<input type="password" name="password">
</td>
</tr>
<tr>
<td align="right">
 
<font color="#ff0000">**</font>确认密码:
</td>
<td>
 
<input type="password" name="cofpassword">
</td>
</tr>
<tr>
<td align="right">
 
<font color="#ff0000">**</font>E-mail:
</td>
<td>
 
<input type="text" name="email">
</td>
</tr>
<tr>
<td align="right">
 个人主页:
</td>
<td>
 
<input type="text" name="homepage">
</td>
</tr>
<tr>
<td>
 
</td>
<td>
 
<input type="submit" value="提交" name="button1">
<input type="reset" value="重置" name="button2">
</td>
</tr>
</tbody>
</table>
<br>
</body>
</html>


第二个Zhucecof.jsp


<%@ page contentType="text/html;charset=gb2312" language="java"%>
<%@ page session="true"%>
<%@ page import="java.sql.*"%>
<% request.setCharacterEncoding("GB2312");%>
<html>
<head>
<title>登录验证</title>
</head>
<body bgcolor="#ccccff">
<%
String strSql="chatreg";
String database_username="root";
String database_password=null;
String url="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=gb2312";
try{
Class.forName( "org.gjt.mm.mysql.Driver" ).newInstance();
Connection conn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test", "root", null);

Statement stmt=conn.createStatement();
ResultSet rs=null;
String s_username=request.getParameter("username");
strSql="select * from chatreg where username like '"+s_username+"'";
rs=stmt.executeQuery(strSql);
if(rs.next()){
%>
<center><h2>对不起,你注册的账号已经存在</h2></centre><br><br>
<center><a href="Zhuce.jsp""Zhuce.jsp\">重新注册</a><br><br><a href="index.jsp">进入登录页面</a><br></center><br>
<%
}else{
String s_password=request.getParameter("password");
//System.out.println("s_password===>"+s_password);
String s_email=request.getParameter("email");
String s_homepage=request.getParameter("homepage");
strSql = "insert into chatreg (username,password,email,homepage) values('"+s_username+"','"+s_password+"','"+s_email+"','"+s_homepage+"')";
int temp = stmt.executeUpdate(strSql);
if(temp>0){
%>
<center><h2 color=red>恭喜你注册成功!</h2></center><br><br>
<center><a href="index.jsp""index.jsp\"> 进入登录页面 </a></center>
<%
}else {
%>
<center><h2 color=red>对不起,注册失败!</h2></center><br><br>
<center><a href="Zhuce.jsp""Zhuce.jsp\">返回</a></center>
<%
}
}
rs.close();
stmt.close();
conn.close();
}
catch(Exception e){
e.printStackTrace();
%>
<h2 align=center>数据库连接错误</h2>
<%
}
%>
</body>
</html>


第三个 index.jsp


<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
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>
<center>
<body bgcolor="ffffcc">
<h2><font face="黑体" color="#8080ff"> 您好!</font>${user.username}.</h2><h2><br></h2><h2><a href="Login.jsp" title="点击这里登录">登录</a>  <a href="Logout.jsp" title="点击这里退出系统">退出</a></h2><br>
</body>
</center>
</html>


第四个Login.jsp


<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
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">
<script type="text/javascript">
//验证输入不为空的脚本代码
function checkForm(form){
if(form.username.value==""){
alert("账号不能为空");
form.username.focus();
return false;
}
if(form.password.value==""){
alert("密码不能为空");
form.password.focus();
return false;
}
return ture;
}
</script>
</head>
<body bgcolor="ffffcc"><br><div align="center"><blockquote><div><h1><font face="黑体"><font color="#8080ff">   
    欢迎您登录本系统</font> </font></h1></div></blockquote>
<form name="regForm" method="post" action="Logincof.jsp" onSubmit="return checkForm(this)"> <p> </p>
<table width="66%" border="0" cellpadding="5" align="center">
<tr>
<td width="50%" height="26"> </td>
<td width="50%" height="26"><div align="left"><b>登陆界面</b></div>
</td>
</tr>
<tr>
<td width="50%" height="26"><div align="right"><b>账号:</b></div>
</td>
<td width="50%" height="26" align="left">
<input type="text" name="username"></td>
</tr>
<tr>
<td width="50%" height="26"><div align="right"><b>密码:</b></div>
<td width="50%" height="26">
<input type="password" name="password"></td>
</tr>
<tr>
<td width="50%" height="38"> </td>
<td width="50%" height=38">
<input type="submit" name="Submit" value="提交">
<input type="reset" name="Submit2" value="重置"> <br></td>
</tr>

</table>
</form>
</div>
</body>
</html>


第五个 Logincof.jsp


<%@ page contentType="text/html;charset=gb2312" %>
<%@ page session="true" %>
<%@ page import="java.sql.*"%>
<% request.setCharacterEncoding("GB2312");%>
<html>
<head>
<title>登录检查</title>
</head>
<body bgcolor="#ccccff">
<%
String regName=(String)request.getParameter("username");
regName=regName.trim();
String regPassword=(String)request.getParameter("password");
regPassword=regPassword.trim();
%>
<jsp:useBean id="reg" scope="page" class="log.Login"/>
<%
String sql="select*from chatreg where username='"+regName+"' and password='"+regPassword+"'";
ResultSet rs=reg.executeQuery(sql);
if(rs.next()){
out.println("<center><h2 color=red>您要查询的信息</h2></center>");
out.println("<center><h2 color=red>编号:"+rs.getInt("id")+"</h2></center>");
out.println("<center><h2 color=red>姓名:"+rs.getString("username")+"</h2></center>");
out.println("<center><h2 color=red>密码:"+rs.getString("password")+"</h2></center>");
out.println("<center><h2 color=red>邮箱:"+rs.getString("email")+"</h2></center>");
out.println("<center><h2 color=red>主页:"+rs.getString("homepage")+"</h2></center>");
}else{
out.println("<center><h2 color=red>对不起,登录失败!"+"</h2></center><br><br>");
out.println("<center><a href=\"Login.jsp\">返回</a></center>");
}
rs.close();
reg.close();
%>
<br>
<br>
<center><a href="Login.jsp">返回登录界面</a></center>
</body>
</html>



第六个Logout.jsp


<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
session.removeAttribute("user");

%>

<!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 bgcolor="ffffcc">
您已经成功退出系统.<br><br><a href="index.jsp">返回首页</a><br><br>
<a href="Login.jsp">重新登录</a><br>
<a href="Zhuce.jsp"><br></a>
<a href="Zhuce.jsp">返回注册页面</a><br>
</body>
</html>


Java 代码log.Login


package log;

import java.sql.*;

public class Login {
Connection conn = null;
ResultSet rs = null;
Statement stmt;

public Login() {
try {
Class.forName("org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection( "jdbc:mysql://127.0.0.1:3306/test", "root", null);
stmt = conn.createStatement();
} catch (java.lang.ClassNotFoundException e) {
System.err.println("chatreg():" + e.getMessage());
} catch (java.sql.SQLException e) {
System.err.println("chatreg():" + e.getMessage());
}
}

public ResultSet executeQuery(String sql) {
rs = null;
try {
if (conn == null) {
conn = DriverManager.getConnection( "jdbc:mysql://127.0.0.1:3306/test", "root", null);
stmt = conn.createStatement();
}
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
System.err.println(e.getMessage());
}
return rs;
}

public boolean executeUpdate(String sql) {
boolean success = false;
try {
if (conn == null) {
conn = DriverManager.getConnection( "jdbc:mysql://127.0.0.1:3306/test", "root", null);
stmt = conn.createStatement();
}
stmt.executeUpdate(sql);
success = true;
} catch (SQLException e) {
success = false;
}
return success;
}

public void close() {
try {
if (!conn.isClosed())
conn.close();
conn = null;
} catch (SQLException el) {
el.printStackTrace();
}
}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值