开发者

Problem of retrieving username and password using Ajax and Java

开发者 https://www.devze.com 2023-02-07 21:57 出处:网络
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 use

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.


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:

  • Update current page with a servlet
0

精彩评论

暂无评论...
验证码 换一张
取 消