aspose file tools*
The moose likes JSP and the fly likes Passing parameters on URL query string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Passing parameters on URL query string" Watch "Passing parameters on URL query string" New topic
Author

Passing parameters on URL query string

andy kra
Greenhorn

Joined: Jun 21, 2012
Posts: 4
Hi All,

Long time, first time.

I have a simple JSP question, and you will have to forgive me because I know this has been answered on these forums before, but I can't seem to get them to work for my application.

I have a series of check boxes, text boxes, and radio buttons in a form. What I'd like for is when the user submits the form on one "test.jsp" page, these parameters are added to the URL query string and are present on the submitted page "result.jsp", such as:

http://localhost/MyApp/result.jsp?param1=a?param2=b

Is anyone able to provide a sample code snippet for what the "test.jsp" page should look like for this?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31054
    
232

Andy,
Welcome to CodeRanch!

What do you have so far? A form?
A form with a checkbox?
A form with a checkbox with a name and value?

Show what you have and then people will make comments/give you tips. You'll learn more and people will know you tried.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
andy kra
Greenhorn

Joined: Jun 21, 2012
Posts: 4
Thanks, Jeanne.

Here's my form:



The onLoad function: <body onload="javascript:generateData()"> is just calling a function that randomizes the values for the check boxes, text boxes, and combo box.

Can I write a JS function that builds the URL parameters, then just append it to the action="result.jsp" when submitted? I would consider this a dirty solution, but what would the action="" look like if you want to call a JS function that does this? Can you submit a form within JS?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

A method of post will cause the request parameters to be sent as the post body. A method of "get" will cause them to be passed on the URL.

Why does it matter to you?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18987
    
    8

Andy, remember that a JSP is just a tool for generating HTML. And that HTML can include Javascript. So you should think of your question as being "How can I write HTML to do (whatever)?". Once you have answered that question, then write your JSP so that it generates that HTML.

Given that, it appears that your question is actually about HTML and Javascript. We have a forum specifically for those topics, so would you like a moderator to move your post there? Or have I misunderstood, and is your question really about how to write a JSP to generate that HTML code?
andy kra
Greenhorn

Joined: Jun 21, 2012
Posts: 4
Bear,

Thanks! Using the get method showed exactly what I needed .. then I could just use <% request.getParameter("name") %> to build my result.jsp page with these parameters. Doing it this way matters to me only for learning, which I hope is not an issue here?

Paul,

Thanks for the advice. I'll mark this one resolved.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

andy kra wrote:Using the get method showed exactly what I needed .. then I could just use <% request.getParameter("name") %> to build my result.jsp page with these parameters.

Three important points here:
  • Regardless of whether you use get or post, and regardless of whether the parameters are passed on the URL or in the post body, you fetch the parameters the same way.
  • Constructs such as <% request.getParameter("name") %> in a JSP are an extremely poor practice that has been discredited for over 10 years now. Whatever you are using as a JSP tutorial is very out of date if it is not directing you to use the JSTL and EL instead of Java scriptlets.
  • Submitting a form to a JSP page is another no-no. Always submit data for processing to a servlet. A JSP is for view generation only.

  • Doing it this way matters to me only for learning, which I hope is not an issue here?

    If you are just learning, you are definitely heading down the wrong path, using antiquated and discredited techniques. Modern JSPs should contain no Java code.

    First, read this JspFaq entry.

    Then I always suggest that newcomers to JSP read the following articles:
  • The Secret Life of JSPs
  • The Front Man

  • andy kra
    Greenhorn

    Joined: Jun 21, 2012
    Posts: 4
    Your message is well received - thanks again, Bear!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Passing parameters on URL query string