aspose file tools*
The moose likes Servlets and the fly likes function call on form action inside servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "function call on form action inside servlet" Watch "function call on form action inside servlet" New topic
Author

function call on form action inside servlet

Singh Anisha
Ranch Hand

Joined: May 09, 2012
Posts: 100

Is it possible to call a function on form action defined inside the servlet.
<form action=functioncall() >
is it legal??
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39575
    
  27
That's HTML you're posting there - how do you imagine Java objects to play a role in this? Are you trying to generate HTML that includes values returned by a Java method?


Ping & DNS - updated with new look and Ping home screen widget
Singh Anisha
Ranch Hand

Joined: May 09, 2012
Posts: 100

out.println("<form action=updatePlus() method=post>");

Want something like this.
function call defined in the same servlet that has above code.
Till the time i learn to call a web page or another servlet on action.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39575
    
  27
I see. You want something like

out.println("<form action='" + updatePlus()+ "' method=post>");

where updatePlus is the Java method that does whatever needs doing.

You should learn JSP, though, which lets you separate the controller (in the servlet) from the view (in the JSP). See http://www.javaranch.com/journal/200603/Journal200603.jsp#a5 for an overview of how that works.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60071
    
  65

Be aware that updatePlus() will be called while the servlet is running, sticking whatever it returns into the action string. This will not cause updatePlus() to be called when the form is later submitted by the user.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Singh Anisha
Ranch Hand

Joined: May 09, 2012
Posts: 100

I am getting your point Bear n its true..
so does it mean its not possible or there is some way to imply it.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60071
    
  65

No you can't call just any servlet method directly when the form is submitted. You need to set the action to the URL mapping of a servlet, and upon submit the doGet() or doPost() method of the servlet will be invoked.
shivam singhal
Ranch Hand

Joined: Jul 15, 2012
Posts: 194

why sould not you.. just call your servlet from the HTML page.. and there in your servlet you can all your function method ..
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60071
    
  65

shivam singhal wrote:just call your servlet from the HTML page.. and there in your servlet you can all your function method ..

As pointed out, you can't just "call" a servlet via a normal method call. A request must be submitted to the servlet.
shivam singhal
Ranch Hand

Joined: Jul 15, 2012
Posts: 194

alright bear,,

just redirect the control from HTML to servlet"
Singh Anisha
Ranch Hand

Joined: May 09, 2012
Posts: 100

Thanks to all for your guidance.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60071
    
  65

shivam singhal wrote:just redirect the control from HTML to servlet"

Sorry to be pedantic but that's not the right term either. A redirect is a specific thing in HTTP and submitting a form is not a redirect.
shivam singhal
Ranch Hand

Joined: Jul 15, 2012
Posts: 194

soory bear.

now i don't know..
please make me correct by writing what you want to say..

soory, but now i know am not good in English..
bharat salunkhe
Greenhorn

Joined: May 28, 2013
Posts: 25

you can call whole servlet (doGet or doPost) no need to call specific function and if you want to do so then go for Struts 2 !
or one more way is you can call that function from doGet or doPost Simple !!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: function call on form action inside servlet
 
Similar Threads
Calling servlet from JavaScript
Calling a Servlet (Urgent)
calling a servlet from javascript function??
Session in HTTPServlet
struts control problem..