zoukankan      html  css  js  c++  java
  • Servlet作业1-实现注册登录

    1,注册功能

    注册页面zhuce.html

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

    注册处理Servlet: Zhcchl.java

      1 package com.hanqi;
      2 
      3 import java.io.IOException;
      4 
      5 import javax.servlet.ServletContext;
      6 import javax.servlet.ServletException;
      7 
      8 import javax.servlet.http.HttpServlet;
      9 import javax.servlet.http.HttpServletRequest;
     10 import javax.servlet.http.HttpServletResponse;
     11 import com.hanqi.*;
     12 
     13 /**
     14  * Servlet implementation class Zhcchl
     15  */
     16 public class Zhcchl extends HttpServlet {
     17     private static final long serialVersionUID = 1L;
     18     
     19      
     20     /**
     21      * @see HttpServlet#HttpServlet()
     22      */
     23     public Zhcchl() {
     24         super();
     25         // TODO Auto-generated constructor stub
     26     }
     27 
     28     /**
     29      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     30      */
     31     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     32         //解决中文乱码
     33         response.setCharacterEncoding("GBK");
     34         //request.setCharacterEncoding("GBK");
     35         //response.setContentType("text/html;charset=GBK");
     36         
     37         // 接收表单提交信息        
     38         String user = request.getParameter("user");
     39         String nich = request.getParameter("nich");
     40         String passw = request.getParameter("passw");
     41         
     42         
     43         
     44         
     45         //判断接收信息合法性
     46         if(user == null || user.trim().length() == 0)
     47         {
     48             response.getWriter().append("用户名不能为空");
     49             response.setHeader("refresh", "3;URL=zhuce.html");
     50         }
     51         else if(nich == null || nich.trim().length() == 0)
     52         {
     53             response.getWriter().append("昵称不能为空");
     54             response.setHeader("refresh", "3;URL=zhuce.html");
     55         }
     56         else if(passw == null || passw.trim().length() == 0)
     57         {
     58             response.getWriter().append("密码不能为空");
     59             response.setHeader("refresh", "3;URL=zhuce.html");
     60         }
     61         else
     62         {
     63             ServletContext application = this.getServletContext();
     64                 
     65             if(application.getAttribute(user) != null)
     66             {
     67                 response.getWriter().append("用户已存在");
     68                 response.setHeader("refresh", "3;URL=zhuce.html");
     69 
     70             }
     71             else
     72             {
     73                 //实例化Zhcxx类
     74                 Zhcxx zc = new Zhcxx();
     75                 //将信息存入zc对象
     76                 zc.setUser(user);
     77                 zc.setNich(nich);
     78                 zc.setPassw(passw);
     79                 
     80                 //将用户注册信息写入application
     81                 
     82                 application.setAttribute(user, zc);
     83                 
     84                 response.getWriter().append("注册成功");
     85                 
     86                 response.setHeader("refresh", "3;URL=index.html");
     87             }
     88         }
     89         //response.getWriter().append("Served at: ").append(request.getContextPath());
     90     }
     91 
     92     /**
     93      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     94      */
     95     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     96         // TODO Auto-generated method stub
     97         doGet(request, response);
     98     }
     99 
    100 }

    自定义存储用户注册信息的类:Zhcxx.java

     1 package com.hanqi;
     2 
     3 public class Zhcxx {
     4     
     5     private String user;
     6 
     7     public String getUser() {
     8         return user;
     9     }
    10 
    11     public void setUser(String user) {
    12         this.user = user;
    13     }
    14     
    15     private String nich;
    16     public String getNich() {
    17         return nich;
    18     }
    19 
    20     public void setNich(String nich) {
    21         this.nich = nich;
    22     }
    23 
    24     private String passw;
    25     public String getPassw() {
    26         return passw;
    27     }
    28 
    29     public void setPassw(String passw) {
    30         this.passw = passw;
    31     }
    32 
    33     
    34 
    35 }

    2登录功能

    登录页面index.html

     1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     2 <html>
     3 <head>
     4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     5 <title>登录页</title>
     6 
     7     <script type="text/javascript">
     8     
     9     //js方法验证用户名密码是否为空
    10     function yanzh()
    11     {
    12         if(form1.user.value == "")
    13             {
    14             alert("用户名不能为空");
    15             return false;
    16             }
    17         else if(form1.password.value == "")
    18             {
    19             alert("密码不能为空");
    20             return false;
    21             }
    22         else
    23             {
    24             return true;
    25             }
    26     }
    27     </script>
    28 
    29 </head>
    30 <body>
    31 
    32 <!-- 登录表单 -->
    33 <form action="Logac" name="form1" id="from1" onsubmit="return yanzh()" method="post">
    34 
    35     请登录
    36     <br>
    37     用户名:<input type="text" name="user" id="user">
    38     <br>
    39     密码:<input type="password" name="password" id="password">
    40     <br>
    41     <input type="submit" value="登录">
    42     <a href="zhuce.html">注册</a>
    43 </form>
    44 
    45 </body>
    46 </html>

    登录处理Servlet:logac.java

     1 package com.hanqi;
     2 
     3 import java.io.IOException;
     4 
     5 import javax.servlet.ServletContext;
     6 import javax.servlet.ServletException;
     7 import javax.servlet.http.HttpServlet;
     8 import javax.servlet.http.HttpServletRequest;
     9 import javax.servlet.http.HttpServletResponse;
    10 
    11 /**
    12  * Servlet implementation class Logac
    13  */
    14 public class Logac extends HttpServlet {
    15     private static final long serialVersionUID = 1L;
    16        
    17     /**
    18      * @see HttpServlet#HttpServlet()
    19      */
    20     public Logac() {
    21         super();
    22         // TODO Auto-generated constructor stub
    23     }
    24 
    25     /**
    26      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    27      */
    28     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    29         // 指定中文字符集
    30         response.setCharacterEncoding("GBK");
    31         //request.setCharacterEncoding("GBK");
    32         //response.setContentType("text/html;charset=GBK");
    33         //接收表单信息
    34         String user = request.getParameter("user");
    35         
    36         String passw = request.getParameter("password");
    37         
    38         //判断获取信息合法性
    39         if(user == null || user.trim().length() == 0)
    40         {
    41             response.getWriter().append("用户名非法");
    42             response.setHeader("refresh", "3;URL=index.html");
    43         }
    44         else if(passw == null || passw.trim().length() == 0)
    45         {
    46             response.getWriter().append("密码非法");
    47             response.setHeader("refresh", "3;URL=index.html");
    48         }
    49         else
    50         {
    51             //创建application对象
    52             ServletContext application = this.getServletContext();
    53             //取出以user为名存储的对象
    54             Object obj = application.getAttribute(user);
    55             //如果对象不存在说明用户名不存在
    56             if(obj == null)
    57             {
    58                 response.getWriter().append("用户名不存在");
    59                 response.setHeader("refresh", "3;URL=index.html");
    60             }
    61             else
    62             {
    63                 //实例化Zhcxx
    64                 Zhcxx zhc = (Zhcxx)obj;
    65                 //从zhc对象取出密码,昵称
    66                 String ipsw = zhc.getPassw();
    67                 String inch = new String(zhc.getNich().getBytes("ISO-8859-1"),"UTF-8");
    68                 //判断密码是否正确
    69                 if(passw.trim().equals(ipsw.trim()))
    70                 {
    71                     response.getWriter().println("欢迎" + inch +"登录");
    72                 }
    73                 else
    74                 {
    75                     response.getWriter().println("密码错误");
    76             response.setHeader("refresh", "3;URL=index.html");
    77                 }
    78             }
    79         }
    80         
    81         //response.getWriter().append("Served at: ").append(request.getContextPath());
    82     }
    83 
    84     /**
    85      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    86      */
    87     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    88         // TODO Auto-generated method stub
    89         doGet(request, response);
    90     }
    91 
    92 }
  • 相关阅读:
    C语言关键字
    C语言返回值
    五、Vue:使用axios库进行get和post、用拦截器对请求和响应进行预处理、Mock(数据模拟)
    四、Vue过渡与动画、过渡css类名、自定义指定、过滤器
    三、深入Vue组件——Vue插槽slot、动态组件
    二、Vue组件(component):组件的相互引用、通过props实现父子组件互传值
    一、Vue环境搭建及基础用法
    Django(十三)状态保持 —— cookie与session+ajax异步请求+session记住登录状态+cookie记住登录名密码
    Django(十二)视图--利用jquery从后台发送ajax请求并处理、ajax登录案例
    Django(十一)视图详解:基本使用、登录实例、HttpReqeust对象、HttpResponse对象
  • 原文地址:https://www.cnblogs.com/dirgo/p/5013275.html
Copyright © 2011-2022 走看看