java 将变量从 JSP 传递到 servlet

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

Passing variables from JSP to servlet

javajspservletshttprequest

提问by Sanjaya Pandey

All the time, when I searched on Google, I got dozen of answers which are posted in Stackoverflow about passing variables to servlet from JSP. But I am wondering, I don't get answer of: How to pass a variable from JSP to a servlet class? Is it possible?

一直以来,当我在 Google 上搜索时,我得到了很多关于从 JSP 将变量传递给 servlet 的答案,这些答案发布在 Stackoverflow 中。但我想知道,我没有得到以下答案:How to pass a variable from JSP to a servlet class? Is it possible?

Actually I am doing a simple PhoneBookapplication. Here I have to send contact id to a servlet for editing and deleting. How can I pass this value?

其实我正在做一个简单的电话簿应用程序。在这里,我必须将联系人 ID 发送到 servlet 以进行编辑和删除。我怎样才能传递这个值?

I know, we can pass variable from servlet to JSP by using request.setAttribute(key, value)But when I used it to set variable in JSP and again get it by using session.getAttribute(key )then result is null.

我知道,我们可以通过使用将变量从 servlet 传递到 JSPrequest.setAttribute(key, value)但是当我使用它在 JSP 中设置变量并再次使用它来获取它时,session.getAttribute(key )结果为空。

God help me.

神救救我。

回答by informatik01

The standard way of passing/submitting data to the server in the pure Servlets/JSP world(as in your case from the JSP to the servlet) is by using HTML form, i.e. the same way as when using other technologies (ASP.NET, PHP etc). And it doesn't matter whether it is a pure HTML page or JSP page. The recommended/most used method of submitting data from the form to the server is POST.

在纯 Servlets/JSP 世界中将数据传递/提交到服务器的标准方法(如从 JSP 到 servlet 的情况)是使用HTML 表单,即与使用其他技术(ASP.NET, PHP 等)。并且它是纯 HTML 页面还是 JSP 页面都没有关系。从表单向服务器提交数据的推荐/最常用方法是POST

You also can pass data in the query string that is contained in the request URLafter the path (this also happens when instead of POSTyou use GETmethod in the form). But that is for the simple cases, like constructing URLs for the pagination etc (you can see the example of constructing URLs with the additional queries here: Composing URL in JSP)
Example of passing parameters in the URL:
http://example.com/foo?param1=bar&page=100

您还可以在路径之后的请求 URL 中包含的查询字符串中传递数据(当您在表单中使用GET方法而不是POST时,也会发生这种情况)。不过,这是简单的情况下,如构建网址分页等(你可以看到与其他查询这里构建的URL的例子:创作URL在JSP), 例如,在URL参数传递:

http://example.com/foo?param1=bar&page=100

For the difference between submitting data using GETand POSTmethodsread here:

有关使用GETPOST方法提交数据之间区别,请阅读此处:

So you can configure some servletto process data sent/submitted from a JSP or HTML etc. It is highly recommended to submit data using POSTmethod and respectively to process the submitted data using the doPost()method in your servlet. You will then get the parameters passed by the client in the request by using one of the following ServletRequestmethods:

因此,您可以配置一些servlet来处理从 JSP 或 HTML 等发送/提交的数据。强烈建议使用POST方法提交数据,并分别使用doPost()servlet 中的方法处理提交的数据。然后,您将使用以下ServletRequest方法之一获取客户端在请求中传递的参数:

Here is a nice tutorial with examples: Handling the Client Request: Form Data

这是一个很好的示例教程:处理客户端请求:表单数据

The above tutorial is from the following course:
Building Web Apps in Java: Beginning & Intermediate Servlet & JSP Tutorials

上述教程来自以下课程:
用 Java 构建 Web 应用程序:初级和中级 Servlet 和 JSP 教程



Another way of exchanging datausing Java EE is by storing data as attributes in different scopes. (Following is the excerpt from one of my answers on SO)

使用 Java EE交换数据的另一种方法是将数据存储为不同范围内的属性。(以下是我对 SO 的回答之一的摘录)

There are 4 scopesin Java EE 5 (see The Java EE 5 Tutorial: Using Scope Objects). In Java EE 6 and in Java EE 7 there are 5 scopes(see The Java EE 6 Tutorial: Using Scopesand The Java EE 7 Tutorial: Using Scopes). The most used are:

Java EE 5 中有4 个作用域(请参阅Java EE 5 教程:使用作用域对象)。在 Java EE 6 和 Java EE 7 中有5 个作用域(请参阅Java EE 6 教程:使用作用域Java EE 7 教程:使用作用域)。最常用的是:

  • Request scope
  • Session scope
  • Application scope(Web Context)
  • 请求范围
  • 会话范围
  • 应用范围(Web Context)

You can store some data in all the above scopes by setting the appropriate attribute.

您可以通过设置适当的属性在上述所有范围内存储一些数据。

Here is a quote from the Java EE API docs related to ServletRequest.setAttribute(String, Object)method in regard to request scope:

这是 Java EE API 文档中与ServletRequest.setAttribute(String, Object)方法相关的请求范围的引用:

void setAttribute(java.lang.String name,
                  java.lang.Object o)

Stores an attribute in this request. Attributes are reset between requests. This method is most often used in conjunction with RequestDispatcher.
...

void setAttribute(java.lang.String name,
                  java.lang.Object o)

在此请求中存储一个属性。属性在请求之间重置。此方法最常与 RequestDispatcher 结合使用。
...

So with every new request the previous attributes you have set in requestwill be lost. After you have set an attribute in a request, you must forwardthe request to the desired page. If you redirect, this will be a totally NEW request, thus the attributes previously set will be lost. (If you still want use redirection read this: Servlet Redirection to same page with error message)

因此,对于每个新请求,您在请求中设置的先前属性都将丢失。在请求中设置属性后,您必须将请求转发到所需页面。如果重定向,这将是一个全新的请求,因此之前设置的属性将丢失。(如果您仍然想使用重定向,请阅读:Servlet Redirection to same page with error message

Those attributes that are set in a HttpSession(in the session scope) will live as long as the session lives and, of course, will be available to only the user to which session belongs.

HttpSession中设置的那些属性(在会话范围内)将在会话存在期间一直存在,当然,仅对会话所属的用户可用。

As for the context attributes they are meant to be available to the whole web application (application scope) and for ALL users, plus they live as long as the web application lives.

至于上下文属性,它们旨在对整个 Web 应用程序(应用程序范围)和所有用户可用,而且只要 Web 应用程序存在,它们就存在。

Also maybe this article will be useful for you as well: How Java EE 6 Scopes Affect User Interactions

也许这篇文章也对你有用:Java EE 6 Scopes 如何影响用户交互



Also pay attention to the following issue. You wrote (quote):

还要注意以下问题。你写道(引用):

I know , We can pass variable from servlet to jsp by using request.setAttribute(key , value) But when I used it to set variable in jsp and again get it by using session.getAttribute(key ) then result is null.

我知道,我们可以使用 request.setAttribute(key , value) 将变量从 servlet 传递到 jsp 但是当我使用它在 jsp 中设置变量并再次使用 session.getAttribute(key) 获取它时,结果为空。

As the users @neel and @Sanchit have noticed, you are setting an attribute in the requestobject, but trying to get it back from the session. No wonder you are getting nullin this case.

正如用户@neel 和@Sanchit 所注意到的,您正在request对象中设置一个属性,但试图从session. 难怪你会遇到null这种情况。



Hope this will help you.

希望这会帮助你。