我想实现一个servlet并在WebApp中调用它。
我经常从Apache Tomcat Server获得java.lang.ClassCastException: Servlet.Telnet cannot be cast to javax.servlet.Servlet
。我确保我的课程延伸HttpServlet
这是我的代码:
package Servlet;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.net.telnet.TelnetClient;
public class Servlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
TelnetClient telnet = new TelnetClient();
telnet.connect(request.getParameter("router"), 23);
PrintStream output = new PrintStream(telnet.getOutputStream());
output.println(request.getParameter("login"));
output.flush();
output.println(request.getParameter("password"));
output.flush();
out.printf("SUCCESS");
telnet.disconnect();
} catch (Exception e) {
e.printStackTrace();
out.printf("ERROR");
}
}
答案 0 :(得分:0)
我重命名了包和类,它完美无缺。