weixin_33691598 2015-06-14 18:13 采纳率: 0%
浏览 8

Servlet JSON Java

I am sending json using ajax from my jsp page, as follows:

function dateRCA()
{

var $tipUtilizare = $("select[name=tipUtilizare]").val();
var $dataRCA = $("input[name=dataRCA]").val();

var jsonObj = {"tipUtilizare": $tipUtilizare,
    "dataRCA": $dataRCA};

alert($tipUtilizare);
alert($dataRCA);

$.ajax
        (
                {
                    url: 'AsigurareRCA',
                    data: {dateRCA: JSON.stringify(jsonObj)},
                    dataType: 'json',
                    type: 'post',
                    cache: false,
                    success: function (raspuns) {

                        alert("succes");

                        if (raspuns.status.succes === "true") {

                            $("#dateAsigurareRCATab").removeClass("active");
                            $("#dateAsigurareRCATab").addClass("disabled");
                            $("#dateAsigurareCASCOTab").removeClass("disabled");
                            $("#dateAsigurareCASCOTab").addClass("active");
                            $("#dateAsigurareRCA").removeClass("active");
                            $("#dateAsigurareRCA").removeClass("in");
                            $("#dateAsigurareCASCO").addClass("active");
                            $("#dateAsigurareCASCO").addClass("in");

                        }
                        else {
                            //Handlde errors
                        }

                    },
                    error: function () {
                        alert('Ne cerem scuze, dar a aparut e eroare de sistem!');

                    }
                }
        );

}

The form from my jsp file that uses these functions is:

<div class="tab-pane fade" id="dateAsigurareRCA">
                <div  class="mainbox ">
                    <div class="panel panel-info">
                        <div class="panel-body" >
                            <form name="dateAsigurareRCA" action="AsigurareRCA" method="post" class="form-horizontal" role="form">

                                <div class="form-group">
                                    <label for="tipUtilizare" class="col-md-6 control-label">Tip utilizare autovehicul</label>
                                    <div class="col-md-6">
                                        <div class="col-md-6">
                                            <select class="form-control"  name="tipUtilizare" id="tipUtilizare">
                                                <option value="">Selecteaza tipul de utilizare</option>
                                            </select>
                                            <span style="color:red; display: none" class="eroareTipUtilizare">  </span>
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group" id="event_period">
                                    <label for="dataRCA" class="col-md-6 control-label">Data</label>
                                    <div class="col-md-6">
                                        <div class="col-md-6">

                                            <div class="input-group">
                                                <input id="datepicker" type="text" class="date-picker form-control" name="dataRCA" value="" readonly/>
                                                </label>
                                            </div>
                                        </div>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <!-- Button -->                                        
                                    <div class="col-md-offset-3 col-md-9">
                                        <input  id="btn-signup" type="submit" class="btn btn-info" name="nextAsigurareCASCO" value="Continuare" onclick="dateRCA();">

                                    </div>
                                </div>

                            </form>
                        </div>
                    </div>

                </div>
            </div>
            <div class="tab-pane fade" id="dateAsigurareCASCO">
                Date asigurare casco
            </div>

So, if the servlet tells me the information is correct, I need to change the tab and show the next div.

Here is the post method from the servlet that handles the data received (I am using GSON to handle JSON):

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("application/json");
    Gson gson = new Gson();

    synchronized (getServletContext()) {
        ServletContext context = getServletContext();
        ConnectionPool cp = (ConnectionPool) context.getAttribute("CONNECTION_POOL");

        System.out.println("ce am preluat " + request.getParameter("dateRCA"));

        String tipUtilizare = request.getParameter("tipUtilizare");
        String dataRCA = request.getParameter("dataRCA");

        Enumeration<String> parameterNames = request.getParameterNames();

        while (parameterNames.hasMoreElements()) {

            String paramName = parameterNames.nextElement();
            System.out.println("parametru name " + paramName);

            String[] paramValues = request.getParameterValues(paramName);
            for (int i = 0; i < paramValues.length; i++) {
                String paramValue = paramValues[i];
                System.out.println("parametru value  " + paramValue);
            }

        }

        HttpSession session = request.getSession();
        Connection con;

        Map<String, Map<String, Object>> mesaj = new LinkedHashMap<>();
        Map<String, Object> status = new LinkedHashMap<>();
        status.put("succes", "true");
        mesaj.put("status", status);
        response.getOutputStream().print(gson.toJson(mesaj));
        response.getOutputStream().flush();

    }

}

The problem is that request.getParameter("dateRCA") returns null. What I get from the jsp page are the following (those are the request parameters and their values ):

Info:   param name tipUtilizare
Info:   param value  Paza si protectie
Info:   param name dataRCA
Info:   param value  06/16/2015
Info:   param name nextAsigurareCASCO
Info:   param value  Continuare

It gives me even the button's value, even if I don't send it from ajax ...

What I get from the servlet is a valid JSON: {"status":{"succes":"true"}}, but instead of changing the active tab and the div, it redirects me to the servlet link, showing me the JSON object sent.

I used the same method to work with some other forms and it worked great.. And after a few hours of trying to figure out how to solve this... and finding no reason why it doesn't work like it should.. I ask you, if u can see what the problem is and how can I solve it.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
    • ¥15 不小心不正规的开发公司导致不给我们y码,
    • ¥15 我的代码无法在vc++中运行呀,错误很多
    • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
    • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
    • ¥30 ppOCRLabel导出识别结果失败
    • ¥15 Centos7 / PETGEM
    • ¥15 csmar数据进行spss描述性统计分析
    • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
    • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题