Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP/Servlet How to identify if the HTTP request came from an ajax request

I am using JQuery-Ajax, and History.js for my web app, is there anyway I can identify if the request came from an AJAX request or just normal request? What I aim to do is something like this

<%if(isRequestAjax()){%>
<jsp:include page="/views/header.jsp" />        
}
<%}%>

<div id ="profilePage>
    <!-- user profile will be loaded here !-->
</div>

I am also using Struts2 as may MVC framework

like image 590
user962206 Avatar asked Dec 22 '12 17:12

user962206


People also ask

Is AJAX an HTTP request?

An AJAX request is a request made by an AJAX application. Typically, it is an HTTP request made by (browser-resident) Javascript that uses XML to encode the request data and/or response data.

Can we use AJAX in JSP?

To create ajax example, you need to use any server-side language e.g. Servlet, JSP, PHP, ASP.Net etc.

How does an AJAX call work?

How AJAX Calls Work. AJAX uses both a browser built-in XMLHttpRequest object to get data from the web server and JavaScript and HTML DOM to display that content to the user. Despite the name “AJAX” these calls can also transport data as plain text or JSON instead of XML.


1 Answers

You have to check for a request header X-Requested-With it will be XMLHttpRequest

  if ("XMLHttpRequest".equals(req.getHeader("X-Requested-With")) ){
           // include other file
  } 
like image 187
Subin Sebastian Avatar answered Sep 28 '22 22:09

Subin Sebastian