0

I want to read in a textbox a name and I want to pass-it to the next form, and it would be a problem that the form doesn't reset to show only the second form, "basic.jsp". Is there any command to reset the form? Now it shows me the content of basic.jsp mixed up with the index.jsp (request of the name)...

-HelloWorld.java:

package javapapers.sample.ajax;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {

    public void doPost(HttpServletRequest req, HttpServletResponse res)
            throws java.io.IOException, ServletException {
        res.setContentType("text/html");
        res.getWriter().write("Hey!");

        String textNume = req.getParameter("userInput");
        req.setAttribute("nume",textNume);
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("basic.jsp");
        requestDispatcher.forward(req,res);
    }
}

- index.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" language="javascript" src="ajax.js"></script>
</head>
<body>
<BR>Please enter your name:<input type='text' id='userInput'/>
<div id="hello"><button type="button" onclick="makeRequest()">Adauga</button></div>
<div id="ttt"><input type="text"></input></div>
<p>Welcome to the site <b id='boldStuff'>dude</b> </p>
</script>
</body>
</html>

- ajax.js:

function getXMLHttpRequest() {
    var xmlHttpReq = false;
    // to create XMLHttpRequest object in non-Microsoft browsers
    if (window.XMLHttpRequest) {
        xmlHttpReq = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        try {
            // to create XMLHttpRequest object in later versions of Internet Explorer
            xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (exp1) {
            try {
                // to create XMLHttpRequest object in older versions of Internet Explorer
                xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (exp2) {
                xmlHttpReq = false;
            }
        }
    }
    return xmlHttpReq;
}

//AJAX call starts with this function
function makeRequest() {
    var xmlHttpRequest = getXMLHttpRequest();
    xmlHttpRequest.onreadystatechange = getReadyStateHandler(xmlHttpRequest);
    xmlHttpRequest.open("POST", "helloWorld.do", true);
    xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    var userInputValue = document.getElementById('userInput').value;
    xmlHttpRequest.send("userInput=" + userInputValue);
}

function getReadyStateHandler(xmlHttpRequest) {
    // an anonymous function returned it listens to the XMLHttpRequest instance
    return function() {
        if (xmlHttpRequest.readyState == 4) {
            if (xmlHttpRequest.status == 200) {
                var userInput = document.getElementById("userInput").value;
                document.getElementById("hello").innerHTML = xmlHttpRequest.responseText; //"hey" def.in java!
                document.getElementById("ttt").innerHTML = userInput;
            } else {
                alert("HTTP error " + xmlHttpRequest.status + ": " + xmlHttpRequest.statusText);
            }
        }
    };
}

- basic.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<HTML>
<HEAD>
    <TITLE>Elemente de identificare</TITLE>
</HEAD>
<BODY>
<H1>Elemente de identificare</H1>
Domnule <%= request.getAttribute("nume") %> alegeti elementele de identificare:<br>
Felul notificarii<br>
<select name="fel_notif">
    <option value="Prima notificare">Prima notificare</option>
    <%--<option value="Monday" selected>Monday</option>--%>
</select><br>
Mailul dvs <br><textarea rows="1" cols="30" name="mail"></textarea><br>
Caracterizare <br><textarea rows="3" cols="30" name="caract"></textarea><br>
Circumstante <br><textarea rows="3" cols="30" name="circ"></textarea><br>
Masuri de atenuare <br><textarea rows="3" cols="30" name="masuri"></textarea><br>
Cod notificare: <input type="text" name="cod" value="scot din BD" readonly><br>
<INPUT TYPE="SUBMIT" value="Trimite">
<%--<script type="text/javascript" language="javascript" src="ajax.js"></script>
<div id="pdf"><button type="button" onclick="makeRequest()">Creaza PDF</button></div>--%>
</BODY>
</HTML>
WDrgn
  • 521
  • 10
  • 29

1 Answers1

0

Your not sending userInput to the server. You have to add it to the request to be able to receive it in the servlet. Now you're just doing xmlHttpRequest.send(null). Instead, send the parameter string representing the data from your input. Something like:

xmlHttpRequest.send("userInput=" + userInputValue);
NilsH
  • 13,705
  • 4
  • 41
  • 59
  • xmlHttpRequest.send("userInput"); or how? – WDrgn May 03 '13 at 08:33
  • Thanks a lot! about reseting the form to show only the content of the second form basic.jsp do you know something? – WDrgn May 03 '13 at 08:38
  • If your servlet returns HTML, then you need to get the HTML from the response of the request, and insert/replace it in the document somewhere. I'm sure you'll find plenty of examples if you search google. – NilsH May 03 '13 at 08:40
  • I have another project without ajax and it works fine (passing a value from a jsp to another). I am new to java, that's why I put questions like this, sorry. If you can tell me what I did wrong, plsss (I added the basic.jsp also in the initial post) – WDrgn May 03 '13 at 09:09
  • You shouldn't change your question like that. Now this answer doesn't make sense in the new context. Instead, you should just add the new information, or ask a new question. Anyway... If `basic.jsp` is a full HTML document, I don't understand why you need Ajax at all. Just post it as a regular form. – NilsH May 03 '13 at 09:35