I created one web application project. It contains a servlet class and a HTML form. How do I call the servlet class from the HTML form?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- Is there a way to play audio on a mobile browser w
- HTML form is not sending $_POST values
- implementing html5 drag and drop photos with knock
For instance I create a login.html like that
Between tags I call LoginServlet by defining method as "post".
Just create a class extending
HttpServlet
and annotate it with@WebServlet
on a certain URL pattern.Or when you're still on Servlet 2.5 or older (the annotation was new since Servlet 3.0), then register the servlet as
<servlet>
inweb.xml
and map it on a certain URL pattern via<servlet-mapping>
.Then, just let the HTML link or form action point to an URL which matches the
url-pattern
of the servlet.When using submit buttons, make sure that you use
type="submit"
and nottype="button"
. Explanation on the${pageContext.request.contextPath}
part can be found in this related question and answer: How to use servlet URL pattern in HTML form action without getting HTTP 404 error.Links and forms with
method="get"
will invokedoGet()
method of the servlet. You usually use this method to preprocess a request "on page load".Forms with
method="post"
will invokedoPost()
method of the servlet. You usually use this method to postprocess a request with user-submitted form data (collect request parameters, convert and validate them, update model, invoke business action and finally render response).To learn more about servlets and to find more concrete examples, head to our Servlets wiki page. Noted should be that you can also use a JSP file instead of a plain HTML file. JSP allows you to interact with backend via EL expressions while producing HTML output, and to use taglibs like JSTL to control the flow. See also our JSP wiki page.