0

I am using a html form like this:

      <form action="question" method="get">

where question is a java servlet class which renders the data from the form and display on other page.

What I am trying to do is display this data just below the html form not on other screen. (Somewhat like the page where we Ask Question in stackoverflow.com where the question you enter is rendered and displayed below.)

So I am trying to do same. Anyone has an idea how to do that?

TacticalCoder
  • 6,275
  • 3
  • 31
  • 39
Navdroid
  • 4,453
  • 7
  • 29
  • 47
  • Do you mean that the data from the form is displayed on the other screen AS YOU TYPE and BEFORE you submit the form (like on Ask Question) ? If so, the servlet solutions below would not work and you'd need to look at something more client-side based. – matt freake Feb 22 '12 at 13:42
  • Have you got any idea what should I try? – Navdroid Feb 22 '12 at 13:44

3 Answers3

1

The simplest way to do it, is to use javascript (client side).

Below is a very crude example on how to do this. This will give you an idea on how to proceed.

  1. create a html page, with two separate text area boxes.

  2. Let the first text area box be the source where you type in the text. Assign it an id 'source_area'.

    <textarea id='source_area'>
    </textarea>
    
  3. Let the second text area box be the destination. Assign it an id 'destination_area'.

    Set this area as "readonly" because you don't want users typing here directly.

    <textarea id='destination_area' readonly>
    </textarea>
    
  4. Now when a user types into the first box, we need to capture the particular action. For this example I will use the "onKeyUp" to capture events when a keyboard key is released.

    Now when typing into the source text box, a key on your keyboard is released, it will invoke a javascript function "transferToNextArea()" is invoked. We will create the javascript function "transferToNextArea()" in

    Read more about javascripts here.
    http://w3schools.com/js/js_events.asp
    Complete list of events here.
    http://w3schools.com/jsref/dom_obj_event.asp

  5. The javascript function will extract text from 'source_area' text box. It will then assign the same text into 'destination_area'.

    function transferToNextArea()
    {
        //extracting text.
        var varSrcText = document.getElementById("source_area").value;  
    
        //assigning text to destination.
        document.getElementById("destination_area").value=varSrcText
    
    }
    

Complete html (tested in Google Chrome)

<html>
    <body >
        Source Box
        <textarea id='source_area' onKeyUp="transferToNextArea();">
        </textarea>
        <br>
        Destination Box
        <textarea id='destination_area' readonly>
        </textarea>
    </body>
    <script type="text/javascript">
        function transferToNextArea()
        {
            var varSrcText = document.getElementById("source_area").value;
            document.getElementById("destination_area").value=varSrcText

        }

    </script>
   </html>

This is just a very basic example. It is not very effecient, but it will give you an idea of how data can be moved around. Before assigning the text, you could manipulate the text however you want it using javascript.

Stackoverflow formats the text as per the html tags after extracting it. This will require lot more code and more work.

Using a servlet for the above task is overkill. You would use a servlet, only if you want to do something with the data on the server side.

Example
    a) store it in a database before displaying it below.

Read about "ajax" calls to send and recieve data between the server and client. Ajax will give you the means to send data to the servlet without having to refresh the whole page.

kensen john
  • 5,439
  • 5
  • 28
  • 36
0
  • Create a JSP with a form

  • on submit post the data to some servlet

  • process request and produce resultant data and set it to request's attribute

  • forward the request to same jsp

  • check if the data is not null display under the form

jmj
  • 237,923
  • 42
  • 401
  • 438
  • I am having similar problem I have tried what you said in above answer but the problem is that when the user refreshes the page the data get entered into the database again so my page displays duplicate entries below the form. Can you please help? – Abubakkar Oct 06 '12 at 12:54
0

Just let the servlet forward the request to the same JSP page and use JSTL <c:if> to conditionally display the results.

request.setAttribute("questions", questions);
request.getRequestDispatcher("/WEB-INF/questions.jsp").forward(request, response);

with

<c:if test="${not empty questions}">
    <h2>There are ${fn:length(questions)} questions.</h2>

    <c:forEach items="${questions}" var="question">
         <div class="question">${question}</div>
    </c:forEach>
</c:if>

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanx that worked.But how can I create it similar to stackoverflow Ask question page ..like as user type it renders the typed data into code html etc simultaneously. – Navdroid Feb 22 '12 at 13:43
  • This magic is done by Ajax which is an entirely separate subject at its own. To get started with Servlets and Ajax, start here: http://stackoverflow.com/questions/4112686/how-to-use-servlets-and-ajax – BalusC Feb 22 '12 at 13:45