您的位置:首页 > Web前端 > JavaScript

我的第一个jsp程序-实现注册登录留言功能

2015-11-30 00:00 316 查看
1,注册功能,包括两个页面

zhuce.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>注册页面</title>

<script type="text/javascript">
//js方法,验证用户名,昵称,密码是否为空,两次密码输入是否一致
function yanzh()
{
if(form1.user.value == "")
{
alert("用户名不能为空");
return false
}
else if(form1.passw.value == "")
{
alert("密码不能为空");
return false
}
else if(form1.nich.value == "")
{
alert("昵称不能为空");
return false
}
else if(form1.passw.value != form1.passw1.value)
{
alert("两次输入密码不一致");
return false
}
else
{
return true
}

}

</script>

</head>
<body>
<!-- 注册表单 -->
<form action="zhcchl.jsp" name="form1" id="form1" onsubmit="return yanzh()" method="post">

请注册
<br>
用户名:<input type="text" name="user" id="user">
<br>
昵称:<input type="text" name="nich" id="nich">
<br>
密码:<input type="password" name="passw" id="passw">
<br>
确认密码:<input type="password" name="passw1" id="passw1">
<br>
<input type="submit" value="注册">

</form>

</body>
</html>


zhcchl.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>注册处理页面</title>
</head>
<body>

<%

//接收用户名,密码,昵称
String user = request.getParameter("user");
String passw = request.getParameter("passw");
String nich = request.getParameter("nich");

//取用户名对应的application对象
Object obj = application.getAttribute(user);

//如果没有找到用户名,跳转到错误页面
if(user == null || user.trim().length() == 0)
{
response.sendRedirect("cuowu.jsp?mes=1");
}

//如果没有找到密码,跳转到错误页面
else if(passw == null || passw.trim().length() == 0)
{
response.sendRedirect("cuowu.jsp?mes=2");
}

//如果没有找到昵称,跳转到错误页面
else if(nich == null || nich.trim().length() == 0)
{
response.sendRedirect("cuowu.jsp?mes=3");
}

//如果用户名已存在,跳转至错误页面
else if(obj != null)
{
response.sendRedirect("cuowu.jsp?mes=4");
}

else
{

//将注册信息写入application对象
application.setAttribute(user, user + "#" + nich + "#" + passw);

//跳转至错误页面提示注册成功
response.sendRedirect("cuowu.jsp?mes=5");
}

%>

</body>
</html>


2,登录功能,包括两个页面

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>登录页</title>

<script type="text/javascript">

//js方法验证用户名密码是否为空
function yanzh()
{
if(form1.user.value == "")
{
alert("用户名不能为空");
return false;
}
else if(form1.password.value == "")
{
alert("密码不能为空");
return false;
}
else
{
return true;
}
}
</script>

</head>
<body>

<!-- 登录表单 -->
<form action="logac.jsp" name="form1" id="from1" onsubmit="return yanzh()" method="post">

请登录
<br>
用户名:<input type="text" name="user" id="user">
<br>
密码:<input type="password" name="password" id="password">
<br>
<input type="submit" value="登录"> <a href="zhuce.jsp">注册</a>

</form>

</body>
</html>


logac.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>登录处理页</title>
</head>
<body>

<%

//接收登录表单用户名,密码
String user = request.getParameter("user");
String password = request.getParameter("password");

//如果没有收到用户名,跳转错误页面
if(user == null || user.trim().length() == 0)
{
response.sendRedirect("cuowu.jsp?mes=1");
}

//如果没有收到密码,跳转错误页面
else if(password == null || password.trim().length() == 0)
{
response.sendRedirect("cuowu.jsp?mes=2");
}

