【问题标题】:Java Servlet redirect to page auto calls its form servletJava Servlet 重定向到页面自动调用其表单 servlet
【发布时间】:2017-02-26 17:05:36
【问题描述】:

当我填写表单时,它调用了一个显示正确信息的 servlet,当用户单击后退按钮时,它似乎调用了该​​页面上值为 null 的 servlet。我该怎么做才能重新加载页面,以便用户可以重新填写表单。

SetTimeZone.xhtml

 <html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>SetTimeZone</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<body>
    <div>
        <form name ="SetTimeZone" method="post" action="SetTimeZoneServlet">
            Set Time Zone: <input type="text" name="timeZone"/><br></br><br></br>
            <input type ="submit" value="Submit" name="submit"/>
        </form>
    </div>
</body>

public class SetTimeZoneServlet extends HttpServlet {

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    TimeZoneBean bean = new TimeZoneBean();
    String city = request.getParameter("timeZone");
    bean.setCity(city);
    String temp = bean.checkCity();
    String value = "";

    if ("error".equals(temp)) {
        value = "Sorry no information is availible for " + city;
    } else {
        value = "The current time in " + city + " is " + bean.getTime();
    }

    try (PrintWriter out = response.getWriter()) {

        response.setContentType("text/html;charset=UTF-8");

        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet OrderFormServlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<p>" + value + "</p>");
        out.println("<form name=\"SetTimeZone.xhtml\" method=\"post\" name=\""
                + "SetTimeZoneRedirectServlet\"> ");
        out.println("<input type =\"submit\" value=\"Back\"/ name=\"back\">");
        out.println("</form>");
        out.println("</body>");
        out.println("</html>");
    }
}

public class SetTimeZoneRedirectServlet extends HttpServlet {

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.sendRedirect("SetTimeZone.xhtml");
}
}

重定向回页面后我得到的输出是; 抱歉,没有可用的 null 信息。

【问题讨论】:

    标签: java html servlets


    【解决方案1】:

    尝试使用 GET 而不是 POST 这可能会解决您的问题

    【讨论】:

    • 我之前尝试过这个,当我将 GET 更改为 POST 时出现此错误。 HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET
    • 你可以覆盖servlet中的get方法并调用doPost(req, res);在被覆盖的 doGet() 方法中
    【解决方案2】:


    通过为您的表单使用POST 方法,您可能会遇到此问题。正如@visray 建议的那样,您需要覆盖doGet() Servlet 方法,GET 方法才能正常工作。

    在处理数据库更改时应使用POST 方法,因此在您的情况下GET 是合适的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-27
      • 1970-01-01
      • 1970-01-01
      • 2019-07-16
      • 2015-07-24
      相关资源
      最近更新 更多