我的 JSP 页面上有这段代码
<form action="LoginServlet" method="get">
<input type="text" name="username" />
<input type="text" name="password" />
<input type="submit" value="Submit" />
</form>
当我按下提交时,我得到:
The page cannot be displayed The page you are looking for is currently unavailable. The Web site might be experiencing technical difficulties, or you may need to adjust your browser settings.
和 LoginServlet.doGet
方法不会被调用。
但是当我再次按 Enter 键(在地址栏中)时,我的 doGet
方法被调用。
怎么了?我正在使用 Java EE eclipse 和 Tomcat
最佳答案
您的 web.xml 文件中有什么?
您的 WEB-INF/web.xml 文件需要将 JSP 中指定的 LoginServlet 与 Java 类 LoginServlet 相关联。 (为清楚起见,我更改了值的名称。)
所以如果在你的 JSP 中你有
<form action="jspAction" method="get">
<input type="submit" value="Submit" />
</form>
你的 Java 类是
package com.me
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginServlet extends HttpServlet {
public void goGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
//your code
}
}
你的 web.xml 文件应该有
<web-app>
<!-- Standard Action Servlet Configuration -->
<servlet>
<servlet-name>myServletName</servlet-name>
<servlet-class>com.me.LoginAction</servlet-class>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>myServletName</servlet-name>
<url-pattern>jspAction</url-pattern>
</servlet-mapping>
</web-app>
因此 web.xml 文件会将 myServletName 与 servlet 类 com.me.LoginAction 相关联。然后对 http://localhost:8080/myApp/jspAction 的任何请求将定向到 myServletName,并最终定向到 com.me.LoginAction。
关于jsp - Servlet 的 doGet 不是从 JSP 表单调用的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2757861/