解决了将数据从一个servlet传递到另一个servlet的问题

问题描述 投票:0回答:1

我已经编写了一个jsp页,两个servlet和一个最终页的代码。jsp页将数据从表单发送到第一个servlet(控制器),然后将其发送到第二个servlet(AreaCheck)。在/ controller页面上(请参阅web.xml),发生错误405“不支持HTTP方法POST”。

index.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);
}}

Web.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>

此处仅显示相关代码。程序的逻辑看似很奇怪,但是由于它是任务,因此无法更改。

我该如何解决?

java jsp servlets
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.