如何从HTML表单调用servlet类 [英] How to call servlet class from HTML form
问题描述
我创建了一个Web应用程序项目。它包含一个servlet类和一个HTML表单。如何从HTML表单调用servlet类?
只需创建一个扩展 HttpServlet
并注释它与 @WebServlet
$ b
@WebServlet(/ login)$ b $ code> 关于特定的网址格式。 b public class LoginServlet extends HttpServlet {}
或者当您仍然使用Servlet 2.5或更旧版本时注释自Servlet 3.0以来是新的),然后在 web.xml
中注册servlet为< servlet>
并映射它通过< servlet-mapping>
。
< servlet的>
< servlet-name>登录< / servlet-name>
< servlet-class> com.example.LoginServlet< / servlet-class>
< / servlet>
< servlet-mapping>
< servlet-name>登录< / servlet-name>
< url-pattern> / login< / url-pattern>
< / servlet-mapping>
然后,让HTML链接或表单操作指向一个与<$ c $匹配的URL c> url-pattern 。
$ b
< a HREF = $ {pageContext.request.contextPath} /登录 >登录< / A>
< form action = $ {pageContext.request.contextPath} / loginmethod =post>
< input type =textname =username>
< input type =passwordname =password>
< input type =submit>
< / form>
使用提交按钮时,请确保使用 type =submit
而不是 type =button
。关于 $ {pageContext.request.contextPath}
部分的解释可以在这个相关的问题和答案中找到:如何在HTML表单操作中使用servlet URL模式而不会出现HTTP 404错误。
链接和表单使用 method = get
会调用servlet的 doGet()
方法。您通常使用此方法预先处理请求在页面加载。
@Override
保护无效doGet(HttpServletRequest请求,HttpServletResponse响应)throws ServletException,IOException {
// ...
}
使用 method =post
的表单将调用servlet的 doPost()
方法。您通常使用此方法使用用户提交的表单数据后处理请求(收集请求参数,转换和验证它们,更新模型,调用业务操作并最终呈现响应)。
< pre $ @Override
保护无效doPost(HttpServletRequest请求,HttpServletResponse响应)抛出ServletException,IOException {
// ...
}
要了解有关servlet的更多信息并查找更多具体示例,请转至我们的Servlets wiki页面。值得注意的是,您也可以使用JSP文件而不是纯HTML文件。 JSP允许您在生成HTML输出的同时通过EL表达式与后端进行交互,并使用像JSTL这样的taglib来控制流。另请参阅我们的JSP wiki页面。
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?
Just create a class extending HttpServlet
and annotate it with @WebServlet
on a certain URL pattern.
@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>
<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.
<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.
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 {
// ...
}
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 {
// ...
}
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.
这篇关于如何从HTML表单调用servlet类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!