Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing parameters on URL query string

 
andy kra
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34372
345
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
andy kra
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Your message is well received - thanks again, Bear!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic