I saw answers relating to this question and tried to solve still I am getting null in Servlet, where I am doing mistake?? Perhaps I am missing something in Javascript or in jsp??
<head>
<script type="text/javascript" src="layout/styles/jquery-latest.min.js"></script>
<script type="text/javascript">
function callMe(){
$.ajax({
type: "POST",
url: "/NewServlet",
data: { methodToInvoke: "sayHello" , data: "4" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
}
</script>
I want to pass the value 4 to servlet(doPost in NewServlet.java) from home.jsp
<a href="NewServlet?count=4" onclick="callMe()" id="4" >HTML Images</a>
String t= request.getParameter("count");
out.println(t);// should display 4, but getting null here
Put the count=4
in the url of your ajax request
function callMe(count){
$.ajax({
type: "POST",
url: "/NewServlet?count=" + count,
data: { methodToInvoke: "sayHello" , data: "4" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
}
and in your jsp:
<a onclick="callMe(4)" id="4" >HTML Images</a>
Edit : To send the count back to the webbrowser, do:
PrintWriter out = response.getWriter();
out.println(t);
where response is a HttpServletResponse
.
See here to get started
您的参数名称是“ count”而不是“ param1” ..因此,它应该是:
String t = request.getParameter("count");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.