简体   繁体   English

在通过RequestDispatcher调用的html页面上获取请求参数

[英]Getting request parameters on html page called through RequestDispatcher

I've been having some problem managing a homework task and got stumped looking for an answer online too. 我在管理家庭作业方面遇到了一些问题,而且在网上也难以找到答案。

I have a website built using HTML and using Javascript and jQuery to update dynamically given data from a server using Java and Jersey for servlets. 我有一个使用HTML构建的网站,使用Javascript和jQuery从服务器使用Java和Jersey为servlet动态更新给定的数据。

One of the HTML pages I have is "create_card.html", it lets the user assign values and send a post request to the servlet, which adds a new card to the database, and sends a forward request to the page "buy_card.html" with the created card's ID as a request parameter as: 我有一个HTML页面是“create_card.html”,它允许用户分配值并向servlet发送一个post请求,后者向数据库中添加一张新卡,并向页面“buy_card.html”发送转发请求。 “将创建的卡片ID作为请求参数:

RequestDispatcher dispatcher = request.getRequestDispatcher("/buy_card.html?card_id="+cardID.ToString());
dispatcher.forward(request, response);

All this works fine, and the parameter is passed correctly(I'll get back to this in a bit), but I'm having trouble finding a way to read it. 所有这一切工作正常,参数传递正确(我稍后会回到这一点),但我找不到一种方法来阅读它。

The assignment limited us to only using html pages and not jsp(On jsp it's easy to get the parameter and is how I know it's passed correctly), but on html, I have to use JS or jQuery to read it somehow, but since I'm using the request dispatcher, the url of the page doesn't change and window.location gives me the address of the previous page, so I can't access the parameters through the URL. 赋值限制我们只使用html页面而不是jsp(在jsp上它很容易获取参数,我知道它是如何正确传递的),但在html上,我必须使用JS或jQuery以某种方式读取它,但是因为我使用请求调度程序,页面的url不会改变,window.location给我上一页的地址,所以我无法通过URL访问参数。 I can't change way this works either since the assignment requires us to make the site as a Single Page Application. 由于作业要求我们将网站作为单页面应用程序,因此我无法改变其工作方式。

So I'm looking for any ideas on another way to pull the parameter from forwarded page, not using JSP. 所以我正在寻找任何想法以另一种方式从转发页面中提取参数,而不是使用JSP。

Does anyone know of a way? 有谁知道一种方式?

Thanks in advance. 提前致谢。

EDIT: So after some more digging around, I found this: How to read the post request parameters using javascript 编辑:所以经过一些更深入的挖掘后,我发现了这个: 如何使用javascript读取post请求参数

So, after a few more days of looking and finding nothing, I gave up and went for a hack. 所以,经过几天的寻找和发现之后,我放弃了,去了一个黑客。

Instead of passing parameters to my form, I instead save them as cookies sent with the response. 我没有将参数传递给我的表单,而是将它们保存为随响应一起发送的cookie。 It's ugly, but I suppose it works =/ 这很难看,但我认为它有效= /

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

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