【问题标题】:Servlet's doGet not called from JSP formServlet 的 doGet 未从 JSP 表单调用
【发布时间】:2011-02-15 00:24:59
【问题描述】:

我的 JSP 页面上有这段代码

<form action="LoginServlet" method="get">
    <input type="text" name="username" /> 
    <input type="text" name="password" /> 
    <input type="submit" value="Submit" />
</form>

当我按下提交时,我得到:

页面无法显示该页面 您正在寻找目前 不可用。该网站可能是 遇到技术难题, 或者您可能需要调整浏览器 设置。

并且LoginServlet.doGet 方法不会被调用。 但是当我再次按 Enter(在地址栏中)时,我的 doGet 方法被调用。

怎么了?我正在使用 Java EE eclipse 和 Tomcat

【问题讨论】:

  • 用真正的网络浏览器测试它怎么样?例如。 Firefox.
  • 该错误消息看起来特定于 Internet Explorer,而 IE 可能显示该消息的原因有很多(例如internetfixes.com/readers_questions/IF01710.htm)。正如@BalusC 建议的那样,手头有一个备用浏览器是帮助确定问题是浏览器还是 Web 服务器的好工具。试试看,用更多细节更新您的问题。

标签: jsp tomcat servlets


【解决方案1】:

您的 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。

【讨论】:

    猜你喜欢
    • 2015-06-02
    • 2015-04-01
    • 1970-01-01
    • 2013-01-11
    • 2012-11-12
    • 2015-02-06
    • 2012-01-31
    • 1970-01-01
    • 2017-12-03
    相关资源
    最近更新 更多