json - Send a List object from a Java Servlet to JSP using the PrintWriter object? -


before proceeding realise there's similar question (passing list servlet jsp) couple of years ago. realise possible set list i'm trying pass session attribute out of curiosity wondered if it's possible use printwriter object send data jsp page.

jsp

<script type="text/javascript">         function getengineschemes(engineid) {             $.get('schemetypeservlet', {                 action: "getschemes",                 engineid: engineid             },             function(data, status){              }).fail(function() {                 alert("error obtaining schemes engine engine id: " + engineid);             });         }     </script> </body>  

servlet

@override protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {     //set type of response     response.setcontenttype("text/html");     response.setcharacterencoding("utf-8");     //get parameters may passed client determine methods should called     string action = request.getparameter("action");      if(action.equalsignorecase("getschemesforengine")) {          integer engineid = integer.parseint(request.getparameter("engineid"));         list<string> schemenames = getschemenamesforengine(engineid);          response.getwriter(). //insert code send object jsp         response.getwriter().flush();     }        

options

one solution think may reasonable create jsonobject , have along lines of

response.setcontenttype("application/json"); jsonobject json = new jsonobject();      for(string name : schemenames) {             json.put(engineid, name);          }     response.getwriter().write(json.tojsonstring());     response.getwriter().flush(); 

i haven't tested above code want know if seems best solution problem or if i'm making overly complicated? maybe there's far simpler solution question.

i think having things messed in head. first of all, cannot sent text or kind of data servlet jsp. proper way use session , use response.sendredirect or requestdispatcher.forward methods. ignore calls made before servlet's writer object.


Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -