且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从 HTML 表单调用 servlet 类

更新时间:2023-12-04 14:41:10

只需创建一个扩展 HttpServlet 并用 @WebServlet 在某个 URL 模式上.

Just create a class extending HttpServlet and annotate it with @WebServlet on a certain URL pattern.

@WebServlet("/login")
public class LoginServlet extends HttpServlet {}

或者当您仍然使用 Servlet 2.5 或更早版本时(注解自 Servlet 3.0 以来是新的),然后在 web.xml中将 servlet 注册为 <servlet>> 并通过 将其映射到某个 URL 模式.

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

然后,让 HTML 链接或表单操作指向与 servlet 的 url-pattern 匹配的 URL.

Then, just let the HTML link or form action point to an URL which matches the url-pattern of the servlet.

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

使用提交按钮时,请确保使用 type="submit" 而不是 type="button".${pageContext.request.contextPath} 部分的说明可以在这个相关问答中找到:如何使用HTML 表单操作中的 servlet URL 模式而不会出现 HTTP 404 错误.

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.

带有 method="get" 的链接和表单将调用 servlet 的 doGet() 方法.您通常使用此方法在页面加载时"预处理请求.

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".

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // ...
}

带有 method="post" 的表单将调用 servlet 的 doPost() 方法.您通常使用此方法对包含用户提交的表单数据的请求进行后处理(收集请求参数、转换和验证它们、更新模型、调用业务操作并最终呈现响应).

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).

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // ...
}

要了解有关 servlet 的更多信息并查找更具体的示例,请前往我们的 Servlet 维基页面.应该注意的是,您还可以使用 JSP 文件而不是纯 HTML 文件.JSP 允许您在生成 HTML 输出时通过 EL 表达式与后端交互,并使用 JSTL 之类的标签库来控制流程.另请参阅我们的 JSP wiki 页面.

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.