0

I'm trying to retrieve the value in JSP which has been submitted via XHR request in HTML

Below is the HTML code for reference

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery.js">
</script>
<script>
function click() {
    var xhr = null;
    if (navigator.appName == 'Microsoft Internet Explorer') {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    } else if (navigator.appName == 'Netscape') {
        xhr = new XMLHttpRequest();
    }
    xhr.open('POST', "verify.jsp", true);
    var params = 'empIds=' + '123';
    xhr.onreadystatechange = function() {
        if (xhr.readyState == XMLHttpRequest.DONE) {
            alert(xhr.responseText);

        }
    };
    xhr.send(params);
}
</script>
</head>
<body>
<p><button onclick="click()" type="button">Click</button></p>

Below is the JSP Page which retrieves the value submitted

<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<HTML>
<HEAD>   
</HEAD>
<BODY>
<BR>
<%
  long timeInMilliSeconds = System.currentTimeMillis();
  String empId = request.getParameter("empIds");
  out.print("emp Id: "+empId);
%>
</BODY>
</HTML>

Actual Result: empId: 
Expected Result:empId: 123

JSP should retrieve the value and send the same value back to HTML

Reason: String empId = request.getParameter("empIds"); is returning null.

Thanks

web dev
  • 1
  • 1
  • Not sure why your server does not get the parameter. BUT you load jQuery, why not [use it](https://api.jquery.com/jquery.post/) instead of a decades old script? `function click() { $.post("verify.jsp", { "empIds": "123" }, function(response) { console.log(response) }); }` – mplungjan May 11 '22 at 05:39
  • @mplungjan It needs to work in IE6 version. Does your above code works in IE6 as well ? – web dev May 11 '22 at 05:50
  • IE6?! No it likely won't. Why would anyone still need IE? And even XP can run IE8 (which also cannot run the above) But you load jQuery - that would break IE6-10 or so – mplungjan May 11 '22 at 05:52
  • @mplungjan pls help me how to support in IE6 version? – web dev May 11 '22 at 06:44
  • Sorry. I have no way to help you with that. I have not have IE6 on my machine for almost two decades. `March 4, 2011, Microsoft urged web users to stop using IE6 in favor of newer versions of Internet Explorer. ` – mplungjan May 11 '22 at 07:06
  • @mplungjan even with jquery i am getting the same response...as null – web dev May 11 '22 at 09:49
  • So look in the network console to see if you actually call the jsp correctly. – mplungjan May 11 '22 at 09:50
  • Have a look here: https://stackoverflow.com/questions/15570734/how-to-get-http-post-parameter-in-jsp – mplungjan May 11 '22 at 09:52
  • @mplungjan The above link doesn't solve the problem. My problem is simple, the value being passed via XHR request is not able to retrieve in jsp. Can you please advise any changes needs to be done in web.xml or any changes in server which solves this? – web dev May 11 '22 at 10:18
  • Sorry, no. I only do JS – mplungjan May 11 '22 at 10:24

0 Answers0