用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; width: 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();
}
}

 

posted @ 2019-03-30 11:15  楼南  阅读(512)  评论(0编辑  收藏  举报