我试图从JSP调用一个Servlet方法,而JSP又调用一个简单的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>
<html>
<body> Main Page
<form method="post" action="CallingServlet">
<input type="submit" name="button1" value="Check Services" /><br>
</form>
</body>
</html>
<?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>
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();
}
}
}
答案 0 :(得分:0)
您正尝试从CallingServlet
调用servlet类main.jsp
表单action
的{{1}}属性存在问题
你需要改变,
tag
到
<form method="post" action="\CallingServlet">
↑
更改后,如果您仍然遇到问题,请发布<form method="post" action="CallingServlet">
在查看web.xml
后确定一件事,web.xml
在CallingServlet
包中。
DashboardISAAC
不需要导入类
package DashboardISAAC;
public class CallingServlet extends HttpServlet
{
...
.....
}
<{1>}中的因为你想从Servlet调用Java类而不是从JSP调用Java类。