• Post Reply Bookmark Topic Watch Topic
  • New Topic

how can I call another servlet from an action inside my servlet

 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a servlet class in which I have an export button.When I click on the export button it should call another servlet which would create a report of the data from the database.I am not sure how to call the new Servlet through the submit button action inside my servlet.Can some one please help me?



On click of Export Data button.The request should be passed to the exportdbservlet which would create the report.At the moment I cannot call the exportdbservlet.The code of the new servlet is pasted below:



Please can some one advice on this issue?

Thanks
 
ujjwal soni
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

As you said you want to call a servlet, then you need to create a new servlet, describe it in web.xml and call the servlet URL in form post parameter.

See for example i have one main servlet called TestOne



Now, i want to call a new servlet called TestTwo so, i would define this in web.xml something like this,



Create a class called TestOne.java and TestTwo.java

Now, on my jsp page called as testone.jsp



will call the second servlet

 
Milan Jagatiya
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use RequestDispatcher.

http://java.sun.com/products/servlet/2.2/javadoc/
 
ujjwal soni
Ranch Hand
Posts: 405
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out below example

in the doGet method compare this parameter and call the apropiate method inside the forwarded servlet, something like this:

Hope that can be helpful,

 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply!

How can I call this on the submit inside my existing servlet? What should I write for submit button?



Please can you explain how can I call your code from the above submit button?

Thanks
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What? You don't call the form submit from within the Java that creates the HTML that emits the form submit button.

The *user* calls the servlet (or whatever) when they click the submit button. You might want to find a basic tutorial on how web applications work to clear up some misunderstandings regarding the client and server sides and how they interact.
 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I use RequestForwarding or RequestDispatching on click of the submit button? If yes,how can I forward the existing omc object to the new servlet which will print the html?

Thanks
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...

We're not getting anywhere here.

1) User clicks "submit" button--this makes a request to a servlet.
2) Servlet runs, processes info from the DB, and returns something.

That's all there is to it.

Here's a more detailed sequence of events:

1) User requests the servlet that generates the HTML you've shown.
2) That servlet returns a bunch of HTML including a submit button.
3) The browser renders that HTML, creating a form on a web page.
4) User sees the form, clicks the "submit" button.
5) That makes a request to another servlet
6) That other servlet reads stuff from the DB and either renders its own HTML (IMO not a good idea) or forwards to a JSP that renders HTML.

I'm not sure what else to tell you.
 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I resolved this issue by using request dispatch to a jsp.It works now for me.

Thanks for the help provided!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!