0

I have a jsp page in my project. In this jsp page, I have inputs that are filling by javascript function. When the servlet post action happens, this input's values are disappears. I want to fill these input again when the servlet forwarded the jsp page. So, is it possible to trigger a javascript function from servlet?

My JSP page codes

<div style="margin-left:7%; margin-right:10%; border-style:groove;">
            <div style="margin-right:65&; margin-left:5px;">
                <label for="salesOrder">Sales Order</label>&nbsp;&nbsp;<input id="sOrder" style="margin-left:58px;" onkeyup="this.value = this.value.toUpperCase();" onselect="salesOrder();fill_table();" value="${salesorder}">&emsp;&emsp;<label style="margin-left:75px;">Count:</label>&nbsp;&nbsp;<input type="text" id="count" size="3" style="margin-top:3px;"><br>
                <label>Ind. Date:</label>&emsp;&nbsp;<input type="text" id="induction" style="margin-left:59px;" size="15"><br>
                <label>Customer/Operator:</label>&nbsp;<textarea  id="customer" rows="1" cols="10" wrap="soft"></textarea>&ensp;<textarea id="operator" rows="1" cols="10" wrap="soft"></textarea><br>
                <label>Removal Reason:</label>&emsp;<textarea rows="2" cols="30" wrap="soft" id="rem_reason" style="margin-left:10px; height:50px; text-size:10px;"></textarea>
            </div>  
            <div style="margin-left:45%; margin-top:-130px;">
                <input type="checkbox" name="v2500" value="V2500-A5" onclick="check_v25()">&nbsp;<label for="v2500">V2500-A5</label><br>
                <input type="checkbox" name="7b" value="CFM56-7B" onclick="check_7b()">&nbsp;<label for="7b">CFM56-7B</label><br>
                <input type="checkbox" name="5c" value="CFM56-5C" onclick="check_5c()">&nbsp;<label for="5c">CFM56-5C</label><br>
                <input type="checkbox" name="3" value="CFM56-3" onclick="check_3()">&nbsp;<label for="3">CFM56-3</label>
            </div>
            <div style="margin-left:65%; margin-top:-130px;">
                <label>TSN:</label>&emsp;&ensp;<input type="text" id="tsn" style="margin-left:1px;"><br>
                <label>CSN:</label>&emsp;&ensp;<input type="text" id="csn" style="margin-left:-0.5px;"><br>
                <label>TSLSV:</label>&ensp;&nbsp;<input type="text" id="tslsv" style="margin-left:-1px;"><br>
                <label>CSLSV:</label>&ensp;&nbsp;<input type="text" id="cslsv" style="margin-left:-3px;">
            </div>
        </div>

My JavaScript function

<script>
function salesOrder(event,ui){
    $("#sOrder").val(ui.item.label);
    var salesOrder=$("#sOrder").val();
    var URL_PREFIX="http://ptrisd01:8983/solr/SalesOrderCore/select?q=STRSO:";
    var URL=URL_PREFIX+salesOrder;
    $.ajax({
        url : URL,
        dataType : 'json',
        type:'get',
        json : 'json.wrf',
        success:function(data){
            console.log("success");
            var docs = JSON.stringify(data.response.docs);
            var jsonData = JSON.parse(docs);
            document.getElementById("induction").value=jsonData[0].STRINDDATE;
            document.getElementById("customer").value=jsonData[0].STRCUSTOMER;
            document.getElementById("operator").value=jsonData[0].STROPERATOR;
            document.getElementById("rem_reason").value=jsonData[0].STRREMREASON;
            document.getElementById("tsn").value=jsonData[0].STRTSN;
            document.getElementById("csn").value=jsonData[0].STRCSN;
            document.getElementById("tslsv").value=jsonData[0].STRTSLSV;
            document.getElementById("cslsv").value=jsonData[0].STRCSLSV;
        }
    });
};
</script>

A part of my servlet codes

            request.setAttribute("salesorder", salesOrder_hidden);
            PrintWriter out = response.getWriter();
            out.println("<script type=\"text/javascript\">");
            //out.println("alert('Saving completed...');");
            //out.println("location='upload.jsp';");
            out.println("salesOrder();");
            out.println("</script>");
            request.getRequestDispatcher("upload.jsp").forward(request, response);

Also, I'm using JQuery Autocomplete for sOrder input.

demir5334
  • 215
  • 7
  • 17
  • 1
    There are two ways ..either store it at client side or server side . With client side use `localstorage or sessionstorage` and at server side use `setAttribute..` – Swati Dec 17 '20 at 12:55
  • @Swati I'm using IE for some reasons and as I know the IE doesn't support **localstorage** or **sessionstorage**. Am I right? – demir5334 Dec 21 '20 at 10:49
  • I have not use localstorage on IE but it might work on newer versions .Also, check [these](https://stackoverflow.com/questions/23391353/localstorage-on-internet-explorer-not-working) and [this](https://stackoverflow.com/questions/3392032/localstorage-object-is-undefined-in-ie) will help you . – Swati Dec 21 '20 at 11:57

1 Answers1

1

What Swati said. Looks like you don't know about sessions yet. If you do it with Java, create Session at the servlet.

HttpSession session = request.getSession();
session.setAttribute("attributeName", value);
response.sendRedirect("your.jsp");