0

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 -

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "w3.org/TR/html4/loose.dtd">; 
<html>
  <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
    <title>Welcome</title> 
  </head> 
  <body> Welcome !
    <form action="main.jsp" method="POST"> 
      <input type="submit" value="Proceed" /> 
    </form> 
  </body> 
 </html>  

main.jsp

 <html> 
   <body> Main Page 
     <form method="post" action="CallingServlet"> 
       <input type="submit" name="button1" value="Check Services" /><br> 
     </form> 
   </body> 
 </html> 

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <display-name>DashBoard_ISAAC</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
        <servlet>
        <servlet-name>CallingServlet</servlet-name>
        <servlet-class>DashboardISAAC.CallingServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>CallingServlet</servlet-name>
        <url-pattern>/CallingServlet</url-pattern>
    </servlet-mapping>
</web-app>

CallingServlet.java

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);
    }

}

AutomatedTelnetClient

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();
        }
    }
}
user2906736
  • 1
  • 1
  • 4

1 Answers1

0

You are trying to call servlet class CallingServlet from main.jsp.
There is problem in action attribute of your form tag You need to change from,

<form method="post" action="\CallingServlet">
                            ↑

to

<form method="post" action="CallingServlet">  

After changing, if you still face problem then please post web.xml

Update

After looking at web.xml make one thing sure, your CallingServlet is in DashboardISAAC package.

 package DashboardISAAC;
 public class CallingServlet extends HttpServlet
 {
   ...
   .....
 }  

There is no need of import class

<%@ page import="DashboardISAAC.AutomatedTelnetClient"%> 

in main.jsp because you want to call Java class from Servlet not from JSP.

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
  • Hi Aniket, i tried changing main.jsp as mentioned above , but still getting Cannot display Webpage error. i have added web.xml – user2906736 Oct 23 '13 at 08:02
  • You should also include the context path in the action or remove the /. – Alexandre Lavoie Oct 23 '13 at 08:09
  • i have added Servlet class code as well...and all the above code is now per the suggestions provided so far, but still getting same error. – user2906736 Oct 23 '13 at 10:27
  • @user2906736: I ran the code on my local machine. it is running perfectly fine. I have just one line in `doPost()` method `System.out.println("inside calling servlet dopost !");`. It gets printed on the log. I think you have problem with class `AutomatedTelnetClient` or there is something wrong with deployment server. Tell me where are you deploying/ running application. Just restart the server see if it works. – Aniket Kulkarni Oct 23 '13 at 10:42
  • Thanks for this..i am trying to run in eclipse 3.2 using internal web browser. I tried running Tomcat v6.0 server , but its not getting established, stating main class not found. I have attached AutomatedTelnetClient class as well...if you can please check that as well and help me out with this issue. – user2906736 Oct 24 '13 at 05:52
  • Sorry! I do not know much about `TelnetClient` class. But error `main class not found` is related path variable. can you post the stacktrace where is exact error. – Aniket Kulkarni Oct 24 '13 at 06:43
  • When i run TelnetClient class individually without using JSP / servlets, i used to get output properly on console. And below is the error when i try to start Tomcat server - – user2906736 Oct 24 '13 at 07:50
  • See these links. You are compiling java class on one version and running on another [1](http://stackoverflow.com/q/2466828/1031945)[2](http://www.mkyong.com/java/javalangunsupportedclassversionerror-bad-version-number-in-class-file/)[3](http://www.coderanch.com/t/518459/JBoss/Caused-java-lang-UnsupportedClassVersionError-Bad) – Aniket Kulkarni Oct 24 '13 at 08:40
  • You are using tomcat 6, minimum [jdk version supported is 5](http://tomcat.apache.org/tomcat-6.0-doc/building.html). The problem is either you have set wrong path variable or using different version at compile time and different version at run time. I would suggest, you upgrade to jdk 6 that is better, recreate the project in eclipse. Before upgrading clean the project,server and try to run. Check path variable is not pointing to another version. – Aniket Kulkarni Oct 24 '13 at 10:10
  • Hey Aniket...thanks for your help..it has helped me to some extent...i am now able to establish /start Tomcat server. But now with a new error of " HTTP Status 404 " -description The requested resource is not available. ...when i am running Welcome.jsp using server. – user2906736 Oct 28 '13 at 05:40
  • web.xml is as above..i am not sure on what wrong is with it...can you please help me on where is it incorrect ??? – user2906736 Oct 28 '13 at 06:05
  • i am just running it on server and its automatically taking it as - http://localhost:8080/DashBoard_ISAAC/WEB-INF/classes/DashboardISAAC/Welcome.jsp – user2906736 Oct 28 '13 at 06:12
  • ok, got it. is your project name `CheckISAAC_final`? if yes then see the URL. http://localhost:8080/CheckISAAC_final/Welcome.jsp , see how URL is prepared http://server_address:Webserver_port_no/Project_name/JSP_file_name this is the format. – Aniket Kulkarni Oct 28 '13 at 06:28
  • No my project is DashBoard_ISAAC only. I tried the above format still no luck with it – user2906736 Oct 28 '13 at 06:35
  • Can you post new question or update the above code with updated code that you have, because from above `web.xml` the tag `CheckISAAC_final` tells that your project name is `CheckISAAC_final` not `DashBoard_ISAAC`. – Aniket Kulkarni Oct 28 '13 at 06:41
  • Code is ok. Now, can you see the web server welcome page hit this URL `http://localhost:8080/` Tell me. Your code is working fine on my machine. Just clean the project, clean webserver and restart from eclipse – Aniket Kulkarni Oct 28 '13 at 06:52
  • Yes..i think that's the problem ..even "http://localhost:8080/" is not working and giving 404 error..what can be done to resolve this? – user2906736 Oct 28 '13 at 07:11
  • re-install tomcat. If there is port issue then you can install tomcat on different ports such as 9090, 8089 etc. If you have oracle installed then oracle also uses the same port 8080. – Aniket Kulkarni Oct 28 '13 at 07:19