[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 402
  • Last Modified:

scriplet in jsp does not work

Dear experts
I have this code in my java action class which works fine

 BufferedReader in =new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
               StringBuffer lines = new StringBuffer();
               String linefeed = "\n";              
               while((line = in.readLine()) != null) {
                 lines.append(line);
                 lines.append(linefeed);
               }
                       
               System.out.println(lines.toString());
               HttpSession session = request.getSession();
               session.setAttribute("result",in);

***** System.out.println(lines.toString()); works fine and i can see the result in the console. but i want to print the
same result in the jsp page but its not working . I am setting "in"(BufferedReader) in session and calling in jsp

******I have the above code as a scriplet in my  jsp page

<div>

<%
  StringBuffer lines = new StringBuffer();
if(session.getAttribute("result") != null)
{
  out.println("111111");
BufferedReader in = (BufferedReader)session.getAttribute("reportresult");


               String line;
               String linefeed = "\n";              
               while(line = in.readLine() != null)
{
                 
                 out.println("22222");
                 lines.append(line);
                  out.println(line);
                 out.println(lines.toString());
                 lines.append(linefeed);
               }
}  
%>

</div>
In the above jsp scriplet I can see 111111  BUT 22222  is not getting displayed at all since  
while(line = in.readLine() != null)   is not getting executed but the same works fine in java program and shows result in
console


any help greatly appreciated
J
0
jaggernat
Asked:
jaggernat
2 Solutions
 
rama_krishna580Commented:
Hi,

you should be doing this...

are you doing like below example ?

Socket s = new Socket(..)
session.setAttribute("socket", s);
session.setAttribute("socketIn", new BufferedReader(new InputStreamReader(s.getInputStream())));
session.setAttribute("socketOut", new PrintWriter(new OutputStreamWriter(s.getOutputStream())));

BufferedReader in = (BufferedReader)session.getAttribute("socketIn");
PrintWriter out = (PrintWriter)session.getAttribute("socketOut");

R.K
0
 
jaggernatAuthor Commented:
No

I am just creating instance of BufferedReader

BufferedReader  in =new BufferedReader(new InputStreamReader(connection.getInputStream()));

setting "in"   in session and trying to call that in jsp page ,but unable to do it


this segment scriplet of code  does not work in jsp

BufferedReader in = (BufferedReader)session.getAttribute("result");  //WORKS


               String line;
               String linefeed = "\n";              
               while(line = in.readLine() != null) // DOES NOT WORK
{
....
}
0
 
objectsCommented:
>      session.setAttribute("result",in);
> BufferedReader in = (BufferedReader)session.getAttribute("reportresult");

firstly you are using the wrong name to get the reader,you set it as result and retrieve it as reportresult

Secondly you have already read the complete conntents of your stream in yoiur action so there is nothing left to read, it is already at eof.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now