简体   繁体   English

将变量从 JSP 传递到 servlet

[英]Passing variables from JSP to 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.一直以来,当我在谷歌上搜索时,我得到了几十个关于从 JSP 将变量传递给 servlet 的答案,这些答案发布在 Stackoverflow 中。 But I am wondering, I don't get answer of: How to pass a variable from JSP to a servlet class? Is it possible?但我想知道,我没有得到以下答案: How to pass a variable from JSP to a servlet class? Is it possible? How to pass a variable from JSP to a servlet class? Is it possible?

Actually I am doing a simple PhoneBook application.其实我正在做一个简单的电话簿应用程序。 Here I have to send contact id to a servlet for editing and deleting.在这里,我必须将联系人 ID 发送到 servlet 以进行编辑和删除。 How can I pass this value?我怎样才能传递这个值?

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 )获取它时,结果为空。

God help me.神救救我。

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 , ie the same way as when using other technologies (ASP.NET, PHP etc).在纯 Servlets/JSP 世界中将数据传递/提交到服务器的标准方法(如从 JSP 到 servlet 的情况)是使用HTML 表单,即与使用其他技术(ASP.NET, PHP等)。 And it doesn't matter whether it is a pure HTML page or JSP page.并且它是纯 HTML 页面还是 JSP 页面都没有关系。 The recommended/most used method of submitting data from the form to the server is POST .从表单向服务器提交数据的推荐/最常用方法是POST

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).您还可以在路径之后的请求 URL 中包含的查询字符串中传递数据(当您在表单中使用GET方法而不是POST时,也会发生这种情况)。 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 )但这是针对简单的情况,例如为分页等构建 URL(您可以在此处查看使用附加查询构建 URL 的示例: Composing URL in JSP
Example of passing parameters in the URL:在 URL 中传递参数的示例:
http://example.com/foo?param1=bar&page=100

For the difference between submitting data using GET and POST methods read here:有关使用GETPOST方法提交数据之间区别,请阅读此处:

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.因此,您可以配置一些servlet来处理从 JSP 或 HTML 等发送/提交的数据。强烈建议使用POST方法提交数据,并分别使用 servlet 中的doPost()方法处理提交的数据。 You will then get the parameters passed by the client in the request by using one of the following ServletRequest methods:然后,您将使用以下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 data using Java EE is by storing data as attributes in different scopes .使用 Java EE交换数据的另一种方法是将数据存储为不同范围内的属性。 (Following is the excerpt from one of my answers on SO) (以下是我对 SO 的回答之一的摘录)

There are 4 scopes in Java EE 5 (see The Java EE 5 Tutorial: Using Scope Objects ). Java EE 5 中有4 个作用域(请参阅Java EE 5 教程:使用作用域对象)。 In Java EE 6 and in Java EE 7 there are 5 scopes (see The Java EE 6 Tutorial: Using Scopes andThe Java EE 7 Tutorial: Using Scopes ).在 Java EE 6 和 Java EE 7 中有5 个范围(请参阅Java EE 6 教程:使用范围Java EE 7 教程:使用范围)。 The most used are:最常用的是:

  • 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.此方法最常与 RequestDispatcher 结合使用。
... ...

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 ) (如果您仍想使用重定向,请阅读: 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 request object, but trying to get it back from the session .正如用户@neel 和@Sanchit 所注意到的,您正在request对象中设置一个属性,但试图从session取回它。 No wonder you are getting null in this case.难怪在这种情况下你会得到null


Hope this will help you.希望这会帮助你。

Yes Definitely !当然是 ! passing parameter from jsp to jsp case we can simply use anchor tag ( <a href="page.jsp"?=parameter> ).将参数从 jsp 传递到 jsp 的情况下,我们可以简单地使用锚标记( <a href="page.jsp"?=parameter> )。 & Then simply get that parameter into other jsp with parameter = request.getParameter("parameter"); & 然后简单地使用parameter = request.getParameter("parameter");将该参数传入其他jsp But in Jsp To Servlet Case, Protocol is as shown as in image但在 Jsp To Servlet Case 中,协议如图所示在此处输入图片说明

& in Servlet For retrieving that parameter, Again use parameter = request.getParameter("parameter"); & 在 Servlet 中检索该参数,再次使用parameter = request.getParameter("parameter");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM