How to redirect the response of a servlet into the same jsp page from where we got the request

28,022

In your servlet, rather than printing the output of util directly to the response, hold it in memory and pass it to the jsp:

String stat;
StringBuffer utilOutput = new StringBuffer();
while((stat = input.readLine()) != null){
    utilOutput.append(stat + "\n");
}
req.setAttribute("utilOutput", utilOutput.toString());
req.getRequestDispatcher("/Example/status.jsp").forward(req, res);

In your jsp, I assume you have a div or something where you want to see the output:

<div id= "status" style="display:none;">
    <form action="/status" method="POST">
        <input type="submit" name="showstatus" id="txtSubmit" value="Status" />
    </form>
</div>
<div id="result">
    <pre>
        ${requestScope.utilOutput}
    </pre>
</div>

Notice that I added a forward slash in the form action. If your user is submitting the form from /Example/status.jsp, then the relative path would have the post going to /Example/status, but your servlet only handles requests to /status.

Your request object, req, has a map of attributes, and you can put any relevant objects in it using the setAttribute() method on the request. The request dispatcher forwards your request and response objects on to another resource (your jsp) for processing. The jsp can access these values by name, using the EL notation above.

The display:none in your inline style will always prevent that form from being seen on the page if it loads that way. I think the most apparent way to display it would be with javascript on the client side. You'd need to change the display value when the tab is clicked. You don't mention what your tab elements are, but let's say they're divs. You could write a javascript function to load the form, or you could even do it all inline:

<div id="mytab" onclick="document.getElementById('status').style.display = 'block';">Load the form</div>
Share:
28,022
user2266817
Author by

user2266817

Updated on July 06, 2022

Comments

  • user2266817
    user2266817 almost 2 years

    How can I redirect a response of a servlet to the same jsp page from where I get the request. Suppose I have a tab called status on my jsp page http://localhost:8080/Example/status.jsp. Now when I send a request and when I get a response,it should display the response on the same page, ie. it should show response on
    http://localhost:8080/Example/status.jsp. But it is showing me the response in
    http://localhost:8080/Example/Statuswhere Status is the url-pattern in web.xml file. Please any help is appreciated. below is my code. How do I get the response in status.jsp.

    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws 
    IOException {
    
                 try{
    
                     String showstatus =req.getParameter("showstatus");
                     PrintWriter out = res.getWriter();   
                     Runtime rt = Runtime.getRuntime();
                     Process pr = rt.exec("C:\\tools\\server\\util stat -a" );
                     BufferedReader stdInput = new BufferedReader(new   
    InputStreamReader(pr.getInputStream()));
    
                    BufferedReader input = new BufferedReader(stdInput);
                    String stat = "";
                        while((stat = input.readLine()) != null){
                            out.println(stat);
                        }
                 }
                    catch (Throwable t)  
                      {  
                        t.printStackTrace();  
                      }  
    
    
                    finally {  
    
                    }  }
    

    JSP CODE:

    <div id= "status" style="display:none;">
                <FORM action="status" METHOD="POST">
                    <input type=submit name=showstatus 
    id=txtSubmit value=Status />
    
                </FORM>
            </div>
    

    WEB.XML:

    <servlet>       
        <servlet-name>Status</servlet-name>
        <servlet-class>com.emc.clp.license.Status</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Status</servlet-name>
        <url-pattern>/status</url-pattern>
    </servlet-mapping>
    
    • Tap
      Tap over 10 years
      Do you want status.jsp to process the form post? If so, you need to change the form action to /Example/status.jsp. As it is now, the request is going to be handled by the servlet because your form action is just status.
    • user2266817
      user2266817 over 10 years
      @Tap: Thanks for the quick reply. I don't have status.jsp.Do I need to create one?
    • cowls
      cowls over 10 years
      You dont have status.jsp?! In your question: "Suppose I have a tab called status on my jsp page localhost:8080/Example/status.jsp"
    • user2266817
      user2266817 over 10 years
      oops sorry, it was my bad. I do have status.jsp.I changed the form action as per above, it shows the url as http:....Example/status.jsp but the page is blank. there is no response in it
    • user2266817
      user2266817 over 10 years
      Please help me in resolving this issue.
    • Tap
      Tap over 10 years
      So your goal is to show the user the output of the util command that you're running on the server?
    • user2266817
      user2266817 over 10 years
      @Tap : Yes, please help me in understandin how to do this.
  • user2266817
    user2266817 over 10 years
    :Thanks so much Tap. It worked. You saved my day.Could you please explain me how this worked.I really appreciate it.But still there is one issue. If am using <div id= "status" style="display:none;"> then the result is not getting displayed,its blank. If I remove style="display:none;" the result get displayed. So I will not be able to click this from a tab. Its giving a seperate button. My idea was to have a list of tabs in a webpage and when I click each tab the response of respective tabs should be displayed on the same page.
  • user2266817
    user2266817 over 10 years
    :Thanks again. The above suggestion did work using <div id="mytab" onclick="document.getElementById('status').style.display = 'block';">Load the form</div>. But still it has an issue. Suppose I have the following tabs on my webpage-start/stop, version, viewlog, status. when I click individual tabs it is displaying the result but the issue is suppose if I click tab status it is diplaying the status button and when i click the button it displays the result of status on webpage but along with it, it is also displaying the buttons of the remaining tabs which should not happen.
  • user2266817
    user2266817 over 10 years
    I mean if when I click a particular tab it should show me the result of only that tab and it should not show me any information of other tabs which is happening now.
  • Tap
    Tap over 10 years
    That becomes a very different question. There are libraries out there to help with this - check out jquery ui tabs. This may be overkill for your project, but the basic idea is to have a function that shows the clicked tab's content and hides the content of all the other tabs. There's lots of good information on the many ways to approach the problem.