• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Priety Sharma
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing Tomcat-specific about this. Moved to the JSP forum.
 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65124
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Thread hijack removed. Please ask your own questions in new topics.]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic