Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

function call on form action inside servlet

 
Singh Anisha
Ranch Hand
Posts: 100
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to call a function on form action defined inside the servlet.
<form action=functioncall() >
is it legal??
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Singh Anisha
Ranch Hand
Posts: 100
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Singh Anisha
Ranch Hand
Posts: 100
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 231
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 231
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
alright bear,,

just redirect the control from HTML to servlet"
 
Singh Anisha
Ranch Hand
Posts: 100
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all for your guidance.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 231
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic