如何实现用户名或密码错误,弹出重新登录的提示

登录页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录界面</title>

<style type="text/css">
	body{font-size:16 }
</style>

<script type="text/javascript">
	function mycheck() {
		//判断用户名是否为空
		if(form1.userName.value==""){
			alert("用户名不能为空,请输入用户名!")
			form1.userName.focus();
			return;
		}
		//判断密码是否为空
		if(form1.password.value==""){
			alert("密码不能为空,请输入密码!")
			form1.password.focus();
			return;
		}
		//判断验证码是否为空
		if(form1.validationCode.value==""){
			alert("验证码不能为空,请输入验证码!")
			form1.validationCode.focus();
			return;
		}
		//判断验证码是否正确
		if(form1.validationCode.value!=form1.validationCode1.value){
			alert("请输入正确的验证码!")
			form1.validationCode.focus();
			return;
		}
	}
</script>

</head>
<body>
	<form action="loginCheck.jsp" name="form1" method="post">
		用户名:<input type="text" name="userName" size="16">	
		<br>&nbsp;&nbsp;&nbsp;码:<input type="text" name="password"size="18">	
		<br>
		验证码:<input type="text"name="validationCode"
		onkeydown="if(event.keyCode==13){form1.submit.focus();}" size="6">
		<%
			int intmethod1=(int)(((Math.random())*11)-1);
			int intmethod2=(int)(((Math.random())*11)-1);
			int intmethod3=(int)(((Math.random())*11)-1);
			int intmethod4=(int)(((Math.random())*11)-1);
			//将得到的随机数进行连接
			String intsum=intmethod1+""+intmethod2+intmethod3+intmethod4;
		%>
		<!-- 设置隐藏域,验证比较时使用 -->
		<input type="hidden" name="validationCode1" value="<%=intsum %>">
		<!-- 将图片名称与得到随机数相同的图片显示在页面上 -->
		<img alt="" src="../image/<%=intmethod1%>.png">
		<img alt="" src="../image/<%=intmethod2%>.png">
		<img alt="" src="../image/<%=intmethod3%>.png">
		<img alt="" src="../image/<%=intmethod4%>.png">
		
		<br>
		
		<input type="submit" name="submit1" value="登录" onclick="mycheck()">
		<input type="reset" value="重置">
	</form>
</body>
</html>

验证页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>处理登录页面的数据</title>
</head>
<body>
	<%
		//设置请求编码,用于解决中文乱码问题
		request.setCharacterEncoding("UTf-8");
		String name=request.getParameter("userName");
		String password=request.getParameter("password");
		if(request.getParameter("validationCode1").equals(request.getParameter("validationCode"))){
			if(name.equals("WWW")&&(password.equals("123"))){
				//把用户名保存到session中
				session.setAttribute("userName", name);
				response.sendRedirect("main.jsp");
			}else{
				String script="<script>alert('用户名或密码错误,请重新登陆');location.href='login.jsp'</script>";
				response.getWriter().println(script);
			}
		}
	%>
</body>
</html>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值