使用Ajax和Java检索用户名和密码的问题
我在 jsp 页面中使用 Ajax,其中有一个填写用户名和密码的表单。我能够在同一页面中检索它们。但是,当将其传递给 Servlet 即 Admin.java 文件时,用户名和密码都没有被传递。
代码:
function prepareLoginDialog(){
$dialog = $('<div></div>')
.html('<div id="dialog-confirm" title="Enter the login details here :">'+
'User: <input type="text" id="u" name="u" ></input><br />'+
'Pass: <input type="password" id="p" name="p" ></input>'+
'</div>')
//System.out.println(user);
//System.out.println(pass);
.dialog({
autoOpen: false,
modal: true,
title: 'Login',
buttons: {
'Login': function() {
//Send Login Request to Server
var user = document.getElementById("u").value;
var pass = document.getElementById("p").value;
alert(user);
alert(pass);
//System.out.println(u.text);
login(user,pass);
$(this).dialog('close');
},
Cancel: function() {
$(this).dialog('close');
}
}
登录功能是:
function login(user, pass) {
//ToDo: User Login check
alert(user);
$.ajax({
type:"GET",
url: "Admin?action=login",
dataType: "html",
data: { username: user, password: pass },
success: function(response){
prepareLogoutDialog();
prepareLoggedIn();
}
});
在 Java 文件中,它
request.getSession().setAttribute("access", true);
System.out.println("Admin:doGet:login:true");
System.out.println("u");
String userName = request.getParameter("u");
String password = request.getParameter("p");
System.out.println(userName);
System.out.println(password);
不会被打印。 在通过之前我需要将用户名和密码转换为字符串吗?如果是的话我们该怎么做?
请帮忙。
I am using Ajax in a jsp page wherein we have a form of filling with Username and Password. I am able to retrieve them in the same page. But while passing it to a Servlet i.e. Admin.java file both username and password are not been passed.
Code:
function prepareLoginDialog(){
$dialog = $('<div></div>')
.html('<div id="dialog-confirm" title="Enter the login details here :">'+
'User: <input type="text" id="u" name="u" ></input><br />'+
'Pass: <input type="password" id="p" name="p" ></input>'+
'</div>')
//System.out.println(user);
//System.out.println(pass);
.dialog({
autoOpen: false,
modal: true,
title: 'Login',
buttons: {
'Login': function() {
//Send Login Request to Server
var user = document.getElementById("u").value;
var pass = document.getElementById("p").value;
alert(user);
alert(pass);
//System.out.println(u.text);
login(user,pass);
$(this).dialog('close');
},
Cancel: function() {
$(this).dialog('close');
}
}
The Login Function is:
function login(user, pass) {
//ToDo: User Login check
alert(user);
$.ajax({
type:"GET",
url: "Admin?action=login",
dataType: "html",
data: { username: user, password: pass },
success: function(response){
prepareLogoutDialog();
prepareLoggedIn();
}
});
In the Java file it is
request.getSession().setAttribute("access", true);
System.out.println("Admin:doGet:login:true");
System.out.println("u");
String userName = request.getParameter("u");
String password = request.getParameter("p");
System.out.println(userName);
System.out.println(password);
Not getting printed.
Do I need to convert the username and password to string before passing? If so how do we do that?
Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您期望它们作为请求参数
u
和p
,因此您也应该这样传递它们:或者更改您的 servlet 以使用
data { 中的给定名称检索它们}
另一方面,
System.out.println()
打印到 stdout(最终在日志文件中),而不是打印到 HTTP 响应。如果您希望它们出现在响应中,以便它可以作为function(response)
中的response
内容使用,那么您需要打印到 HTTP 响应。相关问题:
You expect them as request parameters
u
andp
, you should thus also pass them as such:or change your servlet to retrieve them with the given names in
data {}
On the other hand, the
System.out.println()
prints to the stdout (which end up in logfile), not to the HTTP response. If you expect them in the response, so that it's available as content ofresponse
infunction(response)
, then you need to print to the HTTP response.Related questions: