从按钮单击调用jsp scriptlet函数 [英] jsp scriptlet function called from button click

查看:72
本文介绍了从按钮单击调用jsp scriptlet函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种允许用户在文本字段中输入内容,单击按钮并根据输入内容显示一些结果的快速而肮脏的方法.如何在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:

  1. 将该文本字段放入具有action属性的表单内:<form action="myServlet">
  2. 在表单中放置一个<input type='submit'>按钮.
  3. myServlet servlet中,检索文本字段的值(request.getParameter())并执行搜索.向另一个/相同的jsp发出请求,并将结果放入request属性中
  4. 在该jsp中显示结果.您可以使用<% request.getAttribute(); %>
  5. 来获取它们
  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::请考虑将小脚本视为不良实践,请使用

UDPATE : Take into account that scriptlets are considered poor practice, use JSTL tags and Unified Expression Language instead.

这篇关于从按钮单击调用jsp scriptlet函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