title: Open a text file with jsp button click and java
body: <p>am asking this question after some research. Am trying to open a text file located in my local maching with java and jsp. ie when I click a button in jsp it should open the text file for me. Could some one please help me on this.</p>
  
  <p>Hi, Here is my code:</p>
  
  <pre><code>import java.awt.Desktop;
  import java.io.File;
  
  public class Start extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {         
      try {         
        if ((new File("C:\\Debug\\log20.txt")).exists()) {     
                  Process p = Runtime
                     .getRuntime()
                     .exec("C:\\Debug\\log20.txt");
                  p.waitFor();
  
              } else {
  
                  System.out.println("File is not exists");
  
              }
  
              System.out.println("Done");
  
            } catch (Exception ex) {
              ex.printStackTrace();
            }
  
          }
      }
  </code></pre>
  
  <p>Web.xml:</p>
  
  <pre><code>&lt;servlet&gt;       
      &lt;servlet-name&gt;LogFile&lt;/servlet-name&gt;
      &lt;servlet-class&gt;com.abc.def.LogFile&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;LogFile&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/logfile&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>JSP: </p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01   
  Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  
  
  &lt;% String status=""; %&gt;
  
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/&gt;
  &lt;title&gt;EMC eLicensing&lt;/title&gt;
  &lt;link rel="stylesheet" type="text/css" href="css/css_ngoe/headerDefault.css" /&gt;
  &lt;link rel="stylesheet" type="text/css" href="css/css_ngoe/bodyTemplate.css" /&gt;
  &lt;link rel="stylesheet" type="text/css" href="css/css_ngoe/footer.css" /&gt;
  &lt;link rel="stylesheet" type="text/css" href="css/css_ngoe/helperClasses.css" /&gt;
  &lt;link rel="stylesheet" type="text/css" href="css/css_ngoe/railPanel.css" /&gt;
  &lt;link rel="stylesheet" type="text/css" href="css/css_ngoe/buttons.css" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  
  &lt;!-- Header Start --&gt;
  &lt;div class="parentheader"&gt;
      &lt;div id="header"&gt;
          &lt;h1&gt;
              &lt;a href="#home" title="E"&gt;E&lt;/a&gt;
          &lt;/h1&gt;
          &lt;div id="header-text-position"&gt;
              &lt;div id="header-text"&gt;&lt;a     
  href="home.jsp"&gt;&lt;/a&gt;&lt;/div&gt;
              &lt;br /&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;!-- Header End --&gt;
  
  &lt;br&gt;
  &lt;br&gt;
  
  &lt;table width=100% cellpadding=0 cellspacing=0 border=0&gt;
      &lt;tr&gt;
          &lt;td&gt;&amp;nbsp;&amp;nbsp;&lt;/td&gt;
          &lt;!-- START CONTENT --&gt;
          &lt;td&gt;
              &lt;P&gt;
              lmgrd options
              &lt;BR&gt;
              &lt;BR&gt;
              &lt;FORM action="start" METHOD="GET"&gt;
  
                              &lt;table   
  cellspacing="5" cellpadding="1" border="0"&gt;
                                  &lt;tr&gt;
                                      &lt;td&gt;Start/Stop/Reread&lt;/td&gt;
                                      &lt;td&gt;&lt;input type=submit name=txtSubmit1 id=txtSubmit value="Start" /&gt;&lt;/td&gt;
                                  &lt;/tr&gt;
  
                              &lt;/table&gt;
  
                  &lt;/FORM&gt;
  
                  &lt;FORM action="stop" METHOD="GET"&gt;
  
                              &lt;table 
  cellspacing="5" cellpadding="1" border="0"&gt;
                                  &lt;tr&gt;
                                      &lt;td&gt;Start/Stop/Reread&lt;/td&gt;
                                      &lt;td&gt;&lt;input type=submit name=txtSubmit2 id=txtSubmit value="Stop" /&gt;&lt;/td&gt;
                                  &lt;/tr&gt;
  
                              &lt;/table&gt;
  
                  &lt;/FORM&gt;
  
                  &lt;FORM action="logfile" METHOD="GET"&gt;
  
                              &lt;table 
  cellspacing="5" cellpadding="1" border="0"&gt;
                                  &lt;tr&gt;
                                      &lt;td&gt;LOGFILE&lt;/td&gt;
                                      &lt;td&gt;&lt;input type=submit name=txtSubmit3 id=txtSubmit value=LOG /&gt;&lt;/td&gt;
                                  &lt;/tr&gt;
  
                              &lt;/table&gt;
  
                  &lt;/FORM&gt;
                  &lt;form action="start" method="GET"  
  enctype="multipart/form-data"&gt;
  
                          &lt;input type="file" name="file" 
  value=text /&gt;
                          &lt;input type="submit" /&gt;
  
  &lt;/form&gt;
                  &lt;form action="status" method=GET&gt;   
  
                  &lt;BR&gt;
                  &lt;BR&gt;
                  &lt;input type=submit name=Submit id=txtSubmit  
  value=Status&gt;
              &lt;/form&gt;
              &lt;BR&gt;
              status: &lt;%=status %&gt;
                  &lt;p&gt;
              &lt;p&gt;
              &lt;p&gt; 
          &lt;/td&gt;
      &lt;/tr&gt;   
  &lt;/table&gt;
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>