1

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.

jmj
  • 237,923
  • 42
  • 401
  • 438
Archana
  • 237
  • 3
  • 9
  • 17
  • 1
    The values "u" and "p" don't appear to be passed as request parameters at all? Your AJAX request only includes their values in the data object - as "username" and "password". – Michael Feb 02 '11 at 11:15

1 Answers1

4

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555