How to call a servlet using jquery ajax
Description : This is a small example to call servlet using ajax jquery. also shows servlet mapping in web.xml file (Deployment descriptor) .How to use external javascript file in java[ajax servlet Tutorial].
web.xml
<servlet>
<description></description>
<display-name>AjaxServlet</display-name>
<servlet-name>AjaxServlet</servlet-name>
<servlet-class>servlets.AjaxServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AjaxServlet</servlet-name>
<url-pattern>/AjaxServlet</url-pattern>
</servlet-mapping>
<welcome-file>index.jsp</welcome-file>
index.jsp
jquery api needed
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript" src="myJquery.js"></script>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<div>
<input type="text" id="headText">
<input type="button" id="newButton" value="sendData">
</div>
servlet : AjaxServlet.java
public class AjaxServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AjaxServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(" inside do post "+request.getParameter("message"));
}
}
jquery file : myjquery.js
$(document).ready(function(){
$('#newButton').click(function(){
sendData();
});
});
function sendData(){
var mge = $('#newText').val();
alert(mge);
$.ajax({
type: "POST",
url: "AjaxServlet",
data: { message : mge }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
}
Description : The Example show how to call a servlet from jsp using ajax jquery. Data from jsp page will pass to servlet as parameter through ajax call
To receive response from the servlet, Change the code to following
In servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String message = request.getParameter("message");
String respMess = message + "data saved ";
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(respMess);
}
In jquey
The function sendData() will send data to post method of servlet "JqueryServlet". the data is taken from html tag whose id is " headText ". In the example i have created a text box named headtext in Index.jsp (java server page)function sendData(){
var mge = $('#headText').val();
$.ajax({
type: "POST",
url: "JQueryServlets",
data: { message : mge},
success : function(data){
alert('success'+data);
}
});
}
Thanking you for reading this post. Hope you will try the code...
Your feedback may help rest of the readers... so please, your valuable comments
Now take a look how cascading style sheet is applied in struts, that is a constant look and feel for all your web pages in your web application . >>> lazy java
Sunday Times 1 Bestseller New York Times 1 Bestseller the global bestseller - Origin is the latest Robert Langdon novel from the author of the Da Vinci Code. 'Fans will not be disappointed' the Times Robert Langdon, Harvard professor of symbology and religious iconology, arrives at the Guggenheim Museum Bilbao to attend the unveiling of an astonishing scientific breakthrough. The evening’s host is billionaire Edmond Kirsch, a futurist whose dazzling high-tech inventions and audacious predictions have made him a controversial figure around the world. But Langdon and several hundred guests are left reeling when the meticulously orchestrated evening is suddenly blown apart. There is a real danger that Kirsch’s precious discovery may be lost in the ensuing chaos. With his life under threat, Langdon is forced into a desperate bid to escape Bilbao, taking with him the museum’s director, Ambra Vidal. Together they flee to Barcelona on a perilous quest to locate a cryptic password that will unlock Kirsch’s secret. To evade a devious enemy who is one step ahead of them at every turn, Langdon and Vidal must navigate the labyrinthine passageways of extreme religion and hidden history. On a trail marked only by enigmatic symbols and elusive modern art, Langdon and Vidal will come face-to-face with a breathtaking truth that has remained buried – until now. ‘Dan Brown is the master of the intellectual cliffhanger’ Wall Street Journal ‘As engaging a hero as you could wish for’ Mail on Sunday ‘For anyone who wants more brain-food than thrillers normally provide’ Sunday Times
By Now Hurry- origin by dan brown
Author : +belazy
This is the third time I've been to your site. Thnx for explaining more information.
ReplyDeleteLook at my web blog : Screen Printing Melbourne
Are you a developer or not?
DeleteHey there Adam, merely accomplished, on hand plus Simon from the Webinar.
ReplyDeletemy page - Jquery ajax example
Please, replace the code
ReplyDeletevar mge = $('#headText').val();
by
var mge = $('#newText').val();
Thanking you...
500+ visitors for this post
800 + visitors for this post
ReplyDeleteDo you mind if I quote a few of your articles as long as I provide credit
ReplyDeleteand sources back to your weblog? My blog is in the exact same area of interest
as yours and my users would certainly benefit from
a lot of the information you provide here. Please let me know if this alright with you.
Many thanks!
my web site - watch la liga online
1000 ++ visitors for this post ...
ReplyDeleteThanks all
i followed the same steps and it didn't worked.
ReplyDeletewhat err did you got?
DeleteThe examples is a working one rohit..
Dude i have a class not found exception i kept in default package
DeleteHey i have a class not found exception i placed it in default package
DeleteRecompile it... Are you using eclipse ide, then build the project and run
Deletemagnificent submіt, ѵeгy infoгmаtive.
ReplyDeleteI ωonder why the otheг ехpertѕ оf this sector do not unԁеrѕtand thіs.
Үou must cоntіnue youг wгіting.
Ι am suгe, you hаve a huge readeгѕ' base already!
Feel free to visit my web page; buy twitter followers cheap
2500 + visitor
ReplyDeleteThanks all
belazy in fb :
ReplyDeletehttp://www.facebook.com/belazzy
Hmm is anyone else experiencing problems with the images on
ReplyDeletethis blog loading? I'm trying to find out if its a problem on my end or if it's the blog.
Any suggestions would be greatly appreciated.
Here is my website Shower Head Filter
Do you have a spam problem on this blog; I also am a blogger, and I
ReplyDeletewas wondering your situation; we have developed some nice
procedures and we are looking to trade solutions with other folks, be sure to shoot me an e-mail if interested.
Have a look at my page: Lift and Glow Pro Review
I like the valuable info you provide in your articles.
ReplyDeleteI will bookmark your blog and check again here regularly.
I'm quite sure I will learn a lot of new stuff right here! Best of luck for the next!
Feel free to surf to my web-site younger look
@stone : can you ping me at belazy1987@gmail.com, I ll send you the whole src code...
ReplyDeleteBelazy
Hello, I do think your blog might be having browser compatibility issues.
ReplyDeleteWhenever I take a look at your website in Safari, it looks fine however when opening in I.
E., it's got some overlapping issues. I just wanted to provide you with a quick heads up! Aside from that, wonderful blog!
My blog; fetish cams