Redirect form input from a servlet to another form
I have a form created by a servlet which is supposed to fill out a text input field from a search. So the form is like this :
<form name="form1">
<input type="text" readonly="true" value="" /> <button href="/se开发者_开发知识库archServlet />
</form>
When the button is clicked, it opens up another form , which does the search and displays the results as list from which the user can pick any result. The picked result id populates some hidden fields in the form. Now do I get those hidden fields data in "form1"? Is there some Javascript for that purpose? Or can I redirect input from one servlet to another?
JavaScript is indeed your friend here. Give the input a name as well and let the button open a dialog by window.open()
.
<form name="form1">
<input type="text" name="text1" readonly="true" />
<button onclick="window.open('/searchServlet');">search</button>
</form>
Then in the HTML generated by /searchServlet
and its JSP in the dialog, you can use window.opener
to get the parent window and then from there do the usual document
stuff as if you're sitting in the parent window. Here's a basic kickoff example:
<button onclick="window.opener.document.form1.text1.value='1'; window.close();">1</button>
<button onclick="window.opener.document.form1.text1.value='2'; window.close();">2</button>
<button onclick="window.opener.document.form1.text1.value='3'; window.close();">3</button>
Of course you'd like to abstract/refactor the verbosity/duplication away in some function.
精彩评论