请求的资源不可用于默认jsp的输入

时间:2014-12-21 17:24:39

标签: jsp servlets

请求的资源不可用

我在Eclipse for Java EE中有一个项目作为动态Web应用程序。一切都已设置,默认页面正确加载。在它上面我有一个带输入的表格。当我点击它来执行基于输入的操作时,我得到一个错误。 Startup servlet应该读取输入并处理它,然后调用servlet Class1中的doPost。然后doPost调用sort和outputSort函数。 outputSort函数写入.jsp文件,然后返回到doPost,它应该显示它。这是我的第一个Java EE应用程序,所以我确定我做错了一些事情。我正在将完整的Java桌面应用程序移植到Web应用程序中。我有更多的函数和类变量,但我只在这里展示了相关的变量。如果我可以让这个工作,剩下的就是轻松移植整个应用程序。这是我的应用程序的基础知识,所以我想让它先工作。

这是错误:

HTTP Status 404 - /MyProject/Startup

type Status report

message /MyProject/Startup

description The requested resource is not available.

Apache Tomcat/7.0.47

这是我的相关代码。我认为问题是Class1中的readnums函数打开并读取输入文本文件。我不认为它被发现了。我应该把它放在哪里,我应该如何在代码中访问它?任何有关这方面的帮助将不胜感激。提前谢谢。

的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>MyProject</display-name>
  <welcome-file-list>
    <welcome-file>index.html</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>
   <display-name>Startup</display-name>
   <servlet-name>Startup</servlet-name>
   <servlet-class>com.MyProject.Startup</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>
  <servlet-mapping>
    <servlet-name>Startup</servlet-name>
    <url-pattern>/MyProject*</url-pattern>
 </servlet-mapping>

</web-app>

Startup.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="UTF-8"%>
<!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>MyProject</title>
</head>
<body>
<h2>Enter the number of sorts:</h2>
<form action="Startup" method="Post">
    Enter your number of sorts: <input type="text" name="sorts" size="20">
    <br><br>
    <input type="submit" name="action" value="Class1">
    <input type="submit" name="action" value="Class2">
</form>
</body>
</html>

启动servlet

package com.MyProject;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Startup extends HttpServlet {

    /**
     * 
     */


    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        req.getRequestDispatcher("/Startup.jsp").forward(req,resp);



    }

    protected void doPost(HttpServletRequest req,
            HttpServletResponse response) throws ServletException, IOException {

        String sorts = req.getParameter("sorts");

        String action = req.getParameter("action");


        if ("Class1".equals(action)) {

            Class1 p = new Class1();

            p.sortInputText = sorts;

            p.PB_operation = "sorts";

            p.doPost (req, response);
        }


    // I didn't define the else if yet because I'm just trying to get Class1 working first.

    else if ("Class2".equals(action)) {

            // Invoke SecondServlet's job here.
    }

    }
}

的Class1

package com.MyProject;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class CLass1 extends HttpServlet {


    String errorMessage;
    String sortInputText;
    String PB_operation;


    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest req,
         HttpServletResponse response) throws ServletException, IOException {


        if (PB_operation == "sorts") {

            readnums();

            if (!errorMessage.isEmpty())  {


                req.getRequestDispatcher("/errormessage1.jsp").forward(req,response);

                return;


            }

            else {

                sort();

                outputSort();

                req.getRequestDispatcher("/class1sorts.jsp").forward(req,response);

            }

        }

      }


    void readnums() throws FileNotFoundException, IOException {

        int i,j;
        String Temp;
        String [] Temp2 = new String [80];

        getClass().getResource("/myfile.txt");

        String path;

        // Read the numbers

        inputfilefound = true;

        path = "/myfile.txt";

            File file_check = new File (path);

        if (!file_check.exists()) {

                errorMessage = "Input file not found";

            return;

        }


        File f = new File(path);

        FileReader fr = new FileReader(f);

        BufferedReader br = null;

        br = new BufferedReader (fr);

        while ((Temp = br.readLine()) != null) {

        // Read data into variables

        }

        br.close();
    }

}

编辑: Class1中的outputSort方法。方法中未定义的所有变量都是在类中定义的,但是我没有在这里或其他方面列出它们,试图保持不太长的东西。

