简体   繁体   中英

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.

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:

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. 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.

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

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. It's ugly, but I suppose it works =/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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