0

我想在文本框中读取一个名称,并且我想将它传递给下一个表单,如果表单不会重置为仅显示第二个表单“basic.jsp”,这将是一个问题。有没有重置表格的命令?现在它向我显示了 basic.jsp 与 index.jsp (名称请求)混合的内容......

-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);
            }
        }
    };
}

- 基本的.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>
4

1 回答 1

0

您没有发送userInput到服务器。您必须将其添加到请求中才能在 servlet 中接收它。现在你只是在做xmlHttpRequest.send(null)。相反,发送代表输入数据的参数字符串。就像是:

xmlHttpRequest.send("userInput=" + userInputValue);
于 2013-05-03T08:29:36.223 回答