从按钮单击调用jsp scriptlet函数 [英] jsp scriptlet function called from button click
问题描述
我正在寻找一种允许用户在文本字段中输入内容,单击按钮并根据输入内容显示一些结果的快速而肮脏的方法.如何在jsp中使用scriptlet完成此操作?谢谢.
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.
推荐答案
除了服务器正准备呈现jsp的那一刻,您不能让scriptlet(<% %>
中的Java代码段)执行. .
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:
- 将该文本字段放入具有
action
属性的表单内:<form action="myServlet">
- 在表单中放置一个
<input type='submit'>
按钮. - 在
myServlet
servlet中,检索文本字段的值(request.getParameter()
)并执行搜索.向另一个/相同的jsp发出请求,并将结果放入request属性中 - 在该jsp中显示结果.您可以使用
<% request.getAttribute(); %>
来获取它们
- 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::请考虑将小脚本视为不良实践,请使用统一表达语言.
UDPATE : Take into account that scriptlets are considered poor practice, use JSTL tags and Unified Expression Language instead.
这篇关于从按钮单击调用jsp scriptlet函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!