调用java方法时如何设置请求参数值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/943387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:21:53  来源:igfitidea点击:

How do set request parameter value while calling java method?

java

提问by

I need to call a method of java class from jsp page.. While calling that in jsp page, need to set some request parameters also. How do i do that in jsp page?

我需要从jsp 页面调用java 类的一个方法。在jsp 页面中调用它时,还需要设置一些请求参数。我如何在jsp页面中做到这一点?

Ex :

前任 :

Java class :

Java类:

 public void execute() {
     string msg = request.getParameter("text");
 }

JSP file :

JSP文件:

 I need to call the method here and also need to set parameter values (eg : &text=hello)

Please help me...

请帮我...

回答by Fernando Miguélez

Just embed your Java code inside the JSP. The object "request" is available to get parameters (parameters can't be set). Unless you plan to forward to another JSP in another context with an attribute should be enough.

只需将您的 Java 代码嵌入 JSP 中即可。对象“请求”可用于获取参数(无法设置参数)。除非您计划将一个属性转发到另一个上下文中的另一个 JSP 就足够了。

To refer to your own class do not forget to add the import statement at the beginning of the JSP (similiar to an import in a Java class).

要引用您自己的类,请不要忘记在 JSP 的开头添加 import 语句(类似于 Java 类中的导入)。

<!-- JSP starts here -->
<%@page import="test.MyClass" %>

<%
MyClass myClass = new MyClass();
String text=myClass.execute();

// This is not neccessary for this JSP since text variable is already available locally.
// Use it to forward to another page.
request.setAttribute("text");
%>

<p>
This is the <%=text%>
</p>

回答by Jon Skeet

You can't set request parameters - they're meant to have come from the client.

您无法设置请求参数 - 它们应该来自客户端。

If you need to give the method some information, you should either use normal method parameters, or some other state. The request parameters are not appropriate for this, IMO.

如果你需要给方法一些信息,你应该使用普通的方法参数,或者其他一些状态。请求参数不适用于此,IMO。

回答by Vini

You cannot set request parameters. I suggest you try using the attributes as suggested in the previous answers.

您不能设置请求参数。我建议您尝试使用之前答案中建议的属性。