0

I have a jsp say with a text field :

<form action="someServlet" method=post>
<input type ="text" name="user" id="uname">
<button type="submit" id="submit">Submit</button>
</form>

So when i type each letter in the jsp in SomeOtherservlet the username should be get before submitting the form and must be processed for future use.... How can i do it???

I tried onKeyup function in jsp and String uname=request.getParameter("user"); in SomeOtherServlet but its passing null value to the servlet.. So please help me....

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
user3349720
  • 179
  • 2
  • 12
  • That does not sound like a viable model for web development. – Elliott Frisch Mar 06 '14 at 06:26
  • 1
    Do you need to hit your servlet on every time or just need to accomplish the auto complete functionality? – Luiggi Mendoza Mar 06 '14 at 06:31
  • @Elliott Frisch To check username availability i need that model... like if a name Rahul is already present in the database and another person wants to take that name again... So till he types RAHU the status should display available and when he types the last letter L the status should be changed to username already taken.... So how do we achieve it... That means each letter should be parsed to the servlet before submit – user3349720 Mar 06 '14 at 06:32
  • @Luiggi Mendoza i think the above comment gave u an idea as what am trying to do... – user3349720 Mar 06 '14 at 06:34
  • @user3349720 you can load the data once and handle the autocomplete in the client, no need to fire an ajax request per keypress/keydown/keyup and fill the server with useless requests... – Luiggi Mendoza Mar 06 '14 at 06:35
  • @Luiggi Mendoza no not auto complete it is just to check with the database whether the username already exists i think as u said i need ajax but am not familiar with ajax so can u give me an idea... – user3349720 Mar 06 '14 at 06:36
  • Read here: http://stackoverflow.com/q/4112686/1065197 – Luiggi Mendoza Mar 06 '14 at 06:36
  • @ElliottFrisch But why submit your old fashioned post at all? means?? – user3349720 Mar 06 '14 at 06:37
  • By the way, if you use an ajax request per key in this case to check against database, your users will have a bad time... – Luiggi Mendoza Mar 06 '14 at 06:42

1 Answers1

0

try this : call a js method in your jsp file in text field like :- and in your js file use these methods. var req;

function initRequest(url) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        initRequest(url);
        req.onreadystatechange = processRequest;
        req.open("POST", url, true); 
        req.send(null);
    }
}

function checkUser() {      
    var val = document.forms["formName"].u_name.value;

    if(val == null || val == "") {

    } else {
        var url = "checkUser.jsp?uname=" + val;
        initRequest(url);
        req.onreadystatechange = processRequest;
        req.open("POST", url, true); 
        req.send(null);
    }
}
function processRequest() {
    //alert(req.readyState);
    if (req.readyState == 4) {
         document.getElementById("usercheck").innerHTML=req.responseText;

    }
}

and now make a page as checkUser.jsp on which you can get parameter value of uname sent throug js and chekc this in your method using db queries. create a div element with an id usercheck that will display the processed result from tha last js method i have shown, to whatever you want to display ,show that using your checkuser.jsp

checkuser.jsp

String user_Name = request.getParameter("uname");
   response.setContentType("text/html;charset=UTF-8");
   PrintWriter pen  =   response.getWriter();
try {

    result              =   ins.populate(user_Name);
} catch (Exception e) {

}
if(result == true) {
    pen.write("<img alt='Available' src='/images/Active.png'>Not Available");
} else {
    pen.write("<img alt='Available' src='/images/Deactive.png'>Available");
}
%>

this will show the result returned on your page a available and not avialble

Divya
  • 1,469
  • 1
  • 13
  • 25
  • i want to use CheckUsername servlet so can i do something like this `var url = "checkUsername?uname=" + val;` Sorry if am wrong cos am not familiar with ajax... and i want a image that shows loading also how can i add it?? – user3349720 Mar 06 '14 at 06:42
  • you wanted the request to take place as the user types while registering your website .you wanted to check its availability of user name at time he is typing for that you use ajax that will be good approach if u beginner .In case of using servlet the whole task will be in the hands of java and it will process a little slow as compared to doing your task in a mixture of ajax + servlet or ajax +jsp.another approach you can use is using dwr. – Divya Mar 06 '14 at 06:49
  • ya let it be a little slow but i need complete java.. so can i use servlet like this `var url = "checkUsername?uname=" + val;` and how to add a loading image?? in ajax?? – user3349720 Mar 06 '14 at 06:52
  • @user3349720 you wanted to show available/not available there only? – Divya Mar 06 '14 at 06:53
  • a servlet is .java which get compiled and you cannot return data from a same servlet for that case you may need to return it to another servlet .i.e you can not make a one to one connection in between your servlet and ajax for that you better use jsp .that will help you to return your data like loading images or anything else – Divya Mar 06 '14 at 06:56
  • and yeah you can use var url = "checkUsername?uname=" + val; it will work – Divya Mar 06 '14 at 06:59
  • Yes i want to show available/unavailable there itself in the jsp form just ryt down the text field..... – user3349720 Mar 06 '14 at 07:09
  • @user3094300 "a servlet is .java which get compiled and you cannot return data from a same servlet for that case you may need to return it to another servlet .i.e you can not make a one to one connection in between your servlet and ajax for that you better use jsp .that will help you to return your data like loading images or anything else" can you just elaborate on it.... – user3349720 Mar 06 '14 at 07:10
  • ya i tried..... but its not updating there in the jsp itself.... thats the problem... my specification is it has to be done at the client side itself... – user3349720 Mar 06 '14 at 07:22
  • and where should i write my db queries and what is ins.populate?? – user3349720 Mar 06 '14 at 07:41
  • use sqlDao-daofactory concept and create a method in it.so that your database connection will remain in .java file.and call that method just like method populate i have called – Divya Mar 06 '14 at 07:46
  • what is ins.populate()? what is "ins"?? – user3349720 Mar 06 '14 at 07:53
  • ins is object of a class(sqlDao) and populate is a mehtod that will check into the database the name passed everytime a user type a letter say R,RA,RAH it will check for its occurance and return true/false on checking – Divya Mar 06 '14 at 08:22
  • don't forget to vote up for the useful answers. if you found any help. – Divya Mar 07 '14 at 09:03