i have a form, on which after i click submit, i want the existing elements to get cleared off , ie, all those form fields and button to be cleared, and the result i wanna display be displayed

kind of like clrscr() in c++. is there anything in JSP or HTML for that?

i want to do it on the same page only. that's why i'm asking and so please don't tell to print the new stuff on a new page

<html>
<head>
<title>parameter example</title>
</head>


<body>
<form action="pex.jsp" method=POST>
<table align=center border =1 >

<tr>

<td>param1

<td><input type=text name=p1 />
</tr>

<tr>
<td>param2
<td><input type=text name=p2 />
</tr>
<tr><td colspan=2><input type=submit value="submit" /></td></tr>

</table>

</form>
</body>
</html>

pex.jsp

<%@ page language="Java" %>
<%! String p1,p2; %>

<% p1=request.getParameter("p1");
p2=request.getParameter("p2");
if(name==null)
    name=" ";
%>

<html>
    
    <body>

hello <%= p1+" "+p2%>

        
    </body>
</html>

Recommended Answers

All 3 Replies

Submit to the same page and add the code of the second page to the first. When the first page loads for the 1st time, the request is empty, it has null, inside. So you can use that in order to decide what to display. Even if you submit the page with empty fields the request will have those parameter names but they will be empty, not null. When something is null it means that it hasn't been sent and in your case, not submited:

When the action of the <form> tag is set to empty, it submits to the same page.
Also I made a few changes to the way you write the html tags. You forgot to close the <td> in almost every time you used it.

<html>
<head>
<title>parameter example</title>
</head>

<body>

<%
String p1 = request.getParameter("p1");
String p2 = request.getParameter("p2");
%>

<form action="" method="POST">
<table align="center" border="1" >

<%
if (p1!=null) { // open if
%>
<tr><td colspan="2">
Hello <%= p1+" "+p2 %>
</td></tr>
<%
} // close if
%>

<tr>
<td>param1: </td>
<td><input type="text" name="p1" /></td>
</tr>

<tr>
<td>param2</td>
<td><input type="text" name="p2" /></td>
</tr>

<tr><td colspan="2"><input type="submit" value="submit" /></td></tr>

</table>

</form>
</body>
</html>

Try to run the above and then submit it.
The first time the p1,p2 will be null, so the if will not execute, and what is inside it will not be displayed

i know how to do that. plus, i always code the handler jsp in a different file, for the purpose of modularity. i dunno any other way.
i can code in a single page, but it looks like spaghetti to me.
so,that's not what i'm asking.

what i'm asking is, clear off all the tables and buttons etc, and freshly display the new information, on the same page. In that case will writing all the code on the same page be useful for me

in this case, the Hello MrX Y appears with the table. Obviously you missed the fact that i wanted to clear away the table.

so please see if that is possible. again i emphasize, anything analogous to the C++'s clrscr() function.

There isn't such mehod. And if your problem was that the "Hello" appears in the table then use the if statement to display it outside. If you don't want to display the table then add an else statement.
If you want the same page then you need to submit to the same page. There isn't a method that clears the page. When you submit, the page where you submit to, gets executed and displays whatever you want it to display.
Using my example you can easily modify it to do what you want.
If you don't want to submit to a different page, then you will have to submit to the same. Take what is in the request and display whatever you want using if-else statements.
Remember if what is inside the request is null, means that you went to page without submitting, so display the table, else display the "Hello".

Another way to do what you want is with javascript. But even if the page does what you want, it would be done without submitting the information and I don't see where is the point in doing that.
Check the javascript API at this site as well as the DOM API and try something like this:
http://www.w3schools.com/
http://www.w3schools.com/jsref/default.asp

<html>
<head>
  <script>
    function callMethod() {
      // take the values of the fields with ids p1, p2
      // use document.getElementById("p1") in combination with the DOM Input Text

      // hide the div with id: "table_form". Use the DOM Style

      // dislpay whatever you want in the div "result" :
      document.getElementById("result").innerHTML = "Hello "+<value of p1>+" "+<value of p2>;
    }
  <script>
</head>

<body>

<div id="result">  
</div>

<div id="table_form">  
<table>
<tr>
<td>param1: </td>
<td><input type="text" name="p1" id="p1" /></td>
</tr>

<tr>
<td>param2</td>
<td><input type="text" name="p2" id="p2" /></td>
</tr>

<tr><td colspan="2"><input type="button" value="submit" onclick="callMethod();" /></td></tr>

</table>
</div>

</body>

</html>

But again all the above does is hide - display what you enter at the fields. It doesn't do anything else. Don't you want eventually to submit the data in order to use them?
Also, you can add another button called "show" and execute javascript that makes the "table_form" visible and clears what is inside the "result" div. You can set its style to hidden at first and make it visible in the method I posted.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.