我attemting从按下按钮移动到不同的JSP在我的索引(主)JSP throught在那里我可以检查是否按下按钮,进入我的doGet这个servlet这个servlet。当我打开我的浏览器,点击按钮,我得到一个错误说:HTTP错误405问题存取/ lab02a_dhilbert1 /指数。原因:
HTTP method POST is not supported by this URL
我不知道这样做的原因是,我需要通过doGets移动到其他页面。
我确实有它的工作它是建立在一个单独的doPost的方式,但它应该在的doGet要做。我试图研究这个问题,很难找到任何东西。
//的servlet
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//go to correct jsp depending on button press
if (req.getParameter("addNumbers") != null) {
System.out.println("Add Numbers Servlet: doGet");
req.getRequestDispatcher("/_view/addNumbers.jsp").forward(req, resp);
}
else if (req.getParameter("multiplyNumbers") != null) {
System.out.println("Multiply Numbers Servlet: doGet");
req.getRequestDispatcher("/_view/multiplyNumbers.jsp").forward(req, resp);
}
else if (req.getParameter("guessingGame") != null) {
System.out.println("Guessing Game Servlet: doGet");
req.getRequestDispatcher("/_view/guessingGame.jsp").forward(req, resp);
}
else {
System.out.println("Index Servlet: doGet");
req.getRequestDispatcher("/_view/index.jsp").forward(req, resp);
}
}
// JSP
<body>
<form action="${pageContext.servletContext.contextPath}/index" method="post">
This is the index view jsp
<br><input type="Submit" name="addNumbers" value="Add Numbers!!">
<br><input type="Submit" name="multiplyNumbers" value="Multiply Numbers!!">
<br><input type="Submit" name="guessingGame" value="Guessing Game!!">
</form>
</body>
该指数JSP应该送按下按钮到servlet,然后servlet的应该调用正确的JSP,这取决于是否按下任何按钮。
对于POST请求,你必须写代码到
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//go to correct jsp depending on button press
if (req.getParameter("addNumbers") != null) {
System.out.println("Add Numbers Servlet: doGet");
req.getRequestDispatcher("/_view/addNumbers.jsp").forward(req, resp);
}
else if (req.getParameter("multiplyNumbers") != null) {
System.out.println("Multiply Numbers Servlet: doGet");
req.getRequestDispatcher("/_view/multiplyNumbers.jsp").forward(req, resp);
}
else if (req.getParameter("guessingGame") != null) {
System.out.println("Guessing Game Servlet: doGet");
req.getRequestDispatcher("/_view/guessingGame.jsp").forward(req, resp);
}
else {
System.out.println("Index Servlet: doGet");
req.getRequestDispatcher("/_view/index.jsp").forward(req, resp);
}
}
希望它的工作非常适合岗位的要求。