Hi

I am not been able to receive URL parameters passed from my ajax code in my jsp.

Here is the code

function showDetails()
  {
	  document.detailsForm.sltdUser.value = selectedUserID;
	  alert('selectedUserID' + selectedUserID);


	  var httpRequest;
	  var url  = 'showDetails.jsp?sltdUser = ' + selectedUserID;

      if (window.XMLHttpRequest) { // Mozilla, Safari, ...
          httpRequest = new XMLHttpRequest();
          
      } 
      else if (window.ActiveXObject) { // IE
          try {
              httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
          } 
          catch (e) {
              try {
                  httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
              } 
              catch (e) {}
          }
      }

      if (!httpRequest) {
          alert('Giving up :( Cannot create an XMLHTTP instance');
          return false;
      }
     
      httpRequest.open('GET',url, true);
      httpRequest.onreadystatechange = function() { processResponse(httpRequest); };
      httpRequest.send(null);
	  
  }

  function processResponse(httpRequest) {

      if (httpRequest.readyState == 4) {
          if (httpRequest.status == 200) {
              alert(httpRequest.responseText);
          } else {
              alert('There was a problem with the request.');
          }
      }

  }

showDetails.jsp

<%
     String userId = request.getParameter("sltdUser");
   %>
   
   <script type="text/javascript">
     alert('userId' + userId);
   </script>

The problem is in the alert I see that userId is not taking the value passed.

Kindly help.

Thanks

Abhik

Dani AI

Generated

Short diagnosis and fix (ties to the thread)

The symptom here is a malformed query string: the AJAX URL in the original code included spaces around the equals sign, so the server actually received a different parameter name (for example a trailing space in the key) and request.getParameter("sltdUser") returned null. JSP runs on the server when the request arrives, so the server-side getParameter only sees exactly the names/values sent with the HTTP request. (jakarta.ee)

Two safe ways to build the GET URL from JavaScript (always URL-encode values):

/* encode the value yourself */
var url = "showDetails.jsp?sltdUser=" + encodeURIComponent(selectedUserID);

or using URLSearchParams:

var params = new URLSearchParams({ sltdUser: selectedUserID });
var url = "showDetails.jsp?" + params.toString();

encodeURIComponent prevents embedded &, =, spaces, etc. from breaking the query string; URLSearchParams builds and percent-encodes the whole query string for you. (developer.mozilla.org)

Quick debugging checklist (use these instead of appending a parameter "twice")

  • Log the final URL before sending: console.log(url) and inspect the request in the browser Network panel to confirm the exact Request URL. (developer.chrome.com)
  • On the server side, print the raw query string to confirm what the servlet container saw (helps spot stray spaces or encoding issues), for example:
<% out.println("raw query: " + request.getQueryString()); %>

getQueryString() returns the un-decoded query portion the container received. If that string does not contain the expected key exactly, getParameter(...) will be null. (docs.oracle.com)

Notes: there is no need to append the same parameter twice; simply construct a correct, encoded query string (or POST the data) and the JSP request.getParameter("sltdUser") will contain the value. Mentioned posts from and show the right ideas (server-side code runs first / check the request), but the root cause here was the malformed URL and missing encoding.

Recommended Answers

All 3 Replies

One cant use client side code to get the server side code, if u try printing out the userId it should be there. what u can do is assign the userId to a hidden field then alert the value of the hidden field.

I have already solved this and u CAN send client side code to server side. The parameter needs to be appended to the URL twice.

so why isnt it marked as solved then?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.