zoukankan      html  css  js  c++  java
  • 我的第一个jsp程序-实现注册登录留言功能

    1,注册功能,包括两个页面

    zhuce.jsp注册页面

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>注册页面</title>
     8 
     9 <script type="text/javascript">
    10 //js方法,验证用户名,昵称,密码是否为空,两次密码输入是否一致
    11 function yanzh()
    12 {
    13     if(form1.user.value == "")
    14         {
    15         alert("用户名不能为空");
    16         return false
    17         }
    18     else if(form1.passw.value == "")
    19         {
    20         alert("密码不能为空");
    21         return false
    22         }
    23     else if(form1.nich.value == "")
    24         {
    25         alert("昵称不能为空");
    26         return false
    27         }
    28     else if(form1.passw.value != form1.passw1.value)
    29         {
    30         alert("两次输入密码不一致");
    31         return false
    32         }
    33     else
    34         {
    35         return true
    36         }
    37         
    38 }
    39 
    40 </script>
    41 
    42 </head>
    43 <body>
    44 <!-- 注册表单 -->
    45 <form action="zhcchl.jsp" name="form1" id="form1" onsubmit="return yanzh()" method="post">
    46 
    47 请注册
    48 <br>
    49 用户名:<input type="text" name="user" id="user">
    50 <br>
    51 昵称:<input type="text" name="nich" id="nich">
    52 <br>
    53 密码:<input type="password" name="passw" id="passw">
    54 <br>
    55 确认密码:<input type="password" name="passw1" id="passw1">
    56 <br>
    57 <input type="submit" value="注册">
    58 
    59 </form>
    60 
    61 </body>
    62 </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登录处理页面

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>登录处理页</title>
     8 </head>
     9 <body>
    10 
    11 <%
    12 
    13 //接收登录表单用户名,密码
    14 String user = request.getParameter("user");
    15 String password = request.getParameter("password");
    16 
    17 //如果没有收到用户名,跳转错误页面
    18 if(user == null || user.trim().length() == 0)
    19 {
    20     response.sendRedirect("cuowu.jsp?mes=1");
    21 }
    22 
    23 //如果没有收到密码,跳转错误页面
    24 else if(password == null || password.trim().length() == 0)
    25 {
    26     response.sendRedirect("cuowu.jsp?mes=2");
    27 }
    28 
    29 //
    30 else
    31 {
    32     
    33 //取出用户名对应的application对象    
    34 Object o = application.getAttribute(user);
    35 
    36     //如果没找到application对象
    37     if(o == null)
    38     {
    39         //跳转到错误页面
    40         response.sendRedirect("cuowu.jsp?mes=6");
    41     }
    42     
    43     //如果找到
    44     else
    45     {
    46     
    47         //从application对象中取出昵称,密码
    48         String inich = o.toString().split("#")[1];
    49         String ipassw = o.toString().split("#")[2];
    50         
    51         //判断密码如果正确
    52         if(ipassw.trim().equals(password.trim()))
    53         {
    54             //登录成功输出欢迎信息
    55             String zhnich = new String(inich.getBytes("iso-8859-1"),"utf-8");//中文乱码处理
    56             out.print("欢迎" + zhnich + "登录!" + " <a href='liuyan.jsp'>留言</a> <a href='zhuxiao.jsp'>注销</a>");
    57             
    58             //创建会话对象,存入昵称
    59             session.setAttribute("user", zhnich);
    60         }
    61         
    62         //如果密码错误,跳转错误页面
    63         else
    64         {
    65             response.sendRedirect("cuowu.jsp?mes=7");            
    66         }
    67     }
    68 }
    69 
    70 
    71 %>
    72 
    73 </body>
    74 </html>

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

      1 <%@ page language="java" contentType="text/html; charset=UTF-8"
      2     pageEncoding="UTF-8"%>
      3 <%@ page import = "java.util.*" %>
      4 <%@ page import ="java.text.*" %>
      5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
      6 <html>
      7 <head>
      8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      9 <title>留言簿</title>
     10 <%
     11 
     12 String userName = "";
     13 
     14 //检查登录状态
     15 Object o = session.getAttribute("user");
     16 
     17 //如果会话不存在,说明登录失效,跳转错误页面
     18 if(o == null)
     19 {
     20     response.sendRedirect("cuowu.jsp?mes=8");
     21 }
     22 //如果存在,取留言人
     23 else
     24 {    
     25     userName = o.toString();
     26 }
     27 
     28 //获取表单留言
     29 String liuyan = request.getParameter("liuyan");
     30 
     31 //如果留言不为空
     32 if(liuyan != null && liuyan != "")
     33 {
     34     
     35     String strly = new String(liuyan.getBytes("ISO-8859-1"),"utf-8");
     36     
     37     //附加时间信息
     38     Date dt = new Date();
     39     SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
     40     
     41     //获取以liuyan为名称的application对象
     42     Object obj = application.getAttribute("liuyan");
     43     ArrayList<String> A;
     44     
     45     //如果对象为空,说明这是第一条留言,则创建集合类对象A
     46     if(obj == null)
     47     {
     48         A  = new ArrayList<String>();        
     49     }
     50     
     51     //如果不为空将以前的留言赋给集合A
     52     else
     53     {        
     54         A = (ArrayList<String>)obj;                
     55     }
     56     
     57     //将新留言信息加入集合
     58     A.add(strly +  " &nbsp;&nbsp;&nbsp;留言日期:" +df.format(dt) + " &nbsp;&nbsp;&nbsp;留言人:" + userName);
     59     
     60     //将留言集合A写入application对象
     61     application.setAttribute("liuyan", A);
     62     
     63 }
     64 
     65 //如果没有输入留言就提交表单则刷新页面而不输出
     66 else
     67 {
     68     
     69 }
     70 
     71 %>
     72 
     73 
     74 </head>
     75 <body>
     76 
     77 最新留言:<br><br>
     78 <%
     79 
     80 //留言编号
     81 int n = 1;
     82 
     83 //获取以liuyan为名称的application对象
     84 Object obj = application.getAttribute("liuyan");
     85 
     86 //如果不为空,遍历集合A,输出留言信息
     87 if(obj != null)
     88 {
     89     
     90     ArrayList<String> A = (ArrayList<String>)obj;
     91     
     92     for(int i = A.size()-1; i >= 0; i--)
     93     {
     94         
     95         out.print(n + "." + A.get(i) + "<br>");
     96         n++;
     97     }
     98 }
     99 
    100 %>
    101 <form>
    102 <br>请输入留言内容:<br>
    103 <textarea rows=10 cols =30 name="liuyan"></textarea><br>
    104 
    105 <input type="submit" value="提交">
    106 <br>
    107 <a href="zhuxiao.jsp">注销</a>
    108 </form>
    109 </body>
    110 </html>

    4.公共页面

    cuowu.jsp错误提示页面

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>错误显示跳转页</title>
     8 </head>
     9 <body>
    10 
    11 <%
    12 
    13 //接收错误代码
    14 String mes = request.getParameter("mes");
    15 
    16 //如果没有收到,或者不是正整数,提示出错
    17 if(mes == null || !(mes.matches("\d+")))
    18 {
    19     out.print("请正确传递错误代码!");    
    20 }
    21 else
    22 {
    23     int imes = Integer.parseInt(mes); 
    24     //根据错误代码输出相应错误信息
    25     switch(imes)
    26     {
    27         case 1 :
    28             out.print("用户名无效");
    29         break;
    30         case 2 :
    31             out.print("密码无效");
    32         break;
    33         case 3 :
    34             out.print("昵称无效");
    35         break;
    36         case 4 :
    37             out.print("用户已存在");
    38         break;
    39         case 5 :
    40             out.print("注册成功!稍后转到登录页面");
    41         break;
    42         case 6 :
    43             out.print("用户不存在");
    44         break;
    45         case 7 :
    46             out.print("密码错误");
    47         break;
    48         case 8 :
    49             out.print("登录已失效,请重新登录");
    50         break;
    51         default:
    52             out.print("无效的错误代码");
    53         break;
    54     
    55     }
    56 
    57 }
    58 //3秒后重定向至登录页面
    59 response.setHeader("refresh", "3;URL=login.jsp");
    60 
    61 %>
    62 
    63 
    64 </body>
    65 </html>

    zhuxiao.jsp注销页面

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     4 <html>
     5 <head>
     6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     7 <title>注销处理</title>
     8 </head>
     9 <body>
    10 <%
    11 //注销会话后跳转至登录页面
    12 session.invalidate();
    13 response.sendRedirect("login.jsp");
    14 %>
    15 </body>
    16 </html>
  • 相关阅读:
    配置文件和脚本文件区别
    .sh
    瘋耔思维空间
    vi编辑器的三种模式
    在ubuntu系统荣品开发配套JDK安装
    如何查看自己运行ubuntu是32位还是64位
    志气
    高仿微信朋友圈
    Java OCR tesseract 图像智能字符识别技术 Java代码实现
    构建基于Javascript的移动CMS——加入滑动
  • 原文地址:https://www.cnblogs.com/dirgo/p/5006002.html
Copyright © 2011-2022 走看看