void outputSort () throws IOException  {

        String path, Temp;
        int i;

        path = "Class1sorts.jsp";

        BufferedWriter bw = null;

        bw = new BufferedWriter(new FileWriter(new File(path), false));

        Temp = "<%@ page language=\"java\" contentType=\"text/html; charset=ISO-8859-1\" pageEncoding=\"UTF-8\"%>";
        bw.write(Temp);
        bw.newLine();

        Temp = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
        bw.write(Temp);
        bw.newLine();

        Temp = "<html>";
        bw.write(Temp);
        bw.newLine();

        Temp = "<head>";
        bw.write(Temp);
        bw.newLine();

        Temp = "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">";
        bw.write(Temp);
        bw.newLine();

        Temp = "<title>MyProject</title>";
        bw.write(Temp);
        bw.newLine();

        Temp = "</head>";
        bw.write(Temp);
        bw.newLine();

        Temp = "<body>";
        bw.write(Temp);
        bw.newLine();

        Temp = "<h2>The sorted totals for the range selected</h2>";
        bw.write(Temp);
        bw.newLine();

        Temp = "<form action=\"Startup\" method=\"Post\">";
        bw.write(Temp);
        bw.newLine();

        Temp = "Go back to prevoius page: ";
        bw.write(Temp);
        bw.newLine();

        Temp = "<br><br>";
        bw.write(Temp);
        bw.newLine();

        Temp = " <input type=\"submit\" name=\"action\" value=\"Go Back to prvious page\">";
        bw.write(Temp);
        bw.newLine();

        Temp = "</form>";
        bw.write(Temp);
        bw.newLine();

        Temp = "<pre>";
        bw.write(Temp);
        bw.newLine();

        Temp = "Rank     Numbers     Totals       XNumber     Totals";
        bw.write(Temp);
        bw.newLine();
        bw.newLine();

        for (i = 1; i <= NUMLIMIT; i++)  {

            Temp =  Integer.toString(i);

            if (i < 10)

                Temp += " ";

            Temp +=  Integer.toString(sortnums[i][1]) + "     ";
            Temp +=  Integer.toString(sortnums[i][2]) + "     ";

            if (i <= XLIMIT) {

                Temp +=  Integer.toString(sortxball[i][1]) + "     ";
                Temp +=  Integer.toString(sortxball[i][2]) + "     ";

            }

            bw.write(Temp);
            bw.newLine();


        }

        Temp = "</pre>";

        Temp = "</body>";

        Temp = "</html>";

    bw.close();

}

编辑:目录结构。在这里,您可以看到我的项目的真实名称。我试图保持私密,因为它的性质。但它将是一个免费,简单的网络应用程序,而不是为了盈利。我会付钱给自己托管。声明。

enter image description here

2 个答案:

答案 0 :(得分:2)

HTTP Status 404 - /MyProject/Startup

此错误告诉您无法找到针对请求URL映射的资源。 另外,

  

/ * MyProject的

这意味着它被映射到类似:

  

的http:// HOST:PORT / PROJECTNAME / MyProject的

其中PROJECTNAME是其部署的目录名称或您设置的上下文。

如果您希望将servlet映射为:

  

的http:// HOST:PORT / PROJECTNAME / STARTUP

你应该把它映射成:

<servlet-mapping>
    <servlet-name>Startup</servlet-name>
    <url-pattern>/*</url-pattern>
 </servlet-mapping>

<servlet-mapping>
        <servlet-name>Startup</servlet-name>
        <url-pattern>/Startup</url-pattern>
     </servlet-mapping>

答案 1 :(得分:0)

我通过将'@Override'与doGet和doPost一起使用并将我的Sartup.jsp放在WEB-INFO文件夹中解决了我的问题。这是代码:

package com.LAEWeb;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public  class Startup extends HttpServlet {

    private static final long serialVersionUID = 1L;


    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        req.getRequestDispatcher("/WEB-INF/Startup.jsp").forward(req,resp);

    }

    @Override
    public void doPost(HttpServletRequest req,
            HttpServletResponse resp) throws ServletException, IOException {


        String sorts = req.getParameter("sorts");

        String action = req.getParameter("action");


        if ("Powerball".equals(action)) {

            Powerball p = new Powerball();

            p.sortInputText = sorts;

            p.doPost (req, resp);
        }

         else if ("Mega Millions".equals(action)) {

            // Invoke SecondServlet's job here.
        }

    }
}

编辑:显然,Startup.jsp也必须在WebContent文件夹中。这是为了允许它在web.xml中的欢迎文件列表中看到。