0

I'am making a website that communicate with a web server. and in the servlet I'am using http request to send queries and store it in my session. i need values that will be coming from the web server and i dont want to put it to my session. Is it ok to communicate with the web server using scriptlet?

<%
Gson gson=new Gson();
//send query to web server
 Httprequest request=new Httprequest("query");
Object[] obj=gson.fromJson(request,obj[].class);
%>


<select>
<%
for(int i=0;i<obj.length;i++){
out.println("<option>"+obj[i]+"</option>");
}
%>
</select>
the bosxcz
  • 91
  • 3
  • 13
  • **Please** don't do that. The JSP becomes a servlet, so your question is confusing because the JSP is on the web server. – Elliott Frisch Jan 07 '14 at 04:45
  • @ElliottFrisch sorry if it is confusing, my web server is a separate project to be specific a project using glashfish server. and now i am making a website using tomcat server and sending query eg.("getUSERNAME") and i just need only the response of the server i dont want the list of usernames to last as long as the user is active i just need the list of usernames for a certain page to add to the select tag.should i really need to put it to my session so i cant access it?or there is any other way? – the bosxcz Jan 07 '14 at 04:49
  • I would not retrieve the values with a scritplet, I might make an http call from a Servlet in Tomcat to the backend glassfish server (but that's really just a RPC mechanism). – Elliott Frisch Jan 07 '14 at 04:52
  • so my only option to get the values retrieved from the glassfish server is to put it in my session? – the bosxcz Jan 07 '14 at 04:56
  • 1
    No. You could add it to the [request scope](http://javapapers.com/jsp/explain-the-scope-of-jsp-objects/). – Elliott Frisch Jan 07 '14 at 04:57
  • @ElliottFrisch May I know why you said this *Please don't do that. The JSP becomes a servlet* ? All jsp's are converted to servlet in web server – SpringLearner Jan 07 '14 at 04:59
  • http://stackoverflow.com/q/3177733/1031945 – Aniket Kulkarni Jan 07 '14 at 05:01
  • @JqueryLearner Yes. Yes they are. – Elliott Frisch Jan 07 '14 at 05:02
  • @ElliottFrisch I am asking why that statement – SpringLearner Jan 07 '14 at 05:06
  • @ElliotFrisch what would be the difference between request.setAttribute("key",object) and request.getSession().setAttribute("key",object);? – the bosxcz Jan 07 '14 at 05:20
  • Here you are binding the object to the request scope, so it will be available only with the particular request . And here request.getSession().setAttribute("key",object); the object is in the session so you can get the object throughout the application session (i.e apps lifetime ) – Santhosh Jan 07 '14 at 05:23
  • does it means the attribute is dedicated to a certain page? thanks for a quick answer – the bosxcz Jan 07 '14 at 05:34
  • Its quite different . Once the object is retrieved from the request it may have PAGE scope – Santhosh Jan 07 '14 at 06:07
  • The "JSP becomes a servlet" comment can mean among other things, real servlets have Try..Catch statements because you have to put them in or it won't compile. JSP allows you to skip that. What happens if you do something in JSP that can fail (like connecting to database, doing a HTTP request to another webserver)? Without a Try..Catch the error thrown might cause your code to be made visible to the user depending on how your server is setup. A real servlet never does that. – developerwjk Jan 07 '14 at 18:44

0 Answers0