使用Ajax和Java检索用户名和密码的问题

发布于 2024-10-15 22:05:40 字数 2150 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别念他 2024-10-22 22:05:40

您期望它们作为请求参数 up,因此您也应该这样传递它们:

data: { u: user, p: pass },

或者更改您的 servlet 以使用 data { 中的给定名称检索它们}

String userName = request.getParameter("username");
String password = request.getParameter("password");

另一方面,System.out.println() 打印到 stdout(最终在日志文件中),而不是打印到 HTTP 响应。如果您希望它们出现在响应中,以便它可以作为 function(response) 中的 response 内容使用,那么您需要打印到 HTTP 响应。

response.setContentType("text/plain;charset=UTF-8");
PrintWriter writer = response.getWriter();
writer.println(userName);
writer.println(password);

相关问题:

You expect them as request parameters u and p, you should thus also pass them as such:

data: { u: user, p: pass },

or change your servlet to retrieve them with the given names in 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. 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.

response.setContentType("text/plain;charset=UTF-8");
PrintWriter writer = response.getWriter();
writer.println(userName);
writer.println(password);

Related questions:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文