aspose file tools*
The moose likes JSP and the fly likes Get and Post on the same JSP page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Get and Post on the same JSP page" Watch "Get and Post on the same JSP page" New topic
Author

Get and Post on the same JSP page

Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
Hello and thanks for reading my post.

When I display a JSP page, I would like to display some values pulled from the DB. Right now, all I have is a form tag with method="post" for when the user submits data. Can I do a "get" first before doing a "post" on the same JSP? Would I need to have two form tags? A form tag with the method="get" for the data (from DB) and then the second form with the method="post" (to submit data to DB)? Is this the only way to do this?

Thanks!
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29241
    
139

Shannon,
While you can certainly have two different forms on a page, they wouldn't be related. And only one would get called per request.

There are a few things you could be trying to accomplish here:
1) Display some values that get used to render the "update form". Then the user chooses values and submits causing the update. - this is really two requests. A get to paint the page. And a post to update
2) Update the database and then display some values to the user. - there is a pattern called post - redirect - get for this. It lets you do the two in sequence.
3) Something else - please elaborate


[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
Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
Hi Jeanne, thanks for your reply!

Oh right, only one request at a time; I was thinking that the submit would make the second "post" call.

This is what I want to do. When the page first displays, check the database for data and render it in a table (above the user input fields). The table will allow them to see what is already in the database; this way, the user does not enter data that already exist.

data from database
I want to render what is in the database the first time the page loads.
<form> data from user input </form> submit the user input and refresh the JSP page with the user input.

Hm, I guess I could do a "get" first and then have the doGet method set a string parameter on the request of "post" to dynamically update the "method" on the form (to flip between the two different methods). This should work?

Thanks!
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29241
    
139

You don't need to share a form to do that.

1) user links to or types url /lookAtTheForm - this triggers a get (urls are gets) to grab the data and paint your JSP
2) user enters data and submits form - this is your actual form tag which you set to a post
Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
Odd, that's what I thought, but I never see doGet() get executed (print statement inside the doGet never shows up in server logs).


Well, I guess I have another problem then. Any idea where I should start looking for the problem?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29241
    
139

Try to trace it through:
You start with a URL. How do you know which servlet it is going to? (hint: look at the web.xml servlet mapping).

You can post code snippets here of that and the url if you aren't sure.
Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
Hi Jeanne, thanks again for all your help; very much appreciated!

I figured out what my issue was. I would have never discovered the issue had I not tried accessing the JSP outside of Eclipse. When I was viewing my JSP, I would right click on the JSP and then clicked on "run on server". When I did this, I never saw any print statements in the server log. Luckily (and finally), I decided to try the browser and when I did, BAM, there it was, my print statement. Eclipse is great, but sometimes too convient and can cause many headaches. I spent so many hours doubting myself and getting frustrated only to find out that, accessing your JSP using the "run on server" will drive you crazy.

Well, I hope no one does this too.

Thanks!
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29241
    
139

I'm glad you got it working. Thanks for sharing the problem for the next person who encounters this!
 
 
subject: Get and Post on the same JSP page
 
Similar Threads
Listener Class and Regular Java
how is the flow in this JSP
jsp and action class
submitting page to itself
Displaying a datagrid on a JSP