登录时,验证码检验失败,onclick事件无响应

随锋而行 2017-03-03 03:19:20
<%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>响应式后台管理系统 </title>
<meta name="keywords" content="HTML5模版,Bootstrap模版,Bootstrap后台模版,Bootstrap网站后台,Bootstrap3模版,Bootstrap后台管理系统模版,Bootstrap主题" />
<meta name="description" content="JS代码网提供HTML5模版,Bootstrap模版,Bootstrap后台模版下载">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<!-- The styles -->
<link id="bs-css" href="css/bootstrap-cerulean.css" rel="stylesheet">
<style type="text/css">
body {
padding-bottom: 40px;
}
.sidebar-nav {
padding: 9px 0;
}
</style>
<link href="${pageContext.request.contextPath}/css/bootstrap-responsive.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/css/charisma-app.css" rel="stylesheet">
<link href="${pageContext.request.contextPath}/css/jquery-ui-1.8.21.custom.css" rel="stylesheet">
<link href='${pageContext.request.contextPath}/css/fullcalendar.css' rel='stylesheet'>
<link href='${pageContext.request.contextPath}/css/fullcalendar.print.css' rel='stylesheet' media='print'>
<link href='${pageContext.request.contextPath}/css/chosen.css' rel='stylesheet'>
<link href='${pageContext.request.contextPath}/css/uniform.default.css' rel='stylesheet'>
<link href='${pageContext.request.contextPath}/css/colorbox.css' rel='stylesheet'>
<link href='${pageContext.request.contextPath}/css/jquery.cleditor.css' rel='stylesheet'>
<link href='${pageContext.request.contextPath}/css/jquery.noty.css' rel='stylesheet'>
<link href='${pageContext.request.contextPath}/css/noty_theme_default.css' rel='stylesheet'>
<link href='${pageContext.request.contextPath}/css/elfinder.min.css' rel='stylesheet'>
<link href='${pageContext.request.contextPath}/css/elfinder.theme.css' rel='stylesheet'>
<link href='${pageContext.request.contextPath}/css/jquery.iphone.toggle.css' rel='stylesheet'>
<link href='${pageContext.request.contextPath}/css/opa-icons.css' rel='stylesheet'>
<link href='${pageContext.request.contextPath}/css/uploadify.css' rel='stylesheet'>

<!-- The HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

<!-- The fav icon -->
<link rel="shortcut icon" href="img/favicon.ico">

<script type="text/javascript">
/***
function test()
{
var username=$("#username").val();
var password=$("#password").val();
alert(username);
alert(password);
}
***/
function login(){
//获取文本数据
var username=$("#username").val();
var password=$("#password").val();
var captcha=$("#captcha").val();
//String captcha = captcha.getParameter("captcha");
String rand=(String)session.getAttribute("rand");
if(captcha.length <= 0)
{
alert("请输入验证码!");
window.location.href="${pageContext.request.contextPath}/login.jsp"
}

else if (captcha.equals(rand))
{

} else {
//认证失败,请返回登录界面,重新输验证码!
alert("验证码错误");
window.location.href="${pageContext.request.contextPath}/login.jsp"

}
//使用Jquery的Ajax方式访问数据---begin
$.ajax({

type:"POST",
async:true,
url:"${pageContext.request.contextPath}/LoginServlet",
data:{
"username":username,
"password":password,

},
dataType:"text",
success:function (data){
if(data=='1'){
window.location.href="${pageContext.request.contextPath}/MeetingServlet?mtype=list"
}
else{
alert("登录失败,请重新登录");
}
}

});
//使用Jquery的Ajax方式访问数据---end
}
</script>
</head>

<body>
<div class="container-fluid">
<div class="row-fluid">

<div class="row-fluid">
<div class="span12 center login-header">
<h2>欢迎使用后台管理系统</h2>
</div><!--/span-->
</div><!--/row-->

<div class="row-fluid">
<div class="well span5 center login-box">
<div class="alert alert-info">
请输入你的用户名和密码.
</div>
<form class="form-horizontal" action="index.html" method="post">
<fieldset>
<div class="input-prepend" title="Username" data-rel="tooltip">
<span class="add-on"><i class="icon-user"></i></span>
<input autofocus class="input-large span10" name="username" id="username" type="text" value="zxf" />
</div>

<div class="clearfix"></div>
<div class="input-prepend" title="Password" data-rel="tooltip">
<span class="add-on"><i class="icon-lock"></i></span>
<input class="input-large span10" name="password" id="password" type="password" value="666" />
</div>

<div
<!-- 验证码图片的生成-->
<img id="code" src="Image.jsp"/>
<a href="#" onclick="javascript:var dt=new Date();document.getElementById('code').src='Image.jsp?dt='+dt;"> 
<img alt="看不清,换一张"src="Images/1.png"/></a>(*区分字母大小写)
</div>
<div class="input-prepend" title="Captcha" data-rel="tooltip">
<span class="add-on"><i class="icon-captcha"></i></span>
<input autofocus class="input-large span10" name="captcha" id="captcha" type="text" value="" />
</div>
<div class="clearfix"></div>
<div class="input-prepend">
<label class="remember" for="remember"><input type="checkbox" id="remember" />记住密码</label>
</div>
<div class="clearfix"></div>


<p class="center span5">
<button type="button" class="btn btn-primary" onclick="login()">登录</button>
<button type="button" class="btn btn-primary" onclick=window.open("register1.jsp")>注册</button>
</p>
<!--
<button type="button" class="btn btn-success" onclick="test()">测试按钮</button>
-->
</fieldset>
</form>
</div><!--/span-->
</div><!--/row-->
</div><!--/fluid-row-->

