0

The requested resource is not available

I have a project in Eclipse for Java EE as a Dynamic Web Application. Everything is set up and the default page loads correctly. On it I have a form with an input. When I click on it to perform an operation based on the input I get an error. The Startup servlet is supposed to read the input and process it and then call the doPost in the servlet Class1. Then the doPost calls the sort and outputSort functions. The outputSort function writes to a .jsp file, and then return to doPost, which is supposed to display it. This is my first Java EE app so I've sure I've done some things wrong. I'm porting a full Java desktop app to a web app. I have many more functions and class variables but I only showed the relevant ones here. If I can get this working the rest would be easy porting the entire app. This is the basics of my app so I wanted to get it working first.

Here is the error:

HTTP Status 404 - /MyProject/Startup

type Status report

message /MyProject/Startup

description The requested resource is not available.

Apache Tomcat/7.0.47

Here is my relevant code. I think the problem is the readnums function in Class1 opens and reads an input text file. I don't think it's being found. Where should I put it and how should I access it in the code? Any help on this would be greatly appreciated. Thanks in advance.

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>

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

}

Edit: outputSort method in Class1. All variables not defined in the method are defined in the class, but I didn't list them here or otherwise, to try to keep things not too long winded.

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

}

Edit: Directory structure. Here you can see the true name of my project. I was trying to keep it private because of the nature of it. But it will be a free, simple web app, not for profit. I'll pay for hosting myself. Disclaimer.

enter image description here

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
te7
  • 601
  • 1
  • 6
  • 23
  • What's your project directory structure? – SSC Dec 21 '14 at 18:14
  • I don't have a Resources folder. Should I add one and where? The directory structure is in the edit to the original post. Thanks for replying. – te7 Dec 21 '14 at 19:19

2 Answers2

2
HTTP Status 404 - /MyProject/Startup

This error tells you that it can not find the resource mapped against the request URL. Plus,

/MyProject*

This means it is mapped to something like:

http://HOST:PORT/PROJECTNAME/MyProject

where PROJECTNAME is the directory name with which it's deployed or the context you have set.

If you want your servlet to be mapped like:

http://HOST:PORT/PROJECTNAME/STARTUP

you should map it like:

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

or

<servlet-mapping>
        <servlet-name>Startup</servlet-name>
        <url-pattern>/Startup</url-pattern>
     </servlet-mapping>
SSC
  • 2,956
  • 3
  • 27
  • 43
  • OK I'll try that. Thanks for replying. See the edit to my original post for the name of my project. – te7 Dec 21 '14 at 19:21
  • Yes so with the current project hierarchy and the servlet mapping do: http://localhost:8080/LAEWeb/MyProject This will call your servlet's doGet method but when you will hit the form it will invoke doPost as you mentioned in your
    – SSC Dec 21 '14 at 19:24
  • I changed the to the second option you listed.../Startup...but I can't access Startup at localhost. I can run the Startup.jsp, but I guess it doesn't find the Startup servlet when I click the button to input the form. I guess that's the problem all along. How to fix this? – te7 Dec 21 '14 at 19:40
  • I get popup errors about the server's conflicting ports..saying they're in use sometimes...could this be related? – te7 Dec 21 '14 at 19:49
  • First you should stop the tomcat. – SSC Dec 21 '14 at 19:54
  • and what URL do you try to hit, paste url here – SSC Dec 21 '14 at 19:55
  • I stopped and restarted tomcat...now I can access the default page at http://localhost:8080/LAEWeb/Startup. But when I click the button on the form it has an error.."HTTP Method POST is not supported by this url...what does this mean? – te7 Dec 21 '14 at 20:10
  • change method ="post" (see all small) in
    of ur jsp
    – SSC Dec 21 '14 at 20:11
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/67428/discussion-between-mr-777-and-te7). – SSC Dec 21 '14 at 20:11
0

I solved my problem by using the '@Override' with doGet and doPost and putting my Sartup.jsp in the WEB-INFO folder. Here's the code:

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

    }
}

Edit: Apparently Startup.jsp has to be in WebContent folder also. This is to allow it to be seen in the welcome file list in the web.xml.

te7
  • 601
  • 1
  • 6
  • 23