开发者

Need help for ajax

开发者 https://www.devze.com 2023-02-22 05:00 出处:网络
HTML Code : <html> <head> <script type=\"text/javascript\"> function checkforValid(str)

HTML Code :

<html>  
<head>
<script type="text/javascript">
function checkforValid(str)
{
        var xmlhttp;
        if (str.length==0)
        { 
              document.getElementById("txtHint").innerHTML="";
              return;
        }
        if (window.XMLHttpRequest)
        {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
         }
    开发者_如何学Go    else
        {
            // code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","get.jsp?q=" + str ,true);
        xmlhttp.send();
    }
    </script>
    </head>

    <body>

        <form action=""> 
            Name: <input type="text" id="user" name = "user" onkeyup="checkforValid(this.value)" />
        </form>

        <br>
        <p>Here : <span id="txtHint"></span> </p> 

    </body>
</html>

JSP:

<%@ page language="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.math.*" %>
<%@ page import="java.security.*" %>
<html>
<body>
<%
    String user = request.getParameter("user");
    out.println("Username is::"+user+".");
    Connection con = null;

    try
    {
        Connection conn = null;
        String url = "jdbc:mysql://localhost:3306/";
        String dbName = "p2p";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "root";
        String password = "123";

        Class.forName(driver).newInstance();
        conn = DriverManager.getConnection(url+dbName,userName,password);

        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery( "select * from newuser where username =" + user  );

            if(rs.next())
            out.println("ok");
            else out.println("absent");

        st.close();
    }
    catch( Exception e )
    {
        out.print( "Database Error"+ e );
    }
    finally
    {
        try
        {
            con.close();
        }
        catch(Exception e1)
        {
        }
    }

%>
</body>
</html>

When i run it on glassfish the,jsp page request.getParameter function is receiving null i.e. it is outputting User:null, so pls help and also suggest some nice projects for ajax


You are sending the username parameter to the JSP in the variable named q in your code and retrieving using the variable user

xmlhttp.open("GET","get.jsp?q=" + str ,true);

Now there can be two fixes :

First and the best

Fix in the javascript , change the name of variable from q to user like this, and let the JSP code remain unchanged.

xmlhttp.open("GET","get.jsp?user=" + str ,true);

Second Fix (not recommended)

Fix the code in the JSP.

Instead of String user = request.getParameter("user"); change it to String user = request.getParameter("q"); and let the script remain as it is...

I think this should do the trick.


Two alternatives for you.

1: Chage request.getParameter("user"); to request.getParameter("q");

2: Submit the form using ajax and you will get the user parameter.


You are sending:

xmlhttp.open("GET","get.jsp?q=" + str ,true);

But then asking:

String user = request.getParameter("user");

It's null because you never send a "user" parameter. Change it to:

xmlhttp.open("GET","get.jsp?user=" + str ,true);

Update


A few AJAX tutorials/documentation sites:

http://www.xul.fr/en-xml-ajax.html

http://code.google.com/edu/ajax/tutorials/ajax-tutorial.html

http://www.hunlock.com/blogs/AJAX_for_n00bs (and many other docs in there)

http://www.ibm.com/developerworks/web/library/wa-ajaxintro1/index.html

And, of course, BalusC's blog:

http://balusc.blogspot.com/2009/05/javajspjsf-and-javascript.html

0

精彩评论

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