0

Am throwing exception from my servlet and in the eclipse console its printing the exception logs whenever exception is occured. But am trying to show that exception in JSP pages that am not getting.

Please find my code below.

code to pass the request :

function sendRequest( functionCallback, servletLocation, queryString)
{
    var asyncRequest = newXMLRequest();

    // Set the handler function to receive callback notifications from the request object
    var handleResponse = getReadyStateHandler(asyncRequest, functionCallback);
    asyncRequest.onreadystatechange = handleResponse;
    
    // Send a POST to servlet for information.  Third parameter specifies request is asynchronous.
    asyncRequest.open("POST", servletLocation, true);

    asyncRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
    asyncRequest.send(queryString);
    
    if ( document.getElementById("filterMessage") != null )
    {
        document.getElementById("filterMessage").innerHTML = "<span class = 'red'>Filtering...Please Wait</span>";  
    }
}

code to handle the respose :

function getReadyStateHandler(req, responseXmlHandler) 
{

    // Return an anonymous function that listens to the 
    // XMLHttpRequest instance
    return function () 
    {
        // If the request's status is "complete"
        if (req.readyState == 4)
        {           
            // Check that a successful server response was received
            if (req.status == 200) 
            {
                // Pass the XML payload of the response to the 
                // handler function
                responseXmlHandler(req.responseXML);
            }
            else 
            {
                // An HTTP problem has occurred
                alert("HTTP error: " + req.status);
            }
        }
    }
}



function reloadPage() {
    window.location.reload(true);
}

invoking servlet call from the below code.

function updateProgramVersion(e) {
    var washoutIdStr = (e.target || e.srcElement ).parentNode.parentNode.parentNode.children.sparWashoutId.children.washoutItem.value;
    var programVersionStr = (e.target || e.srcElement ).parentNode.parentNode.parentNode.children.programVersionModify.children.programVersion.value;
    var sparNumber = (e.target || e.srcElement ).parentNode.parentNode.parentNode.children.sparNumber.children.sparNumber.value
    
    if( (e.target || e.srcElement ).id == 'programVersionUpdatebtn') {
        query = 'actionId=updateProgramVersion&washoutIdStr='+washoutIdStr+"&programVersion="+ programVersionStr+"&sparNumber="+sparNumber;
        servlet = "<%=UrlBuilder.getServletRoot() + ApplicationConstants.SERVLET_REPORT_SPAR%>"; method="POST";
        sendRequest(reloadPage, servlet, query);   // servlet call
        (e.target || e.srcElement ).parentNode.parentNode.parentNode.children.programVersion.style.display = 'none';
        (e.target || e.srcElement ).parentNode.parentNode.parentNode.children.programVersionTD.style.display = 'block';
    }
    
}

Please find my below servlet code :

    else if(actionId.equals("updateProgramVersion")) {
        updateProgramVersion(washoutId, sparNumber,  programVersion);
        //nextPage = mappings.findForward("display");
    }

private void updateProgramVersion(String washoutId, String sparNumber,  String programVersion) throws ApplicationException{
    
    boolean isExist = sparwashoutService.getProgramVersion(washoutId, sparNumber, programVersion);
    
    if(isExist) {
        sparwashoutService.updateProgramVersion(washoutId, sparNumber, programVersion);
    } else {
        throw new InvalidInputException("Version number is not valid","Version number is not valid",this.getClass().toString().substring( getClass().toString().lastIndexOf(".") + 1 ) + ".performTask()");
    }
    
}

1 Answer 1

1

You need to send back custom message which you need to show in jsp via servlet using response.getWriter().write().. also you can set status so that it will not enter inside if (req.status == 200) {... Here is example with try-catch block modify below code according to your requirement .

Servlet Code :

  try
  {
    //check some condition
    response.setContentType ("text/xml");
    response.setCharacterEncoding ("UTF-8");
    response.setStatus(200);    //set status
    response.getWriter().write(yourxmldata);    //send message
  }
  //handling the exception  
  catch (Exception e)
  {
    response.setContentType ("text/plain");//set contenttype to text
    response.setCharacterEncoding ("UTF-8");
    response.setStatus(406);    //set status
    response.getWriter().write (e.getMessage () + "I AM IN EXECPETION");    //get your execption message

  }

and in Ajax just check the status code :

if (req.status == 200)  {
        responseXmlHandler(req.responseXML);//xml return
 } else if(req.status == 406){
       alert(req.responseText);//text return
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. i was stuck in this for more than 3 days.. Thanks for your valuable suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.