1

I have created a dynamic web project in eclipse called testWarNotMaven. I have created an index.html file in the web content folder with the following code

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>File Upload</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <h2>Add Questions</h2>
        <form method="POST" action="/upload" enctype="multipart/form-data" >
            File:
            <input type="file" name="file" id="file" /> <br/>
            <input type="submit" value="Upload" name="upload" id="upload" />
        </form>
    </body>
</html>

I then created a servlet class that begins with the following

@WebServlet("/upload")
@MultipartConfig
public class UploadServlet extends HttpServlet {

When i deployed this project by deploying the EAR project to the server, the index.html page loaded. However when i clicked on the upload button to take me to the servlet class there is

HTTP Status 404 error. The requested resource is not available.

1) I have researched this issue and have come across that the servlet class should be compiled and in the WEB-INF/classes folder. I only have the .java files in the Java Resources/src/. I have no classes folder and no .class files. Why is that and do i need them?

2) When i run the project on the server i am directed to http://localhost:8080/testWarNotMaven/ and when i click the form upload button i am directed to http://localhost:8080/upload. I think i am missing understanding in how the context - root works. I understand that the URL is http://localhost:8080//. Why is the context root missing when the upload button is pressed?

3)Or why else is it that the servlet is not being reached?

EDIT:

I have added a screenshot of enter image description here

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Y.Sas
  • 43
  • 9
  • *How* did you deploy this to the server? And yes, you probably do need something prepending the servlet context path to your `form` `action` value, dynamically or otherwise. – nitind Aug 27 '19 at 20:34
  • have you defined explicit web.xml? – Kris Aug 28 '19 at 06:47
  • @nitind I tried to deploy it be exporting the project as a WAR file and pasting it into payara5\glassfish\domains\domain1\autodeploy. But then when i tried to access http://localhost:8080/testWarNotMaven/ the HTTP 404 error came up. So then i tried to run on server by adding the testWarNotMavenEAR project to the server and i was then able to access http://localhost:8080/testWarNotMaven/ – Y.Sas Aug 28 '19 at 06:48
  • @Kris. I use web annotation instead. The web.xml has been left default. I couldn't come across anything i needed to add. Is there anything to be added? – Y.Sas Aug 28 '19 at 06:53

2 Answers2

2

The project may not be build in Eclipse yet. Please check if there are any build path issues or dependencies missing. The classes will be inside the build folder under the project.

halfer
  • 19,824
  • 17
  • 99
  • 186
MRTJ
  • 141
  • 7
  • There are no build path issues or dependencies missing and the build folder is empty! – Y.Sas Aug 27 '19 at 19:19
  • If the build folder is empty then most likely the project is not build. Usually this will be listed in the Problems view (Windows -> Show View -> Problems). If you could inculde the screenshot of the project structure, that might also help – MRTJ Aug 27 '19 at 22:43
  • I have added a screenshot of the project structure and there are no problems as can also been seen in the screenshot. – Y.Sas Aug 28 '19 at 06:34
  • i had originally looked at Problems, i now noticed an Error Log which i had a look and shows errors saying that Promblem occured when invoking code from plug-in: "org.eclipse.jst.jee" Java.lang.NullPointerException. Do you know what that could mean? – Y.Sas Aug 28 '19 at 08:13
  • I would check 3 things here. 1) The servlet version in web.xml. Whether it is 3.0 or above. 2) Clean and manually build project in Eclipse (Project -> Build Project. Please check whether Build Automatically is enabled or not) 3) Project Facets configuration in Eclipse and verify the JEE version If that doesn't help could you please share a screenshot of the Problems tab here? – MRTJ Aug 28 '19 at 16:48
  • Also please remove the preceding "/" in you form action. This will submit directly to the action not to the context `
    '
    – MRTJ Aug 28 '19 at 16:55
  • It is now working and removing the / sent it to the right URL – Y.Sas Aug 29 '19 at 08:24
  • Was that the only fix required? – MRTJ Aug 29 '19 at 16:33
  • 1
    I also removed it from the server and then deployed it again – Y.Sas Sep 01 '19 at 07:34
1

Make changes in your index file like:

<form method="POST" action="upload" enctype="multipart/form-data" >

Remove / from action="upload"

Correct URL should go like this:

http://localhost:8080/testWarNotMaven/

http://localhost:8080/testWarNotMaven/upload
ErShakirAnsari
  • 653
  • 7
  • 19