2013-05-03 11 views
0

我想在文本框中讀取一個名稱,並且我想將它傳遞給下一個表單,並且這將是一個問題,表單不會重置爲僅顯示第二種形式,「basic.jsp」。是否有任何命令重置表單?現在,它讓我basic.jsp夾雜了的index.jsp(這個名字的請求)的內容......在內容混在一起的兩個jsp的! (第二個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); 
      } 
     } 
    }; 
} 

- 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> 

回答

0

你不發送userInput到服務器。您必須將它添加到請求才能夠在servlet中接收它。現在你只是在做xmlHttpRequest.send(null)。相反,發送表示輸入數據的參數字符串。例如:

xmlHttpRequest.send("userInput=" + userInputValue); 
+0

xmlHttpRequest.send(「userInput」);或者如何? – WDrgn 2013-05-03 08:33:57

+0

非常感謝!關於重置表單以僅顯示第二個表單basic.jsp的內容你知道嗎? – WDrgn 2013-05-03 08:38:13

+0

如果您的servlet返回HTML,那麼您需要從請求的響應中獲取HTML,並在某處插入/替換它。如果你搜索谷歌,我相信你會發現很多例子。 – NilsH 2013-05-03 08:40:58

相關問題