title: Not able to call Servlet methods from JSP
body: <p>I am trying to call a Servlet method from a JSP which in turn calls a simple Java Class Method..But i get get output as Cannot display the Webpage. I have added web.xml and CallingServlet class. Its in DashBoardISAAC package and i tried removing '/' also. Still no luck with that.All the Updated code is as below -</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" "w3.org/TR/html4/loose.dtd"&gt;; 
  &lt;html&gt;
    &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;  
      &lt;title&gt;Welcome&lt;/title&gt; 
    &lt;/head&gt; 
    &lt;body&gt; Welcome !
      &lt;form action="main.jsp" method="POST"&gt; 
        &lt;input type="submit" value="Proceed" /&gt; 
      &lt;/form&gt; 
    &lt;/body&gt; 
   &lt;/html&gt;  
  </code></pre>
  
  <h2>main.jsp</h2>
  
  <pre><code> &lt;html&gt; 
     &lt;body&gt; Main Page 
       &lt;form method="post" action="CallingServlet"&gt; 
         &lt;input type="submit" name="button1" value="Check Services" /&gt;&lt;br&gt; 
       &lt;/form&gt; 
     &lt;/body&gt; 
   &lt;/html&gt; 
  </code></pre>
  
  <h2>web.xml</h2>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"&gt;
    &lt;display-name&gt;DashBoard_ISAAC&lt;/display-name&gt;
    &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;
      &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.html&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.htm&lt;/welcome-file&gt;
      &lt;welcome-file&gt;default.jsp&lt;/welcome-file&gt;
    &lt;/welcome-file-list&gt;
          &lt;servlet&gt;
          &lt;servlet-name&gt;CallingServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;DashboardISAAC.CallingServlet&lt;/servlet-class&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;CallingServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/CallingServlet&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <h2>CallingServlet.java</h2>
  
  <pre><code>package DashboardISAAC;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  //import javax.servlet.annotation.*;
  //@WebServlet(description = "Calling servlet", urlPatterns = {"/CallingServlet" })
  public class CallingServlet extends HttpServlet {
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
          System.out.println("inside calling servlet dopost !");
          AutomatedTelnetClient telnet1 = new AutomatedTelnetClient (Hostname,USername,password);
          System.out.println("inside calling servlet dopost !");
          if (request.getParameter("button1") != null) {
              telnet1.sendCommand("vtc ping \"Object\" \"/\" \"\" 5");
          } else {
              // ???
          }
  
          request.getRequestDispatcher("/Success.jsp").forward(request, response);
      }
  
  }
  </code></pre>
  
  <h2>AutomatedTelnetClient</h2>
  
  <pre><code>package DashboardISAAC;
  import org.apache.commons.net.telnet.TelnetClient;
  
  import java.io.InputStream;
  import java.io.PrintStream;
  
  public class AutomatedTelnetClient {
      private TelnetClient telnet = new TelnetClient();
      private InputStream in;
      private PrintStream out;
      private String prompt = "$";
  
      public AutomatedTelnetClient(String server, String user, String password) {
          try {
              // Connect to the specified server
              telnet.connect(server, 23);
  
              // Get input and output stream references
              in = telnet.getInputStream();
              out = new PrintStream(telnet.getOutputStream());
  
              // Log the user on
              readUntil("Username: ");
              write(user);
              readUntil("Password: ");
              write(password);
  
              // Advance to a prompt
              readUntil(prompt + " ");
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public void su(String password) {
          try {
              write("su");
              readUntil("Password: ");
              write(password);
              prompt = "$";
              readUntil(prompt + " ");
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public String readUntil(String pattern) {
          try {
              int a=pattern.length()-1;
              char lastChar = pattern.charAt(a);
              //char lastChar = pattern.charAt(pattern.length() - 1);
              StringBuffer sb = new StringBuffer();
              //boolean found = false;
              char ch = (char) in.read();
              while (true) {
                  System.out.print(ch);
                  sb.append(ch);
                  if (ch == lastChar) {
                      if (sb.toString().endsWith(pattern)) {
                          return sb.toString();
                      }
                  }
                  ch = (char) in.read();
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
          return null;
      }
  
      public void write(String value) {
          try {
              out.println(value);
              out.flush();
              System.out.println(value);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public String sendCommand(String command) {
          try {
              System.out.println();
              write(command);
              return readUntil(prompt + " ");
          } catch (Exception e) {
              e.printStackTrace();
          }
          return null;
      }
  
      public void disconnect() {
          try {
              telnet.disconnect();
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  
      public static void main(String[] args) {
          try {
              /// main method
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>