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?
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
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?
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
Joined: Jul 03, 2003
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?
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.
Joined: Jul 03, 2003
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.