java - get servlet attribute without loading current jsp page -



java - get servlet attribute without loading current jsp page -

on servlet have code :

processrequest(...,...){ string page = ""; if(true){ string str = "hello"; request.setattribute("str",str); page = "currentpage#"; } else{ page = "otherpage"; } requestdispatcher rd = requestdispatcher("/",page); }

on jsp, servlet attribute,i utilize :

<input type = "text" value = "<c out>${str}</c out>">

this happens,

when utilize currentpage# current page not load, input tag have value of null,

but if used currentpage(without #), "hello" value page loads,which not want happen because page , contents refresh.

can help me str attribute without loading currentpage or there way?

you load part of page using ajax. made easier using jquery

for ex ,

jsp:

<script src="http://code.jquery.com/jquery-1.10.2.js" type="text/javascript"></script> <script src="js/app-ajax.js" type="text/javascript"></script> <script> $(document).ready(function() { $('#username').blur(function() { $.ajax({ url : 'getuserservlet', tpye: "post", info : { username : $('#username').val() }, success : function(responsetext) { $('#ajaxgetuserservletresponse').text(responsetext); } }); }); }); </script> </head> <body> <form> come in name: <input type="text" id="username" /> </form> <br> <br> <strong>ajax response</strong>: <div id="ajaxgetuserservletresponse"></div>

and in servlet,

string username = request.getparameter("username").trim(); if(username == null || "".equals(username)){ username = "guest"; } string greetings = "hello " + username; response.setcontenttype("text/plain"); response.getwriter().write(greetings);

on submitting in jsp , ajaxgetuserservletresponse refreshed presenting info server .

learn more

java javascript jquery jsp servlets

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -