-1

I'm beginner to java and i read a lot of article about posting data into java class , using Servlet ,or on click method. (read this article)

MY CODE

<form action="test/login" method="post" role="form">
     <input type="text" class="form-control" name="username" id="username" placeholder="username" autocomplete="off" />
     <input type="password" class="form-control" name="password" id="password" placeholder="password" autocomplete="off" />
     <input type="submit" name="btnLogin" value="Login" />
</form>

JAVA CLASS

public class test{
     public function login(username,password){
          //do something.
     }
}

but any better way to call .java class method after form submission?

is it possible? or the only way is Servlet ?

Servlet vs Ajax call, which act better?

msDead
  • 35
  • 10

1 Answers1

0

You can't really choose between Servlet and Ajax, because Ajax will interact with a servlet. It's either Ajax and Servlet or plain html form and Servlet.

If you just want to submit the form, then point to the servlet in the "action" property of the form, and handle the username/password from there without using ajax. Read more from BalusC answer here:

How to transfer data from JSP to servlet when submitting HTML form

It really comes down to "process the data without reloading the page" in which case you'd want to use Ajax, or "submit the form and reload the page" and you can do that using the action property of the form. Read more from BalusC answer here:

How to use Servlets and Ajax?

Either way, you will be using a Servlet POST method.

Richard
  • 47
  • 7