为了练手,我就自己试着做了一个网站的登录与注册的小案例。由于没有做美化处理,所以界面并不是很好看。
网站实现的功能如下:
•用户首次注册功能
•用户登录功能
下面我将会分模块展示
注册模块
首先需要一个注册界面,如下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>user to register page!</title>
</head>
<body>
<hr><br>welcome to this <font color="green">enroll(register) page</font>!<br>
<form action="do_register.jsp" method="get">
<br>
<h1>please input your message:</h1><br>
name:<input type="text" name="register_name"><br>
pswd:<input type="password" name="register_password"><br>
<br><br><br>
<input type="submit"> <input type="reset"><br>
</body>
</html>
然后就是action对应的注册处理页,如下do_register.jsp:
<%@ page language="java" contenttype="text/html; charset=utf-8"
pageencoding="utf-8"%>
<%@ page import="java.sql.*" %>
<!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>server to do the register page!</title>
</head>
<body>
<%
string register_name=request.getparameter("register_name");
string register_password=request.getparameter("register_password");
%>
<%
try{
class.forname("com.mysql.jdbc.driver");
connection conn=drivermanager.getconnection("jdbc:mysql://localhost:3306/summer", "root", "mysql");
statement stmt=conn.createstatement();
//desogn the sql statement
string insertsql="insert into user(name,password) values('"+register_name+"','"+register_password+"')";
system.out.println(register_name+"t"+register_password);
//do the query operation,and here is the most important sql statement.
int flag=stmt.executeupdate(insertsql);
if(flag>0){
response.getwriter().write("congratulation! register success!");
}else{
response.getwriter().write("sorry!register failed!nplease retry it!");
}
}catch(sqlexception e){
}
%>
</body>
</html>
小总结:
不足之处:
•对于数据库的操作做得不够好,没有及时的将不用的资源关闭,应该及时的对那些不用的打开的资源进行关闭操作,释放资源。
•界面效果做的不够好,response输出是先于out的输出的。
•数据库操作显得过于繁琐,应该集成一下,做一个专门处理数据库操作的工具包,以实现代码的良好的复用性!
登录模块
首先是登录界面,login.jsp,鄙人加进去一个超链接(用意是让login.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>user login page</title>
</head>
<body>
<hr><br>welcome to this <font color="green">login page</font>!<br>
<form action="do_login.jsp" method="get">
<br>
<h1>please input your message:</h1><br>
name:<input type="text" name="name"><br>
pswd:<input type="password" name="password"><br>
<br><br><br>
<input type="submit"> <input type="reset"><br>
click me to <font color="green"><a href="register.jsp">register</a>!</font><br>
</form>
</body>
</html>
然后是对登录信息的处理页,do_login.jsp:
<%@page import="java.sql.drivermanager"%>
<%@ page language="java" contenttype="text/html; charset=utf-8"
pageencoding="utf-8"%>
<%@ page import="java.sql.*" %>
<!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>server page depend !</title>
</head>
<body>
<h3>which pae will be depend by the user's message!</h3>
<%
string name=request.getparameter("name");
string password=request.getparameter("password");
%>
<%
class.forname("com.mysql.jdbc.driver");
connection conn=drivermanager.getconnection("jdbc:mysql://localhost:3306/summer", "root", "mysql");
statement stmt=conn.createstatement();
//desogn the sql statement
string querynumbersql="select name from user where name='"+name+"' and password='"+password+"'";
//do the query operation
resultset rs=stmt.executequery(querynumbersql);
boolean flag=false;
if(rs.next()){
flag=true;
session.setattribute("username", name);
}else{
flag=false;
}
%>
<%
if(flag){
%>
<jsp:forward page="login_success.jsp"></jsp:forward>
<%
}else{
%>
<jsp:forward page="login_failed.jsp"></jsp:forward>
<%
}
%>
</body>
</html>
对于登陆成功的用户,跳转到登陆成功界面login_success.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>user login success page!</title>
</head>
<body>
<hr><br>
<h1>login success!</h1><br>
<font color="green">welcome <%=session.getattribute("username") %>!</font>
<h3 align="center">your persional message is:</h3>
<%
out.println("name:"+session.getattribute("username"));
%>
<font color="red"><a href="login.jsp">click me</a> to log out!</font>
</body>
</html>
对于登录失败的用户,进行温馨的页面提示,login.failed.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>login failed page!</title>
</head>
<body>
<hr>
<br>
<h1><font color="red">sorry,login failed</font></h1><br>
<font color="red"><a href="login.jsp">click me</a> to login!</font>
</body>
</html>
大总结:
进步之处:
•使用到了session对象来存储用户登录的姓名信息,实现了页面间的信息的交互
•配合了mysql,在一定程度上体验了jee的模式
不足之处:
•代码过于繁冗,复用性不好
•资源利用率不高,使用过的不再使用的资源要及时的进行关闭。虽然java虚拟机有自动的垃圾回收机制,但最好还是养成好的习惯!
•界面控制做的不够好,体验性差,欠缺思考
待改进之处:
•加上复杂一点的用户注册,使用bean的方式做处理比较好
•模块化,使用mvc的概念
•改善界面的权限,防止盗链
•加上其他的诸如上传文件,下载文件功能,丰富网站的功能。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持硕编程。
【说明】:
本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!