我需要在UserDao.java中使用login_role值。我尝试将$('#login_role').val()
传递给session.setAttribute("type", type)
(因此对于typer变量)并从UserDao.java
文件中获取值。但我无法将$('#login_role').val()
类型更改为String。所以我试图通过ajax将login_role
值传递给java文件。但还是有一些问题。也许有人可以帮助我吗?
<script type="text/javascript">
$(function () {
//setup before functions
var typingTimer; //timer identifier
var doneTypingInterval = 500; //time in ms
var $input = $('#login_email');
//on keyup, start the countdown
$input.on('keyup', function () {
clearTimeout(typingTimer);
typingTimer = setTimeout(doneTyping, doneTypingInterval);
});
//on keydown, clear the countdown
$input.on('keydown', function () {
clearTimeout(typingTimer);
});
});
//user is "finished typing"
function doneTyping() {
$.ajax({
url: "ajax/log_in.jsp",
data: {
email: $("#login_email").val(),
typer: $('#login_role').val()
},
success: function (data) {
$("#log_in").html(data);
},
error: function (data) {
}
});
}
function loginPanel() {
var login_email = $('#login_email').val();
var login_password = $('#login_password').val();
if (login_email.length === 0 && login_password.length === 0) {
alert('Write your email or password');
return false;
} else if (login_email.length === 0) {
alert('Write your email.');
return false;
} else if (login_password.length === 0) {
alert('Write your password.');
return false;
} else {
$.ajax({
url: "UserDao",
data: {
login_email: $("#login_email").val(),
login_password: $("#login_password:").val(),
typer: $('#typer').val()
},
success: function (data) {
alert("success");
},
error: function (data) {
alert("error");
}
});
}
}
</script>
<form action="UserDao" method="post">
<div class="form-group has-feedback">
<input type="email" name="login_email" id="login_email" class="form-control" placeholder="Email" />
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="password" name="login_password" id="login_password" class="form-control" placeholder="Password" />
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div id="log_in">
</div><!-- log_in -->
<div class="row">
<div class="col-xs-8">
<div class="checkbox icheck">
<label>
<input type="checkbox" value="remember-me" id="remember_me"> Remember Me
</label>
</div>
</div><!-- /.col -->
<div class="col-xs-4">
<input type="hidden" id="typer" name="typer">
<button name="btnLogin" onclick="loginPanel()" class="btn btn-primary btn-block btn-flat">Sign In</button>
</div><!-- /.col -->
</div>
</form>
<script type="text/javascript">
$(function () {
//setup before functions
var typingTimer; //timer identifier
var doneTypingInterval = 500; //time in ms
var $input = $('#login_email');
//on keyup, start the countdown
$input.on('keyup', function () {
clearTimeout(typingTimer);
typingTimer = setTimeout(doneTyping, doneTypingInterval);
});
//on keydown, clear the countdown
$input.on('keydown', function () {
clearTimeout(typingTimer);
});
});
//user is "finished typing"
function doneTyping() {
$.ajax({
url: "ajax/log_in.jsp",
data: {
email: $("#login_email").val(),
typer: $('#login_role').val()
},
success: function (data) {
$("#log_in").html(data);
},
error: function (data) {
}
});
}
</script>
<% int type = -1;
if (!request.getParameter("email").trim().equals("")) {
type = Dao.checkUserType(request.getParameter("email"));
if (type >= 0) {%>
<div id="log_in">
<% if (type == 1) { %>
<div class="form-group has-feedback">
<select class="form-control" name="login_role" id="login_role">
<option value="1">Dean</option>
<option value="0">Staff</option>
</select>
</div>
<% } else if (type == 2) { %>
<div class="form-group has-feedback">
<select class="form-control" name="login_role" id="login_role">
<option value="2">Head of Department</option>
<option value="1">Dean</option>
<option value="0">Staff</option>
</select>
</div>
<% } else if (type == 3) { %>
<div class="form-group has-feedback">
<select class="form-control" name="login_role" id="login_role">
<option value="3">Admin</option>
<option value="2">Head of Department</option>
<option value="1">Dean</option>
<option value="0">Staff</option>
</select>
</div>
<% } else {
session.setAttribute("typer", 0);
}%>
</div>
<%
} else {
%>
<div id="log_in">
</div>
<% }
} else { %>
<div id="log_in">
</div>
<% }%>
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
HttpSession session = request.getSession(true);
if (session.getAttribute("typer") == null || "".equals(session.getAttribute("typer"))) {
System.out.println("typer is null");
} else {
System.out.println(session.getAttribute("typer"));
}
/* TODO output your page here. You may use following sample code. */
}
}
答案 0 :(得分:0)
我认为你这里有一个拼写错误:$("#login_password:")
应该是$("#login_password")
?