I am looking for a quick and dirty way to allow a user to enter something in a text field, click a button, and have some results be displayed based on what is entered. How do I accomplish this with scriptlets in a jsp? Thanks.
Asked
Active
Viewed 5,937 times
2 Answers
1
You can't make scriptlets (java code snippets in <% %>
) execute in any other moment than the moment when the server is preparing the jsp to be rendered.
In case you want to get the results server-side, you could, for instance:
- Put that textfield inside a form, with an
action
attribute:<form action="myServlet">
- Put a
<input type='submit'>
button in the form. - In the
myServlet
servlet, retrieve the textfield's value (request.getParameter()
) and perform the search. Make a request to another/the same jsp and put the results in a request attribute - Display the results in that jsp. You can get them with
<% request.getAttribute(); %>
UDPATE : Take into account that scriptlets are considered poor practice, use JSTL tags and Unified Expression Language instead.

Community
- 1
- 1

Xavi López
- 27,550
- 11
- 97
- 161
-
Yeah, scriptlets are bad practice, but I'm looking for quick and dirty. Thanks for your help. – Sam Sep 28 '11 at 15:46
0
There may be some other way to achive you goal. But it is good if you can use AJAX to get dynamic data from the serve side. If the result data is static you can go with javascript.

Satya
- 8,146
- 9
- 38
- 43