Member Avatar

Hi,

Below I have code that contains ajax on a JSP. The JSP runs the Servlet which gets a parameter from the JSP and returns the exact same input field's value. For some reason, it is returning a null though. Can someone please assist?

Servlet:

package Web;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

public class fieldProcessing extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Date time = new Date();
        int i = 0;
        String uN = request.getParameter("username");
        try {
            out.println(uN);
            //out.println(time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds() + " Num: " + ++i + " ya");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

JSP:

<html>
    <head>
        <title></title>
        <script type="text/javascript">
            function init(){
                window.setInterval("ajaxFunction()",1000);
            }

            function ajaxFunction() {
                var xmlhttp;
                if (window.XMLHttpRequest) {
                    // code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp=new XMLHttpRequest();
                }
                else if (window.ActiveXObject) {
                    // code for IE6, IE5
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                else {
                    alert("Your browser does not support XMLHTTP!");
                }
                xmlhttp.onreadystatechange=function() {
                    if (xmlhttp.readyState==4) {
                        document.myForm.time.value=xmlhttp.responseText;
                        document.getElementById("testText").rows[0].cells[0].innerHTML=xmlhttp.responseText;
                    }
                }
                xmlhttp.open("GET","fieldProcessing",true);
                xmlhttp.send(null);
            }
        </script>
    </head>
    <body onload="init();">
        <form name="myForm">
            Name: <input type="text" name="username" onkeyup="ajaxFunction();" />
            Time: <input type="text" name="time" />
            <table border="1" id="testText">
                <tr>
                    <td>
                        I bet you $20 this will change soon!
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

I created this in Netbeans, I'm sure it would work if you just created a new Servlet and titled it "fieldProcessing.java" and a new JSP and titled it "index.jsp".

Thanks!
-Ashton.

Recommended Answers

You are not sending the username to the servlet, you ahve to post the form to the servlet or in the ajax function send the username in the url, so the url would look something like this:
http://localhost/servlet?username=Bob

Jump to Post

All 4 Replies

You are not sending the username to the servlet, you ahve to post the form to the servlet or in the ajax function send the username in the url, so the url would look something like this:
http://localhost/servlet?username=Bob

Member Avatar

How do you post with ajax?

var url = "servlet";
var params = "name=Bob";
http.open("POST", url, true);
http.send(params);

I have left out the parts of the ajax code that would normally remain the same.
There are loads of information on the net regarding ajax post if you are still lost, I used this site a while ago: http://www.openjs.com/articles/ajax_xmlhttp_using_post.php

Member Avatar

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.