【问题标题】:doPost() of GAE servlet doesn't workGAE servlet 的 doPost() 不起作用
【发布时间】:2013-07-07 22:16:50
【问题描述】:

我是 JSP 的初学者,我有以下 servlet:

@SuppressWarnings("serial")
public class HelloAppIgorServlet extends HttpServlet {
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        RequestDispatcher disp = req.getRequestDispatcher("/mainpage.jsp");
        disp.forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter pw = resp.getWriter();
        pw.print("Test");
        pw.close();
    }
}

还有一个名为 mainpage.jsp 的 JSP 文件:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <p>Hello there!<p>
    <form action="." method="post">
        <input type="text" name="name1" />
        <input type="submit" value="name2" />
    </form>
</body>
</html>

问题是 doPost() 方法不起作用。当我单击按钮时,它会将我重定向到 index.html 页面。我确定问题不在 servlet 中,那么问题出在哪里?

【问题讨论】:

  • 如果您在此处输入 servlet 的绝对 URL,会发生什么情况:action="."?
  • 应该看起来像action="ProjectName/src/org/package/HelloAppIgorServlet.java"
  • 不,应该是您在web.xml 中为servlet 定义的URL 映射组件。
  • 好的,可以了!非常感谢你,同志!

标签: java google-app-engine jsp servlets


【解决方案1】:

您需要在&lt;form action="." method="post"&gt; 中设置相应的操作。该操作是您通过&lt;servlet-mapping&gt;web.xml 中定义的servlet 的(相对)URL。

【讨论】:

    猜你喜欢
    • 2018-11-19
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 1970-01-01
    • 2011-01-21
    • 2017-01-05
    • 2017-12-23
    • 2011-08-18
    相关资源
    最近更新 更多