繁体   English   中英

收到HTTP Status 404错误,我的网页无法正常工作

[英]Getting HTTP Status 404 error and my web page is not working

我的ExecuteScript.java类

    package scriptOnJsp;

    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.util.HashMap;
    import java.util.Map;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.swing.JOptionPane;

    import com.jcraft.jsch.Channel;
    import com.jcraft.jsch.ChannelExec;
    import com.jcraft.jsch.ChannelSftp;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.JSchException;
    import com.jcraft.jsch.Session;
    import com.jcraft.jsch.SftpATTRS;
    import com.jcraft.jsch.SftpException;

    //@WebServlet("/ExecuteScript")
    public class ExecuteScript extends HttpServlet {

        private static final long serialVersionUID = 1L;

        Session session = null;
        Channel channel = null;
        ChannelSftp channelSftp = null;

        HashMap<String, String> hashMap = new HashMap<String, String>();

        HashMap<String, HashMap<String, String>> hashingMap = new HashMap<String, 

                                                            HashMap<String, String>>();
            MapValue keyValue = new MapValue();

        String scriptResult;
        String host;
        String password;
        String command = "ls";
        String executeDirectory = "/t3/envs/wlppp4/";

        public ExecuteScript() {
            super();

        }

        /*
         * protected void doGet(HttpServletRequest request, HttpServletResponse
         * response) throws ServletException, IOException {
         * response.setContentType("text/html"); PrintWriter printWriter =
         * response.getWriter(); printWriter.println("<h1>ExecuteScript</h1>"); }
         */

        protected void processRequest(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, 
                                                                          IOException {
            // Get user_name and pass_word from the JSP page
            host = request.getParameter("host_name");
            String password = request.getParameter("pass_word"); // Print the 
                                                                                 above

                      // got values in

                               // console
            System.out.println("The username is" + host);
            System.out.println("\nand the password is" + password);
            request.setAttribute("dd", keyValue);
        }

        public void connect() {
            try {
                JSch jsch = new JSch();
                session = jsch.getSession("sftp_t3", host, 22);
                session.setPassword(password);
                session.setConfig("StrictHostKeyChecking", "no");
                java.util.Properties config = new java.util.Properties();
                session.setConfig(config);
                session.connect();
                channel = session.openChannel("sftp");
                channel.connect();
                channelSftp = (ChannelSftp) channel;
                channelSftp.cd(executeDirectory);

                executeCommand(command);
                System.out.println("Executed :)");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        public void executeCommand(String script) throws JSchException,
                IOException, SftpException {
            System.out.println("Execute sudo");
            String sudo_pass = "test";
            ChannelExec channel = (ChannelExec) session.openChannel("exec");
            Channel channelTemp = (ChannelSftp) session.openChannel("sftp");

            ((ChannelExec) channel).setCommand(script);

            InputStream in = channel.getInputStream();
            OutputStream out = channel.getOutputStream();
            ((ChannelExec) channel).setErrStream(System.err);
            channel.connect();
            out.write((sudo_pass + "\n").getBytes());
            out.flush();

            byte[] tmp = new byte[1024];
            while (true) {
                while (in.available() > 0) {
                    int i = in.read(tmp, 0, 1024);
                    if (i < 0)
                        break;
                    scriptResult = new String(tmp, 0, i);
                    String[] strArray = scriptResult.split("\n");

                    for (String temp : strArray) {
                        int var = temp.indexOf(":");
                        String arr = temp.substring(0, var);
                        String arry = temp.substring(var, 
                                                                        temp.length());
                        hashMap.put(arr, arry);
                    }

                    for (Map.Entry<String, String> pairs : 
                                                                 hashMap.entrySet()) {
                        System.out.println("" + pairs.getKey() + 
                                                                     pairs.getValue());
                    }

                    keyValue.setMinDays(hashMap.get("Last password 
                                                                             change"));
                    keyValue.setPassExpires(hashMap.get("Password 
                                                                             expires"));
                    keyValue.setPassInactive(hashMap.get("Password 
                                                                            inactive"));
                    keyValue.setAccExpires(hashMap.get("Account 
                                                                            expires"));
                    keyValue.setMinDays(hashMap
                            .get("Minimum number of days between 
                                                                     password change"));
                    keyValue.setMaxDays(hashMap
                            .get("Maximum number of days between 
                                                                     password change"));
                    keyValue.setNoWarnings(hashMap
                            .get("Number of days of warning 
                                                             before password expires"));

                    hashingMap.put(host, hashMap);

                    for (Map.Entry<String, HashMap<String, String>> 
                                                                  newPairs : hashingMap
                            .entrySet()) {
                        System.out.println("Set 1: \n" + 
                                                             newPairs.getKey()
                                    + newPairs.getValue());
                        for (Map.Entry<String, String> newPairsTemp 
                                                                              : newPairs
                                .getValue().entrySet()) {
                            System.out.println("Set 2: \n" + 
                                                                   newPairsTemp.getKey()
                                    + 
                                                               newPairsTemp.getValue());
                        }
                    }

                    System.out.print("Data are: " + scriptResult);

                }
                if (channel.isClosed()) {
                    System.out.println("exit-status: " + 
                                                               channel.getExitStatus());
                    break;
                }
                try {
                    Thread.sleep(1000);
                } catch (Exception ee) {
                    System.out.println(ee);
                }
            }
            channel.disconnect();
            System.out.println("Sudo disconnect");
        }

        public void disconnect() {
            session.disconnect();
        }

        public void Openpage(HttpServletResponse res) throws IOException {
            // here type your JSP page that you want to open
            res.sendRedirect("host.jsp");
        }

        /*
         * public static void main(String... args) throws JSchException,
         * IOException, SftpException {
         * 
         * ExecuteScript escript = new ExecuteScript();
         * 
         * //escript.processRequest(request, response); escript.connect();
         * escript.disconnect(); }
         */
    }

我的bean类-MapValue.java

   package scriptOnJsp;

   public class MapValue {

    private String minDays;
    private String passInactive;
    private String lastPassChange;
    private String maxDays;
    private String passExpires;
    private String accExpires;
    private String noWarnings;

    public String getMinDays() {
        return minDays;
    }

    public void setMinDays(String minDays) {
        this.minDays = minDays;
    }

    public String getPassInactive() {
        return passInactive;
    }

    public void setPassInactive(String passInactive) {
        this.passInactive = passInactive;
    }

    public String getLastPassChange() {
        return lastPassChange;
    }

    public void setLastPassChange(String lastPassChange) {
        this.lastPassChange = lastPassChange;
    }

    public String getMaxDays() {
        return maxDays;
    }

    public void setMaxDays(String maxDays) {
        this.maxDays = maxDays;
    }

    public String getPassExpires() {
        return passExpires;
    }

    public void setPassExpires(String passExpires) {
        this.passExpires = passExpires;
    }

    public String getAccExpires() {
        return accExpires;
    }

    public void setAccExpires(String accExpires) {
        this.accExpires = accExpires;
    }

    public String getNoWarnings() {
        return noWarnings;
    }

    public void setNoWarnings(String noWarnings) {
        this.noWarnings = noWarnings;
    }

   }

我的jsp-host.jsp

   <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
                                              "http://www.w3.org/TR/html4/loose.dtd">
   <html>
   <head>
   <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>Host details</title>
   </head>
   <body>
    <form name=host action="SaveUser" method="Post">
        Host name: <input type="text" name="host_name"> <br />
        Password: <input type="password" name="pass_word" /> <input
            type="submit" value="Submit" />
    </form>

   </body>
   </html>

   > My 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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                                    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>Scriptingpage</display-name>
    <welcome-file-list>
        <welcome-file>host.jsp</welcome-file>
    </welcome-file-list>
   </web-app>

我需要获取主机名上的用户entery并将其获取到java类中,对其进行操作并将ezxecuted查询显示为键值对。 在这种情况下,我什至无法显示我的jsp页面。 请帮助我。

如果出现404错误,则表示您放置在地址栏上的URL不存在。 在检查代码之前,请确保已将文件上传到服务器的正确位置,并且已为浏览器指定了正确的路径。

您需要在web.xml为servlet添加条目,如下所示:

<?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"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
                                    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>Scriptingpage</display-name>
        <servlet>
        <servlet-name>ExecuteScript</servlet-name>
        <servlet-class>PackageName.ExecuteScript</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ExecuteScript</servlet-name>
        <url-pattern>/ExecuteScript</url-pattern>
    </servlet-mapping>     
    <welcome-file-list>
        <welcome-file>host.jsp</welcome-file>
    </welcome-file-list>
   </web-app>

在您的jsp中,

<form name=host action="/ExecuteScript" method="Post">
    Host name: <input type="text" name="host_name"> <br />
    Password: <input type="password" name="pass_word" /> <input
        type="submit" value="Submit" />
</form>

实际使用的url应该与web.xml提供的url模式匹配

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM