zoukankan      html  css  js  c++  java
  • 用servlet进行用户名和密码校验

    一:用户名和密码容错显示

    二:登录处理

    三:登录成功,并提示密码。

    百度云链接:链接: https://pan.baidu.com/s/13qRvqz7w1rh99gmHOk-0Bg

    提取码: cpi4 

    四:各部分代码

    html:

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>西南石油大学电子邮件系统</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <script type="text/javascript" src="js/login.js"></script>
    </head>
    
    <body>
    
    <div class="top">
    <div class="r1">
    <div class="r2">
    <div class="logo"></div>
    </div>
    <a href="" target="" class="help">帮助</a>
    </div>
    </div>
    
    <div class="content">
    <div class="loginBar">
    <div class="box">
    <div class="tab">
    账号登录
    <div class="dragbar"></div>
    </div>
    </div>
    <div class="boxc">
    <div style="height: 10px;"></div>
    <div style="margin-left: 42px;  270px; height: 30px;">
    <div class="hh" id="hd">用户登录</div>
    </div>
    <form method="post" onsubmit="return ajax()" >
    <input type="text" class="text" name="username" id="username" style="ime-mode: disabled" _autocomplete="off" placeholder="用户名" />
    <input type="password" class="text" name="password" id="password" _autocomplete="off" placeholder="密码" />
    <div style="height: 10px;"></div>
    <div class="bl">
    <span style="float: left;"> <font style="color: red; font-family: 宋体; clear: both;">学生选择@stu.swpu.edu.cn</font>
    </span> <span style="float: right;"> <a href="" style="outline: none; color: #999;">忘记密码</a>
    </span>
    </div>
    <input type="submit" class="btn" value="登 录" style="background: url(img/login_btn.jpg)" />
    </form>
    
    </div>
    </div>
    </div>
    
    <div class="bottom">西南石油大学</div>
    
    </body>
    </html>

    servlet:

    package com.swpu;
    import java.io.IOException;
    import java.io.PrintWriter;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
    * Servlet implementation class LoginServlet
    */
    @WebServlet("/LoginServlet")
    public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private String User;
    private String Password;
    
    /**
    * @see HttpServlet#HttpServlet()
    */
    public LoginServlet() {
    super();
    // TODO Auto-generated constructor stub
    }
    
    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.getWriter().append("Served at: ").append(request.getContextPath());
    }
    
    /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.setContentType("text/html;charset=UTF-8");
    response.setCharacterEncoding("utf-8");
    PrintWriter out=response.getWriter();
    //获取参数
    User=request.getParameter("userName");
    Password=request.getParameter("passWord");
    if(User.equals("tom") && Password.equals("123")) {
    //这个字符串将会在js中被接收
    //1代表登录成功
    //2代表用户名或密码为空
    //3代表用户名或密码错误
    out.write("1");
    }else if(User==""||Password==""){
    out.write("2");
    }else {
    out.write("3");
    }
    out.close();
    }
    }
  • 相关阅读:
    JavaScript入门篇 编程练习
    JavaScript入门篇 第三天(认识DOM)
    JavaScript入门篇 第二天(消息对话框+网页弹出)
    JavaScript入门篇 第一天
    网页布局基础 第四次
    本内容中发现无效字符。处理资源 'file:///C:/Users/XDJ/Desktop/1111/press.xml' 时出错。第 5 行,位置: 11 <author>ƽ
    jquery中的each用法以及js中的each方法实现实例
    jQuery获取页面及个元素高度、宽度
    js正则函数match、exec、test、search、replace、split使用介绍集合
    javascript中window.open()与window.location.href的区别
  • 原文地址:https://www.cnblogs.com/oy0411/p/10626040.html
Copyright © 2011-2022 走看看