2 Replies Latest reply: May 2, 2013 5:44 AM by Tolls RSS

    Reset "the form" by passing between 2 jsp's ?!?

    1006663
      Hello, I have a HelloWorld.java_:

      package com.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);
      }
      }

      and an index.jsp_:

      <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>
      <link rel="stylesheet" type="text/css" href="styles.css"/>
      <BR>Please enter your name:<input type='text' id='userInput'/>
      <div id="hello"><button type="button" onclick="makeRequest()">Next</button></div>
      <div id="stuff"><button type="button" onclick="changeText()">Welcome</button></div>
      <div id="ttt"><input type="text"></input></div>
      <p>Welcome to the site <b id='boldStuff'>dude</b> </p>
      <%--<textarea rows="7" cols="150" name="txt"></textarea>--%>

      <script type="text/javascript">
      function changeText(){
      var userInput = document.getElementById('userInput').value;
      var newHTML = "<span style='color:#ff0e00'>" + userInput + "</span>";
      document.getElementById('boldStuff').innerHTML = newHTML;
      }
      </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;
      }

      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;
      document.getElementById("ttt").innerHTML = userInput;
      } else {
      alert("HTTP error " + xmlHttpRequest.status + ": " + xmlHttpRequest.statusText);
      }
      }
      };
      }

      //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");
      xmlHttpRequest.send(null);
      }

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

      the problem is that when I press the "Next" button I get into "basic.jsp" code but the content of index.jsp is remaining on the screen
      I get a mix-up of the two forms. I want to clear up the window when I load the second form. What do I do wrong or what should I do to "reset" the web page
      to load the second form correctly?

      Thanks!

      Edited by: 1003660 on 02.05.2013 02:33
        • 1. Re: Reset "the form" by passing between 2 jsp's ?!?
          gimbal2
          Use \
           tags when posting code. I wouldn't hold my breath waiting for someone to debug your manual ajax logic for you though, your question has absolutely nothing to do with Java. Somewhere in your DHTML updating logic you are doing something wrong, you just need to go find it. That will take time and patience - but first you need to understand what the code is actually doing. 
          
          I would consider using a popular web framework now that you are making these advanced steps in web development, using something like Wicket or Play framework will make your life a whole lot easier once you get over the learning hump. Now you're just reinventing the wheel, making all the mistakes that comes along with that.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
          • 2. Re: Reset "the form" by passing between 2 jsp's ?!?
            Tolls
            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);
            }
            I would have thought you'd get an error here as you open the output stream (getWriter) and start writing to it, then forward to your jsp.
            The jsp to then try and get the output stream, which normally results in an IllegalStateException.