0

我正在尝试从 JSP 调用 Servlet 方法,该方法又调用简单的 Java 类方法。但我得到的输出是无法显示网页。我添加了 web.xml 和 CallingServlet 类。它在 DashBoardISAAC 包中,我也尝试删除“/”。仍然没有运气。所有更新的代码如下 -

<%@ 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>  

主.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>

调用Servlet.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);
    }

}

自动远程登录客户端

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

1 回答 1

0

您正在尝试从 调用 servletCallingServletmain.jsp。您的表单属性
有问题 您需要更改,actiontag

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

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

更改后,如果您仍然遇到问题,请发布web.xml

更新

在查看web.xml确定一件事后,您的CallingServlet已在DashboardISAAC包装中。

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

不需要导入类

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

因为main.jsp您想从 Servlet 而不是从 JSP 调用 Java 类。

于 2013-10-22T12:21:52.917 回答