This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes Forwarding a request from a jsp to servlet or an html page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Forwarding a request from a jsp to servlet or an html page" Watch "Forwarding a request from a jsp to servlet or an html page" New topic
Author

Forwarding a request from a jsp to servlet or an html page

Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

I have a jsp page with some text boxes.
I have 2 buttons on the jsp.
ADD and CANCEL.

What I want to do here is:
On clicking on the ADD button the request should be forwarded to a servlet.
and
On clicking on the CANCEL button I want direct the user to a html page.

Earlier both ADD and CANCEL were taking the user to html pages.
So javascript was setting the action attribute of the form and it was fine.

Now how do I get this done?
I want to use jsp:forward for taking the user to the servlet
and
javascript for taking him to the html.
Is it possible?


Priety.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12760
    
    5
I want to use jsp:forward for taking the user to the servlet
and
javascript for taking him to the html.


You seem to be mixing up client side (JavaScript) and server side (jsp:forward).

I know it is the current idiom to say "buttons on the jsp" but for clarity of thought you should say two buttons on the HTML page generated by the jsp. The action for one button will be a request to a jsp URL and the other a request to a servlet URL.
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi William,
I got what you saying.

I will phrase it that way.

Now what I am thinking is:
Write a function in the jsp which will have the forward action for the ADD button.

I could set a variable with a value of 1 or 0 depending on the button clicked using javascript.

If the value is 1 call the jsp function through javascript and if its zero set the action value to the desired html page in the script.
Is this possible?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Nothing Tomcat-specific about this. Moved to the JSP forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
leo donahue
Ranch Hand

Joined: Apr 17, 2003
Posts: 327
What you are also not indicating is:

Are you using a web framework, such as JSF?
Are you using JSP scripting elements?
Do you just want to use HTML, maybe?

What I want to do here is:
On clicking on the ADD button the request should be forwarded to a servlet.
and
On clicking on the CANCEL button I want direct the user to a html page.

Earlier both ADD and CANCEL were taking the user to html pages.
So javascript was setting the action attribute of the form and it was fine.


If all you are doing is "forwarding" to another page, then did you know that you can achieve this with HTML button click events? Are you actually submitting a form for the Servlet to process, or are you just wanting to navigate to one page or another based on the button clicked?


Thanks, leo
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

Priety Sharma wrote:Now what I am thinking is:
Write a function in the jsp which will have the forward action for the ADD button.

You are thinking incorrectly. Once the page has made its way to the browser, it's just an HTML page (as William has already indicated) generated from the JSP. The JSP itself is long gone.

You need to handle button events with JavaScript. Period. No JSP. No Java. They no longer exist.

Please read this article to understand how JSP operates.

Is this possible?
No. See above.
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi,

Leo I will be forwarding this information to a servlet from where it will go to the database and then another jsp/html page will show the results of the data stored.

Bear so from what you say a jsp forward should essential be used when the request is being forwarded from one jsp to another before sending the response to the client.

So when I am submitting this form there is no question of forwarding.
Looks like my confusing is clearing.

Thanks to both of you.
Also I will go through the article.

Bear thanks for moving it to JSP forum, I wrongly posted it here.
swapnil kachave
Greenhorn

Joined: Feb 10, 2010
Posts: 27
create ADD and CANCEL as Anchor tag and in anchor tag give the path of Servlet and HTML page it will redirect you properly..
Oscar Calderon
Ranch Hand

Joined: Nov 27, 2009
Posts: 32
[Thread hijack removed. Please ask your own questions in new topics.]


Wanna grow? Learn, live, love people and enjoy life
 
Consider Paul's rocket mass heater.
 
subject: Forwarding a request from a jsp to servlet or an html page
 
Similar Threads
How to show a Progress Bar while operation is being executed
Html contents displayed by servlet
Need Help and Suggestion
quick question
IE doesn't retain form fields