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.