</div><!--/.fluid-container-->

<!-- external javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->

<!-- jQuery -->
<script src="js/jquery-1.7.2.min.js"></script>
<!-- jQuery UI -->
<script src="js/jquery-ui-1.8.21.custom.min.js"></script>
<!-- transition / effect library -->
<script src="js/bootstrap-transition.js"></script>
<!-- alert enhancer library -->
<script src="js/bootstrap-alert.js"></script>
<!-- modal / dialog library -->
<script src="js/bootstrap-modal.js"></script>
<!-- custom dropdown library -->
<script src="js/bootstrap-dropdown.js"></script>
<!-- scrolspy library -->
<script src="js/bootstrap-scrollspy.js"></script>
<!-- library for creating tabs -->
<script src="js/bootstrap-tab.js"></script>
<!-- library for advanced tooltip -->
<script src="js/bootstrap-tooltip.js"></script>
<!-- popover effect library -->
<script src="js/bootstrap-popover.js"></script>
<!-- button enhancer library -->
<script src="js/bootstrap-button.js"></script>
<!-- accordion library (optional, not used in demo) -->
<script src="js/bootstrap-collapse.js"></script>
<!-- carousel slideshow library (optional, not used in demo) -->
<script src="js/bootstrap-carousel.js"></script>
<!-- autocomplete library -->
<script src="js/bootstrap-typeahead.js"></script>
<!-- tour library -->
<script src="js/bootstrap-tour.js"></script>
<!-- library for cookie management -->
<script src="js/jquery.cookie.js"></script>
<!-- calander plugin -->
<script src='js/fullcalendar.min.js'></script>
<!-- data table plugin -->
<script src='js/jquery.dataTables.min.js'></script>

<!-- chart libraries start -->
<script src="js/excanvas.js"></script>
<script src="js/jquery.flot.min.js"></script>
<script src="js/jquery.flot.pie.min.js"></script>
<script src="js/jquery.flot.stack.js"></script>
<script src="js/jquery.flot.resize.min.js"></script>
<!-- chart libraries end -->

<!-- select or dropdown enhancer -->
<script src="js/jquery.chosen.min.js"></script>
<!-- checkbox, radio, and file input styler -->
<script src="js/jquery.uniform.min.js"></script>
<!-- plugin for gallery image view -->
<script src="js/jquery.colorbox.min.js"></script>
<!-- rich text editor library -->
<script src="js/jquery.cleditor.min.js"></script>
<!-- notification plugin -->
<script src="js/jquery.noty.js"></script>
<!-- file manager library -->
<script src="js/jquery.elfinder.min.js"></script>
<!-- star rating plugin -->
<script src="js/jquery.raty.min.js"></script>
<!-- for iOS style toggle switch -->
<script src="js/jquery.iphone.toggle.js"></script>
<!-- autogrowing textarea plugin -->
<script src="js/jquery.autogrow-textarea.js"></script>
<!-- multiple file upload plugin -->
<script src="js/jquery.uploadify-3.1.min.js"></script>
<!-- history.js for cross-browser state change on ajax -->
<script src="js/jquery.history.js"></script>
<!-- application script for Charisma demo -->
<script src="js/charisma.js"></script>


</body>
</html>
...全文
282 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
前端后端搞不清楚。。就算你内嵌服务器端代码也是获取不到最新的验证吗的值,因为你服务器端代码运行后已经发送到客户端了,此时客户端才img重新加载验证码动态页生成验证码和内容存入session中 你要用ajax发送输入的验证码内容到服务器端重新验证,或者给验证码img添加onload事件,用ajax重新获取验证码中session存储内容,而不是直接内嵌服务器端代码
回复
随锋而行 2017-03-03
引用 4 楼 Free_Wind22 的回复:
String rand=(String)session.getAttribute("rand"); 你这js 和 jsp混一块了...
我将<%String rand=(String)session.getAttribute("rand");%> 提到body中,可是要怎么在function login(){}中验证输入的验证码是否相等 if (captcha.equals(rand)) { } 不行啊
回复
2017-03-03
String rand=(String)session.getAttribute("rand"); 你这js 和 jsp混一块了...
回复
chenchen3872 2017-03-03
回复
随锋而行 2017-03-03
就是添加了下面这些代码后,出现了问题 不知道错在哪里 var captcha=$("#captcha").val(); //String captcha = captcha.getParameter("captcha"); String rand=(String)session.getAttribute("rand"); if(captcha.length <= 0) { alert("请输入验证码!"); window.location.href="${pageContext.request.contextPath}/login.jsp" } else if (captcha.equals(rand)) { } else { //认证失败,请返回登录界面,重新输验证码! alert("验证码错误"); window.location.href="${pageContext.request.contextPath}/login.jsp" }
回复
随锋而行 2017-03-03
var username=$("#username").val();
var password=$("#password").val();
var captcha=$("#captcha").val();
//String captcha = captcha.getParameter("captcha");
String rand=(String)session.getAttribute("rand");
if(captcha.length <= 0)
{
alert("请输入验证码!");
window.location.href="${pageContext.request.contextPath}/login.jsp"
}

else if (captcha.equals(rand))
{

} else {
//认证失败,请返回登录界面,重新输验证码!
alert("验证码错误");
window.location.href="${pageContext.request.contextPath}/login.jsp"

}
回复
相关推荐
发帖
JavaScript
创建于2007-09-28

8.5w+

社区成员

Web 开发 JavaScript
申请成为版主
帖子事件
创建了帖子
2017-03-03 03:19
社区公告
暂无公告