[英]Problem of retrieving username and password using Ajax and Java
I am using Ajax in a jsp page wherein we have a form of filling with Username and Password. 我在jsp页面中使用Ajax,其中我们有一个填写用户名和密码的形式。 I am able to retrieve them in the same page.
我可以在同一页面中检索它们。 But while passing it to a Servlet ie Admin.java file both username and password are not been passed.
但是在将其传递给Servlet即Admin.java文件时,都没有传递用户名和密码。
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 在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);
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. 请帮忙。
You expect them as request parameters u
and p
, you should thus also pass them as such: 你希望它们作为请求参数
u
和p
,你也应该这样传递它们:
data: { u: user, p: pass },
or change your servlet to retrieve them with the given names in data {}
或更改您的servlet以使用
data {}
的给定名称检索它们
String userName = request.getParameter("username");
String password = request.getParameter("password");
On the other hand, the System.out.println()
prints to the stdout (which end up in logfile), not to the HTTP response. 另一方面,
System.out.println()
打印到stdout(最终在logfile中),而不是HTTP响应。 If you expect them in the response, so that it's available as content of response
in function(response)
, then you need to print to the HTTP response. 如果您希望它们在响应中,以便它可用作
function(response)
的response
内容,那么您需要打印到HTTP响应。
response.setContentType("text/plain;charset=UTF-8");
PrintWriter writer = response.getWriter();
writer.println(userName);
writer.println(password);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.