动态页面,登陆,注册,留言 JSP

登陆页   

主要使用html表单,javascript验证注册信息

<%@ 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">

function check()
{
    var uid = document.getElementById("userid");
    
    if(uid.value =="")
        {
        alert("请输入用户名");
        
        return false;
        }
    if(dlform.password.value == "")
        {
        alert("密码不能为空")
        
        return false;
        }
    return true;
    }
</script>

</head>
<body>

<form id="dlform" action="yanzheng.jsp" method="post" onSubmit="return check();">

用户名:<input id="userid" name="userid" type="text" value="请输入用户名" size="20"/>

密码:<input id="password" name="password" type="password" size="20"/>

<input type="submit" value="登陆" />

<a href="zhuce.jsp">注册新用户</a>

</form>

</body>
</html>

 

登陆验证页

使用request获取登录信息,response页面重定向,application获取参数,out输出提示。

<%@ 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 userid = request.getParameter("userid");

String password = request.getParameter("password");
//获取保存的注册信息
Object user = application.getAttribute("userxinxi");

    if(user == null)  //判断获取注册信息为空
    {
        response.sendRedirect("cuowutishi.jsp?msgid=4");
    }
    else    //判断用户名密码能否登陆
    {
        String[] zcuser = user.toString().split("#") ;

        String zcuserid = zcuser[0];
        String zcusername = zcuser[1];
        String zcpassword = zcuser[2];
    
         if(userid.equals(zcuserid))
        {
            if(password.equals(zcpassword))
            {
                out.print("欢迎" + zcusername + "登陆成功");
            
            //跳转到留言页面
            session.setAttribute("login", zcusername);
            
            }
            else
            {
                response.sendRedirect("cuowutishi.jsp?msgid=3");
            }
            }
        else
        {
            response.sendRedirect("cuowutishi.jsp?msgid=4");
        }
}


%>
<br><br>
<a href="liuyan.jsp">留言簿</a>


</body>
</html>

 

注册页

主要使用html表单,javascript验证注册信息

<%@ 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">
function check()
{
    if(zcform.zcid.value == "")
        {
        alert("请输入用户名");
        
        return false;
        }
    
    if(zcform.zcname.value == "")
        {
        alert("请输入用户昵称");
        
        return false;
        }
    
    if(zcform.zcpassword.value == "")
        {
        alert("请输入密码");
    
        return false;
        }
    
    if(zcform.qrpassword.value == "")
        {
        alert("请输入确认密码");

        return false;
        }
    if(zcform.qrpassword.value != zcform.zcpassword.value)
        {
        alert("确认密码与用户密码不一致");
        
        return false;
        }
    
    return true;
    }
</script>

</head>
<body>

<form id= "zcform" action="saveUser.jsp" method="get" onsubmit="return check();">

用户代码:<input id="zcid" name="userid" type="text" size="20" /><br><br>

用户名称:<input id="zcname" name="username" type="text" size="20" /><br><br>

用户密码:<input id="zcpassword" name="password" type="password" size="20" /><br><br>

确认密码:<input id="qrpassword" name="qrpassword" type="password" size="20" /><br><br>

<input type="submit" value="提交" /><br>

<a href="login.jsp">返回登陆页</a>

</form>

</body>
</html>

 

保存注册页

request获取注册信息,注意中文转码,application储存注册信息,response页面重定向

<%@ 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 userid = request.getParameter("userid");

String username = new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");

String password = request.getParameter("password");

Object user = application.getAttribute("userxinxi");
    
if(user != null)  //判断是否已被注册
    {
           response.sendRedirect("cuowutishi.jsp?msgid=7");
    }
else         //保存注册信息
    {
        String userxinxi = userid + "#" + username + "#" + password;
    
        application.setAttribute("userxinxi", userxinxi);
    
        response.sendRedirect("cuowutishi.jsp?msgid=6");
    }
%>

</body>
</html>

 

留言页

session查看登陆状态,request获取留言信息,application储存和获取留言信息,response页面重定向,out输出留言。

<%@ 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>



</head>
<body>

<%

String userName ="";

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

if(o == null)
{
    response.sendRedirect("cuowutishi.jsp?msgid=8");
}
else
{
    userName = o.toString();
}
    //获取留言信息
    String liuy = request.getParameter("liuyan");
    //判断留言信息不是空值和空字符串
    if(liuy != null && !liuy.equals(""))
    {
        String strliuy = new String(liuy.getBytes("ISO-8859-1"),"UTF-8");
        
        Object obj = application.getAttribute("liuyan");
        
        //附加时间信息
        Date dt = new Date();
        //格式化时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        
        String time = sdf.format(dt);
        //留言内容 + 留言时间 + 留言人
        strliuy += "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" + time +
        "&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;" + "留言人“" + userName;
        
        ArrayList<String> A;
        
        if (obj == null)
        {
            A = new ArrayList<String>();
        }
        else 
        {
            A = (ArrayList<String>)obj;
        }
        A.add(strliuy);

response.sendRedirect("liuyan.jsp"); //页面重定向,防止刷新提交数据 application.setAttribute(
"liuyan", A); } else { } %> <form id="lyform" action="liuyan.jsp" method="post"> 最新留言<br><br> <% //输出留言 int i = 1; Object obj = application.getAttribute("liuyan"); //判断获取的值不为空 if (obj != null) { ArrayList<String> A = (ArrayList<String>)obj; for(int m = A.size() - 1; m >= 0 ; m--) //倒数遍历 { out.print(i + ". " + A.get(m) + "<br>" ); i++; } } %> <br><br> 留言簿<br> <textarea name="liuyan" rows="20" cols="100"></textarea> <br> <input name="tijiao" type="submit" value="提交留言"/> </form> </body> </html>

 

错误提示页

response页面重定向,定时跳转页面,out输出错误提示。

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.util.*" %>
<!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 strMsgId = request.getParameter("msgid");
//判断错误信息是否为空
if(strMsgId == null || strMsgId.trim().length() == 0)
{
    out.print("请正确传递信息");
}
else
{
    int iMsgid = Integer.parseInt(strMsgId);
            
    switch(iMsgid)
    {
    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;
    }
}
//返回登陆页
response.setHeader("refresh", "3;URL=login.jsp");

%>



</body>
</html>

 

posted @ 2015-11-30 08:33  老趙  阅读(1167)  评论(0编辑  收藏  举报