Java 如何使用表单操作从 JSP 页面映射 servlet 调用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23751965/
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
How to map a servlet call from a JSP page using form action?
提问by StrugglingCoder
I have a JSP page named Welcome_2.html and in its form action I have invoked a servlet like this :
我有一个名为 Welcome_2.html 的 JSP 页面,在它的表单操作中,我调用了一个像这样的 servlet:
<form action="/servlets/MyFirstServlet" method="post" id="form_id">
The servlet "MyFirstServlet" is under WEB-INF
classes
servlets
MyFirstServlet
servlet“MyFirstServlet”位于WEB-INF 类 servlets MyFirstServlet
and the jsp is under the folder HTML which is in the same level like WEB-INF
并且jsp在与WEB-INF相同级别的文件夹HTML下
i.e. inside practice I have 3 foldersHTML
META-INF
WEB-INF
即在实践中我有 3 个文件夹HTML META-INF WEB-INF
in web.xml I have the following snippet
在 web.xml 我有以下片段
<servlet>
<servlet-name>MyFirstServlet</servlet-name>
<servlet-class>servlets.MyFirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyFirstServlet</servlet-name>
<url-pattern>/servlets/MyFirstServlet</url-pattern>
</servlet-mapping>
Why the servlet is not being invoked? I am clicking on the HTML page on my browser and trying to invoke the servlet ... I am just a beginner pardon me for my poor intellect.
为什么不调用 servlet?我正在单击浏览器上的 HTML 页面并尝试调用 servlet ...我只是一个初学者,请原谅我的智力低下。
采纳答案by SpringLearner
As your form action is "/servlets/First"
so your url pattern should be
由于您的表单操作是"/servlets/First"
这样,因此您的网址模式应该是
<url-pattern>/servlets/First</url-pattern>
回答by Santhosh
Change your jsp form to ,
将您的jsp表单更改为,
<form action="/servlets/MyFirstServlet" method="post" id="form_id">
to match the url
pattern in your web.xml
匹配url
您的模式web.xml
<servlet-mapping>
<servlet-name>MyFirstServlet</servlet-name>
<url-pattern>/servlets/MyFirstServlet</url-pattern>
</servlet-mapping>
This line <url-pattern>/servlets/MyFirstServlet</url-pattern>
refers that url's matching the pattern will invoke the MyFirstServlet
这一行<url-pattern>/servlets/MyFirstServlet</url-pattern>
是指与模式匹配的 url 将调用MyFirstServlet
Read the Oracle Tutorialbefore you configure your web.xml
elements
在配置元素之前阅读Oracle 教程web.xml
Hope this helps !!
希望这可以帮助 !!
回答by Maurice Perry
Unless your app is deployed as ROOT.war, all your URLs will be relative to http://myserver/webapp
. So my guess is that you should rather use relative URLs. As your JSP is in HTML, you would need to write:
除非您的应用程序部署为 ROOT.war,否则您的所有 URL 都将相对于http://myserver/webapp
. 所以我的猜测是你应该使用相对 URL。由于您的 JSP 是 HTML,您需要编写:
<form action="../servlets/MyFirstServlet" method="post" id="form_id">
回答by James
If you use tomcat 7 , you don't need to care about that. For example :
如果您使用 tomcat 7 ,则无需关心。例如 :
In your servlet :
在您的 servlet 中:
@WebServlet("/myFirstServlet")
public class LoginPage extends HttpServlet {
// your code
}
In your html :
在你的 html 中:
<!-- here you write myFirstServlet in the action tag -->
<form id="somethingGoesHere" action="myFirstServlet" method="post" >