//
else
{

//取出用户名对应的application对象
Object o = application.getAttribute(user);

//如果没找到application对象
if(o == null)
{
//跳转到错误页面
response.sendRedirect("cuowu.jsp?mes=6");
}

//如果找到
else
{

//从application对象中取出昵称,密码
String inich = o.toString().split("#")[1];
String ipassw = o.toString().split("#")[2];

//判断密码如果正确
if(ipassw.trim().equals(password.trim()))
{
//登录成功输出欢迎信息
String zhnich = new String(inich.getBytes("iso-8859-1"),"utf-8");//中文乱码处理
out.print("欢迎" + zhnich + "登录!" + " <a href='liuyan.jsp'>留言</a> <a href='zhuxiao.jsp'>注销</a>");

//创建会话对象,存入昵称
session.setAttribute("user", zhnich);
}

//如果密码错误,跳转错误页面
else
{
response.sendRedirect("cuowu.jsp?mes=7");
}
}
}

%>

</body>
</html>


3.留言功能,一个页面,liuyan.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import = "java.util.*" %>
<%@ page import ="java.text.*" %>
<!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>留言簿</title>
<%

String userName = "";

//检查登录状态
Object o = session.getAttribute("user");

//如果会话不存在,说明登录失效,跳转错误页面
if(o == null)
{
response.sendRedirect("cuowu.jsp?mes=8");
}
//如果存在,取留言人
else
{
userName = o.toString();
}

//获取表单留言
String liuyan = request.getParameter("liuyan");

//如果留言不为空
if(liuyan != null && liuyan != "")
{

String strly = new String(liuyan.getBytes("ISO-8859-1"),"utf-8");

//附加时间信息
Date dt = new Date();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//获取以liuyan为名称的application对象
Object obj = application.getAttribute("liuyan");
ArrayList<String> A;

//如果对象为空,说明这是第一条留言,则创建集合类对象A
if(obj == null)
{
A  = new ArrayList<String>();
}

//如果不为空将以前的留言赋给集合A
else
{
A = (ArrayList<String>)obj;
}

//将新留言信息加入集合
A.add(strly +  "    留言日期:" +df.format(dt) + "    留言人:" + userName);

//将留言集合A写入application对象
application.setAttribute("liuyan", A);

}

//如果没有输入留言就提交表单则刷新页面而不输出
else
{

}

%>

</head>
<body>

最新留言:<br><br>
<%

//留言编号
int n = 1;

//获取以liuyan为名称的application对象
Object obj = application.getAttribute("liuyan");

//如果不为空,遍历集合A,输出留言信息
if(obj != null)
{

ArrayList<String> A = (ArrayList<String>)obj;

for(int i = A.size()-1; i >= 0; i--)
{

out.print(n + "." + A.get(i) + "<br>");
n++;
}
}

%>
<form>
<br>请输入留言内容:<br>
<textarea rows=10 cols =30 name="liuyan"></textarea><br>

<input type="submit" value="提交">
<br>
<a href="zhuxiao.jsp">注销</a>
</form>
</body>
</html>


4.公共页面

cuowu.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>错误显示跳转页</title>
</head>
<body>

<%

//接收错误代码
String mes = request.getParameter("mes");

//如果没有收到,或者不是正整数,提示出错
if(mes == null || !(mes.matches("\\d+")))
{
out.print("请正确传递错误代码!");
}
else
{
int imes = Integer.parseInt(mes);
//根据错误代码输出相应错误信息
switch(imes)
{
case 1 :
out.print("用户名无效");
break;
case 2 :
out.print("密码无效");
break;
case 3 :
out.print("昵称无效");
break;
case 4 :
out.print("用户已存在");
break;
case 5 :
out.print("注册成功!稍后转到登录页面");
break;
case 6 :
out.print("用户不存在");
break;
case 7 :
out.print("密码错误");
break;
case 8 :
out.print("登录已失效,请重新登录");
break;
default:
out.print("无效的错误代码");
break;

}

}
//3秒后重定向至登录页面
response.setHeader("refresh", "3;URL=login.jsp");

%>

</body>
</html>


zhuxiao.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>注销处理</title>
</head>
<body>
<%
//注销会话后跳转至登录页面
session.invalidate();
response.sendRedirect("login.jsp");
%>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: