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 userid = document.getElementById("userid");        //获取 userid 这个id名的相关数据
    if (userid.value == "")                                                                    //判断输入的 userid 是否为空
        {
            alert("用户代码不可为空!");
            return false;                                                                            //返回一个空值使下方按钮不执行操作
        }
    if(uform.password.value == "")
        {
            alert("密码不能为空");
            return false;
        }
        return true;
}
</script>
</head>
<body>

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

账户:<input id="userid" type="text" name="userid" width=30 />
<br>
密码:<input id="password" type="password" name="password" width=30 />
<br>
<input type="submit" value="登录" />

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

</form>
</body>
</html>
登录页
<%@ 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("传递的msgid不认识");
        
        break;
    }
}

response.setHeader("refresh", "3;URL=login.jsp");

%>

</body>
</html>
消息页面
<%@ 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 strUserid = request.getParameter("userid");

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

if (strUserid == null || strUserid.trim().length() == 0)
{
    response.sendRedirect("message.jsp?msgid=1");
}
else if (strPW == null || strPW.trim().length() == 0)
{
    response.sendRedirect("message.jsp?msgid=2");
}
else
{
    //查找用户信息
    Object obj = application.getAttribute(strUserid);
    
    if (obj != null)
    {
        String strUser = obj.toString();        
        String[] user = strUser.split("#");        
        String strUID = user[0];
        String strUsername = user[1];
        String strP = user[2];    
        if(strPW.equals(strP))
        {
            out.print("欢迎 " + strUsername + " 登录系统");
            
            //创建 session 用来判断是否已登陆
            session.setAttribute("dfe", strUserid );
            
        }
        else
        {
            response.sendRedirect("message.jsp?msgid=3");
        }
    }
    else
    {
        response.sendRedirect("message.jsp?msgid=4");
    }
}
%>
<br>
<a href="liuyan.jsp">留言簿</a>
</body>
</html>
登录验证页
<%@ 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 strUserid = request.getParameter("userid");
String strUsername = request.getParameter("username");
String strPW = request.getParameter("password");
if (strUserid == null || strUserid.trim().length() == 0)
{
    response.sendRedirect("message.jsp?msgid=1");
}
else if (strUsername == null || strUsername.trim().length() == 0)
{
    response.sendRedirect("message.jsp?msgid=5");
}
else if (strPW == null || strPW.trim().length() == 0)
{
    response.sendRedirect("message.jsp?msgid=2");
}
else
{
    //查找用户信息
    Object obj = application.getAttribute(strUserid);
    if (obj != null)
    {
        response.sendRedirect("message.jsp?msgid=7");
    }
    else
    {
    strUsername = new String(strUsername.getBytes("ISO-8859-1"),"UTF-8");
    String strUser = strUserid + "#" + strUsername + "#" + strPW;
    application.setAttribute(strUserid, strUser);
    response.sendRedirect("message.jsp?msgid=6");
    }
}
%>
</body>
</html>
保存用户信息
<%@ 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 yanz()
{
    if  (zhugan.userid.value == "")
        {
            alert("用户代码不能为空!");
            return false;
        }
    if(zhugan.username.value == "")
        {
            alert("用户名称不能为空!");
            return false;
        }
    if(zhugan.password.value == "")
        {
        alert("登陆密码不能为空!");
        return false;
        }
    /*
    if(zhugan.passwore.value == "")
        {
            alert("确认密码不能为空!");
            return false;
            }
    */
    if(zhugan.password.value != zhugan.passwore.value)
    {
        alert("登陆密码与确认密码不相同!");
        return false;
    }
return true;
}
</script>
</head>
<body>
<form id="zhugan" action="saveUser.jsp"  method="post"   onSubmit="return yanz();">      
用户代码:<input id="userid" type="text" name="userid" width=30 />
<br><br>
用户名称:<input id="username" type="text" name="username" width=30 />
<br><br>
登录密码:<input id="password" type="password" name="password" width=30 />
<br><br>
确认密码:<input id="passwore" type="password" name="pueren" width=30 />
<br><br>
<input type="submit" value="提交" />
</form>
</body>
</html>
用户注册
<%@ 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("dfe");
if(o == null)
{
    response.sendRedirect("message.jsp?msgid=8");
}
else
{
    username = o.toString();
}
String liuy = request.getParameter("liuy");
if(liuy != null && !liuy.equals(""))        //liuy 不为空  liuy 也不为空字符串
{    
    String strLiuy = new String(liuy.getBytes("ISO-8859-1"),"UTF-8");                    //转码
    //附加时间信息
    Date dt = new Date();
    //格式化时间
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    //   &nbsp;   真实空格
    strLiuy += "&nbsp; &nbsp; &nbsp; &nbsp;    " + sdf.format(dt) + "留言人:" + username;
    Object obj = application.getAttribute("liuy");
    
    
    
    
    //简化代码
    ArrayList<String> A;
    if(obj == null)
    {
        A =  new ArrayList<String>();
    }
    else
    {
        A = (ArrayList<String>)obj;
    }
    A.add( strLiuy);
    application.setAttribute("liuy", A);    
    /*
    if(obj == null)
    {
        
        ArrayList<String> al = new ArrayList<String>();
        
        al.add(strLiuy);
        
        application.setAttribute("liuy", al);    
    }
    else
    {
        ArrayList<String> A = (ArrayList<String>)obj;
        
        A.add( strLiuy);
        
        application.setAttribute("liuy", A);    
    }
    */
    
    
    
    
    
    }
else
{
    
}
%>
</head>
<body>
<form>
最新留言:
<br>
<br><br>
<%
    int i = 1;
Object obj = application.getAttribute("liuy");

if(obj != null)
{
    //取出留言
    ArrayList<String> A = (ArrayList<String>)application.getAttribute("liuy");
//    for(String ly : A)                
         for(int m = A.size() - 1; m >= 0 ; m--)        //新出现的在上边
    {
    //    out.print(i + "、" + ly + "<br>");     
    out.print(i + "、" + A.get(m) + "<br>");        //实现才出现的在上边
        i++;
    }
}
%>
留言内容:
<textarea rows="10" cols="30" name="liuy"></textarea>
<br>
<input type="submit" value="提交" />
</form>
</body>
</html>
留言页面
原文地址:https://www.cnblogs.com/name-hanlin/p/5005629.html