Java 如何从servlet发送参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1407076/
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-12 11:44:16  来源:igfitidea点击:

How to send parameters from a servlet

javajspservlets

提问by Greener

I am trying to use a RequestDispatcher to send parameters from a servlet.

我正在尝试使用 RequestDispatcher 从 servlet 发送参数。

Here is my servlet code:

这是我的 servlet 代码:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

 String station = request.getParameter("station");
 String insDate = request.getParameter("insDate");

 //test line
 String test = "/response2.jsp?myStation=5";

 RequestDispatcher rd;
 if (station.isEmpty()) {
     rd = getServletContext().getRequestDispatcher("/response1.jsp");

 } else {
     rd = getServletContext().getRequestDispatcher(test);
 }

 rd.forward(request, response);

} 

Here is my jsp, with the code to read the value - however it shows null.

这是我的jsp,带有读取值的代码 - 但是它显示为空。

    <h1>response 2</h1>
    <p>
        <%=request.getAttribute("myStation")  %>
    </p>

Thanks for any suggestions. Greener

感谢您的任何建议。更环保

采纳答案by Vineet Reynolds

In your servlet use request.setAttributein the following manner

在您的 servlet中以下列方式使用request.setAttribute

request.setAttribute("myStation", value);

where value happens to be the object you want to read later.

其中 value 恰好是您稍后要读取的对象。

and extract it later in a different servlet/jsp using request.getAttributeas

并稍后在不同的 servlet/jsp 中使用request.getAttribute作为提取它

String value = (String)request.getAttribute("myStation")

or

或者

<%= request.getAttribute("myStation")%>

Do note that the scope of usage of get/setAttribute is limited in nature - attributes are reset between requests. If you intend to store values for longer, you should use the session or application context, or better a database.

请注意 get/setAttribute 的使用范围本质上是有限的 - 属性在请求之间重置。如果您打算将值存储更长时间,您应该使用会话或应用程序上下文,或者更好的数据库。

Attributes are different from parameters, in that the client never sets attributes. Attributes are more or less used by developers to transfer state from one servlet/JSP to another. So you should use getParameter(there is no setParameter) to extract data from a request, set attributes if needed using setAttribute, forward the request internally using RequestDispatcher and extract the attributes using getAttribute.

属性不同于参数,因为客户端从不设置属性。开发人员或多或少地使用属性将状态从一个 servlet/JSP 转移到另一个。因此,您应该使用getParameter(没有 setParameter)从请求中提取数据,在需要时使用 setAttribute 设置属性,使用 RequestDispatcher 在内部转发请求并使用 getAttribute 提取属性。

回答by erickson

Use getParameter(). An attribute is set and read internally within the application.

使用getParameter()。在应用程序内部设置和读取属性。

回答by reddy

In your code, String test = "/response2.jsp?myStation=5";

在您的代码中, String test = "/response2.jsp?myStation=5";

You are adding myStation=5 as query string.As the query string parameters are stored as request parameters in Request Object.

您正在添加 myStation=5 作为查询字符串。因为查询字符串参数作为请求参数存储在请求对象中。

Therefore you can use ,

因此,您可以使用

It works fine.Thanks.

它工作正常。谢谢。