First You need a JQuery Plugin i.e., jquery-1.8.2.js and add the jquery file to your project and your page
=================================================================
=================================================================
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<display-name>JqueryServlet</display-name>
<servlet-name>JqueryServlet</servlet-name>
<servlet-class>com.JqueryServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JqueryServlet</servlet-name>
<url-pattern>/JqueryServlet</url-pattern>
</servlet-mapping>
<welcome-file>index.jsp</welcome-file>
</web-app>
index.jsp
<html>
<head>
<title>AJAX JQuery in Java Web Application</title>
<script src="jquery-1.8.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#submitData').click(function(){
sendData();
});
});
function sendData(){
var name = $('#username').val();
var pwd = $('#password').val();
$.ajax({
type: "POST",
url: "AjaxjqueryServlet",
data: { userName : name , password : pwd }
});
}
</script>
<style type="text/css">
.centered{
width:100%;
margin-left:auto;
margin-right:auto;
text-align:center;
}
</style>
</head>
<body>
<form method="post" >
<div class="centered">
<h2 style="text-align:center;">AJAX jquery Servlet in Java Web Application</h2>
Name : <input type="text" id="username"/><br/>
Password :<input type="password" id="password" /><br/>
<input type="submit" value="Submit" id="submitData"/><br/>
</div>
</form>
</body>
</html>
Servlet: AjaxJqueryRequest
package com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JqueryServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {
String userName = request.getParameter("userName");
System.out.println("userName:"+userName);
String password = request.getParameter("password");
System.out.println("password:"+password );
}
}
No comments:
Post a Comment