将变量从JSP传递给servlet [英] Passing variables from JSP to servlet

查看:222
本文介绍了将变量从JSP传递给servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直以来,当我在Google上搜索时,我收到了一些在Stackoverflow中发布的关于从JSP传递变量到servlet的答案。
但是我想知道,我没有得到答案:如何将变量从JSP传递给servlet类?有可能吗?

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?

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

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


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

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.

上帝帮助我。

推荐答案

在纯Servlets / JSP世界中向服务器传递/提交数据的标准方法 (在您的情况下,从JSP到servlet)是使用 HTML表单 ,即与使用其他技术(ASP.NET,PHP等)时相同的方式。无论是纯HTML页面还是JSP页面都无关紧要。从表单向服务器提交数据的推荐/最常用方法是 POST

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.

您还可以在路径之后传递请求网址中包含的查询字符串中的数据(这也发生在代替 POST 您在表单中使用 GET 方法)。但这适用于简单的情况,例如为分页等构建URL(您可以在此处看到使用其他查询构建URL的示例:在JSP中编写URL

在URL中传递参数的示例:

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

You also can pass data in the query string that is contained in the request URL after the path (this also happens when instead of POST you use GET method 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

对于使用 GET POST <提交数据之间的差异/ em>方法在这里阅读:

For the difference between submitting data using GET and POST methods read here:

在HTML表单中,使用GET方法

POST之间有什么区别?

因此,您可以配置一些 servlet 进行处理从JSP或HTML等发送/提交的数据。
强烈建议使用 POST 方法提交数据,并分别使用 < servlet中的code> doPost()
方法。
然后,您将使用以下 ServletRequest 方法:

So you can configure some servlet to process data sent/submitted from a JSP or HTML etc. It is highly recommended to submit data using POST method 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 ServletRequest methods:

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

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

以上教程来自以下课程:

用Java构建Web应用程序:
Beginning&中级Servlet& JSP教程

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

使用Java EE交换数据的另一种方式是通过存储数据作为不同范围中的属性。 (以下是我在SO上的一个答案的摘录)

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

Java EE 5中有 4个范围(参见 Java EE 5教程:使用范围对象)。在Java EE 6和Java EE 7中,有 5个范围(参见 Java EE 6教程:使用范围 Java EE 7教程:使用范围)。最常用的是:

There are 4 scopes in 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 Scopes and The Java EE 7 Tutorial: Using Scopes). The most used are:


  • 请求范围

  • 会话范围

  • 应用程序范围(Web上下文)

  • Request scope
  • Session scope
  • Application scope (Web Context)

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

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

以下是与 ServletRequest。关于请求范围的setAttribute(String,Object)方法:

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


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

在此请求中存储属性。 属性在
请求
之间重置。此方法通常与
RequestDispatcher一起使用。

...

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

所以每个新请求都会丢失您在请求中设置的先前属性。在请求中设置属性后,必须将请求转发到所需页面。如果您重定向,这将是一个完全新的请求,因此先前设置的属性将丢失。 (如果您仍想使用重定向,请阅读以下内容:将Servlet重定向到同一页面并显示错误消息

So with every new request the previous attributes you have set in request will be lost. After you have set an attribute in a request, you must forward the 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)

HttpSession (在会话范围中)只要会话存在就会生效,当然,只有会话所属的用户才能使用。

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.

对于上下文属性,它们可用于整个Web应用程序(应用程序范围),对于所有用户,以及他们的生活只要网络应用程序存在。

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.

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

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

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

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


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

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.

正如用户@neel和@Sanchit注意到的那样,你在请求中设置一个属性 object,但是试图从会话中取回它。难怪你在这种情况下得到 null

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

希望这对你有帮助。

这篇关于将变量从JSP传递给servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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