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?
我创建了一个Web应用程序项目。它包含一个servlet类和一个HTML表单。如何从HTML表单中调用servlet类?
2 个解决方案
#1
25
Just create a class extending HttpServlet
and annotate it with @WebServlet
on a certain URL pattern.
只需创建一个扩展HttpServlet的类,并在某个URL模式上使用@WebServlet对其进行注释。
@WebServlet("/login")
public class LoginServlet extends HttpServlet {}
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>
in web.xml
and map it on a certain URL pattern via <servlet-mapping>
.
或者当你仍然使用Servlet 2.5或更早版本时(注释是自Servlet 3.0以来的新版本),然后在web.xml中将servlet注册为
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.example.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
Then, just let the HTML link or form action point to an URL which matches the url-pattern
of the servlet.
然后,只需让HTML链接或表单操作指向与servlet的url-pattern匹配的URL。
<a href="${pageContext.request.contextPath}/login">Login</a>
<form action="${pageContext.request.contextPath}/login" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit">
</form>
When using submit buttons, make sure that you use type="submit"
and not type="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.
使用提交按钮时,请确保使用type =“submit”而不是type =“button”。可以在此相关问题和答案中找到有关$ {pageContext.request.contextPath}部分的说明:如何在HTML表单操作中使用servlet URL模式,而不会收到HTTP 404错误。
Links and forms with method="get"
will invoke doGet()
method of the servlet. You usually use this method to preprocess a request "on page load".
使用method =“get”的链接和表单将调用servlet的doGet()方法。您通常使用此方法预处理“页面加载”请求。
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ...
}
Forms with method="post"
will invoke doPost()
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).
使用method =“post”的表单将调用servlet的doPost()方法。您通常使用此方法使用用户提交的表单数据对请求进行后处理(收集请求参数,转换并验证它们,更新模型,调用业务操作并最终呈现响应)。
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ...
}
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.
要了解有关servlet的更多信息并找到更具体的示例,请访问我们的Servlets维基页面。值得注意的是,您也可以使用JSP文件而不是纯HTML文件。 JSP允许您在生成HTML输出时通过EL表达式与后端进行交互,并使用像JSTL这样的taglib来控制流。另请参阅我们的JSP wiki页面。
#2
0
For instance I create a login.html like that
例如,我创建了一个这样的login.html
<div class="container">
<form method = "post" class="form-signin" role="form" action="LoginServlet">
<h2 class="form-signin-heading">Please sign in</h2>
<input type="text" class="form-control" name = "username" placeholder="User Name" required autofocus>
<input type="password" class="form-control" name = "password" placeholder="Password" required>
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<input type="submit" class="btn btn-lg btn-primary btn-block" value="Sign in">
</form>
</div>
Between tags I call LoginServlet by defining method as "post".
在标签之间我通过将方法定义为“post”来调用LoginServlet。
#1
25
Just create a class extending HttpServlet
and annotate it with @WebServlet
on a certain URL pattern.
只需创建一个扩展HttpServlet的类,并在某个URL模式上使用@WebServlet对其进行注释。
@WebServlet("/login")
public class LoginServlet extends HttpServlet {}
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>
in web.xml
and map it on a certain URL pattern via <servlet-mapping>
.
或者当你仍然使用Servlet 2.5或更早版本时(注释是自Servlet 3.0以来的新版本),然后在web.xml中将servlet注册为
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.example.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
Then, just let the HTML link or form action point to an URL which matches the url-pattern
of the servlet.
然后,只需让HTML链接或表单操作指向与servlet的url-pattern匹配的URL。
<a href="${pageContext.request.contextPath}/login">Login</a>
<form action="${pageContext.request.contextPath}/login" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit">
</form>
When using submit buttons, make sure that you use type="submit"
and not type="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.
使用提交按钮时,请确保使用type =“submit”而不是type =“button”。可以在此相关问题和答案中找到有关$ {pageContext.request.contextPath}部分的说明:如何在HTML表单操作中使用servlet URL模式,而不会收到HTTP 404错误。
Links and forms with method="get"
will invoke doGet()
method of the servlet. You usually use this method to preprocess a request "on page load".
使用method =“get”的链接和表单将调用servlet的doGet()方法。您通常使用此方法预处理“页面加载”请求。
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ...
}
Forms with method="post"
will invoke doPost()
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).
使用method =“post”的表单将调用servlet的doPost()方法。您通常使用此方法使用用户提交的表单数据对请求进行后处理(收集请求参数,转换并验证它们,更新模型,调用业务操作并最终呈现响应)。
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// ...
}
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.
要了解有关servlet的更多信息并找到更具体的示例,请访问我们的Servlets维基页面。值得注意的是,您也可以使用JSP文件而不是纯HTML文件。 JSP允许您在生成HTML输出时通过EL表达式与后端进行交互,并使用像JSTL这样的taglib来控制流。另请参阅我们的JSP wiki页面。
#2
0
For instance I create a login.html like that
例如,我创建了一个这样的login.html
<div class="container">
<form method = "post" class="form-signin" role="form" action="LoginServlet">
<h2 class="form-signin-heading">Please sign in</h2>
<input type="text" class="form-control" name = "username" placeholder="User Name" required autofocus>
<input type="password" class="form-control" name = "password" placeholder="Password" required>
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Remember me
</label>
</div>
<input type="submit" class="btn btn-lg btn-primary btn-block" value="Sign in">
</form>
</div>
Between tags I call LoginServlet by defining method as "post".
在标签之间我通过将方法定义为“post”来调用LoginServlet。