jsp scriptlet function called from button click

你离开我真会死。 提交于 2020-01-04 05:22:08

问题


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.


回答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:

  1. Put that textfield inside a form, with an action attribute: <form action="myServlet">
  2. Put a <input type='submit'> button in the form.
  3. 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
  4. 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.




回答2:


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.



来源:https://stackoverflow.com/questions/7585472/jsp-scriptlet-function-called-from-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!