修复了将数据从一个 servlet 传递到另一个 servlet 的问题



我写了一个jsp-page的代码,两个servlet和一个最后一页。 jsp-page 将数据从表单发送到第一个 servlet(控制器(,后者又将数据发送到第二个 servlet(AreaCheck(。 在/controller 页面上(请参阅 web.xml(,出现错误 405"不支持 HTTP 方法 POST"。

索引.jsp:

<script>
function clickCheckbox(numbid){
var currentCB = document.getElementById(numbid);
if (currentCB.checked === true){
document.getElementById('hidden_x').value = numbid;
}
else{
document.getElementById('hidden_x').value = "";
}
}
</script>
<form action="controller" method="post" id="form"  onsubmit="check(); return true;" novalidate>
<label class="instructions" for="hidden_x"> Insert X: </label>
<p><input type="checkbox" class="button" name="button_x" value="-5" id="-5" onclick="clickCheckbox(-5)"> -5 </p>
<input type="hidden" name="hidden_x" id="hidden_x" value=""> <br>

<label for="yCoordinate" class="instructions"> Insert Y: </label>
<input type="text" id="yCoordinate" name="yCoordinate" autocomplete="off">
<label class="instructions" for="radius"> Insert R: </label>
<select id="radius" name="radius">
<option value="1">1</option>
</select>
<br>
<p id="button" class="input_button">
<input type="submit" value="Check the result.">
</p>
</form>

ControllerServlet.java:

public class ControllerServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String X = req.getParameter("hidden_x");
String Y = req.getParameter("yCoordinate");
String R = req.getParameter("radius");
req.setAttribute("XCoordinate", X);   
req.setAttribute("YCoordinate", Y);
req.setAttribute("Radius", R);
req.getRequestDispatcher("areaCheck").forward(req, resp);
}}

AreaCheckServlet.java:

public class AreaCheckServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
String x = (String) req.getAttribute("XCoordinate");
String y = (String) req.getAttribute("YCordinate");
String r = (String) req.getAttribute("Radius"); 
//some other logic goes here
req.getRequestDispatcher("result.html").forward(req, resp);
}}

网站.xml:

<!--Controller Servlet-->
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>App.Servlets.ControllerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/controller</url-pattern>
</servlet-mapping>
<!--Servlet that checks hitting the area-->
<servlet>
<servlet-name>AreaCheck</servlet-name>
<servlet-class>App.Servlets.AreaCheckServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AreaCheck</servlet-name>
<url-pattern>/areaCheck</url-pattern>
</servlet-mapping>

此处仅显示相关代码。程序的逻辑可能看起来很奇怪,但它无法更改,因为它是任务。

我该如何解决它?

AreaCheckServlet 应该支持 POST 方法,以便您将 post 请求转发给它。 在 AreaCheckServlet 中实现 doPost

相关内容

  • 没有找到相关文章

最新更新