如何从 HTML 表单调用 servlet 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2395251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:25:01  来源:igfitidea点击:

How to call servlet class from HTML form

htmlformsservlets

提问by user246160

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 类?

回答by BalusC

Just create a class extending HttpServletand annotate it with @WebServleton a certain URL pattern.

只需创建一个扩展类HttpServlet并使用@WebServlet特定 URL 模式对其进行注释即可。

@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.xmland map it on a certain URL pattern via <servlet-mapping>.

或者,当您仍在使用 Servlet 2.5 或更早版本时(注解自 Servlet 3.0 以来是新的),然后将 servlet 注册为<servlet>inweb.xml并通过<servlet-mapping>.

<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-patternof the servlet.

然后,让 HTML 链接或表单操作指向与url-patternservlet匹配的 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}部分的解释可以在这个相关问答中找到:How to use servlet URL pattern in HTML form action without getting HTTP 404 error

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"将调用doGet()servlet 的方法。您通常使用此方法“在页面加载时”预处理请求。

@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"将调用doPost()servlet 的方法。您通常使用此方法对带有用户提交的表单数据的请求进行后处理(收集请求参数、转换和验证它们、更新模型、调用业务操作并最终呈现响应)。

@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 wiki 页面。应该注意的是,您还可以使用 JSP 文件而不是纯 HTML 文件。JSP 允许您在生成 HTML 输出时通过 EL 表达式与后端交互,并使用 JSTL 之类的标签库来控制流程。另请参阅我们的 JSP wiki 页面

回答by user3228915

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。