简体   繁体   English

表单向Servlet发送空查询字符串

[英]Form Sending Null Query String to Servlet

I have a JSP that has a form that looks like this: 我有一个JSP,其形式如下:

<form method="GET" action="ManagerLogicServlet?action=salesreport" >
                <select name="monthList">
                    <option value="1">January</option>
                    <option value="2">February</option>
                    <option value="3">March</option>
                    <option value="4">April</option>
                    <option value="5">May</option>
                    <option value="6">June</option>
                    <option value="7">July</option>
                    <option value="8">August</option>
                    <option value="9">September</option>
                    <option value="10">October</option>
                    <option value="11">November</option>
                    <option value="12">December</option>
                </select>
                <input type="submit" value="Submit">
    </form>

I am trying to send over a query string with attribute action = salesreport which will be a condition that will return a sales report for the selected month (don't mind the missing default value). 我正在尝试通过属性为action = salesreport的查询字符串发送查询,这将是返回选定月份的销售报告的条件(不要介意缺少的默认值)。 I submit the form over to the ManagerLogicServlet which has this code snippet: 我将表单提交给具有以下代码片段的ManagerLogicServlet:

..String action = request.getParameter("action");

    if (action.equalsIgnoreCase("salesreport")){

        forward = SALES_REPORT;
        int month = Integer.parseInt(request.getParameter("monthList"));
        String monthString = new DateFormatSymbols().getMonths()[month-1];
        request.setAttribute("monthString", monthString);
        request.setAttribute("salesReport", salesDAO.getSalesReport(month));

    } else if..

But the action attribute is set to null. 但是action属性设置为null。 Why is this? 为什么是这样?

Because your form is using the GET method, the parameters from the action attribute are being discarded. 因为您的表单使用的是GET方法,所以来自action属性的参数将被丢弃。 If you insist on using GET , then you can include an <input> tag containing the parameter you wish to pass on to the servlet. 如果您坚持使用GET ,那么可以包含一个<input>标记,其中包含您希望传递给servlet的参数。 Try doing this: 尝试这样做:

<form method="GET" action="ManagerLogicServlet?action=salesreport" >
    <input type="hidden" name="action" value="salesreport">
    <select name="monthList">
        <option value="1">January</option>
        ...
    </select>
    <input type="submit" value="Submit">
</form>

The alternative would be for you to leave your code as is, but change the form's method to POST . 另一种选择是让您保留您的代码,但将表单的方法更改为POST

Its working fine. 它的工作正常。 I tried this 我试过了

HTML 的HTML

    <form action="AnyServlet?action=salesreport" method="post">
        <input type="submit" value="Submit Data" />
    </form>

AnyServlet.java AnyServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");
        System.out.println("action=="+action);


        }

Output 输出量

action==salesreport

UPDATE 更新

When i changed from "post" to "get",I am getting issue too.You can use hidden input field if you want go with "get". 当我从“发布”更改为“获取”时,我也遇到问题。如果要使用“获取”,则可以使用隐藏的输入字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM