Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaServlet重定向到页面自动调用其表单Servlet_Java_Html_Servlets - Fatal编程技术网

JavaServlet重定向到页面自动调用其表单Servlet

JavaServlet重定向到页面自动调用其表单Servlet,java,html,servlets,Java,Html,Servlets,问题在于,当我填写表单时,它调用了一个servlet,该表单显示了正确的信息,当用户单击后退按钮时,它似乎在该页面上调用了值为null的servlet。如何使其重新加载页面,以便用户可以在表单中重新填充 SetTimeZone.xhtml <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>SetTimeZone</title> <meta name="view

问题在于,当我填写表单时,它调用了一个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>
重定向回页面后得到的输出是;
抱歉,没有空的可用信息。

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

通过对表单使用POST方法,您可能会遇到此问题。正如@visray所建议的,您需要重写dogetservlet方法,以便GET方法能够正常工作


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

我以前尝试过此方法,但在更改GET to POST时出现此错误。HTTP状态405-此URL不支持HTTP方法GET您可以覆盖servlet中的GET方法并调用doPostreq,res;在重写的doGet方法中
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");
}
}