8

I want to upload a file to a server, for which I am writing a servlet program.The location of the directory where the document would be uploaded should be fetched from a parameter in web.xml. I have not use web.xml before and only know that it makes entries for each servlet. I am not able to see this file in my web application project that i am making in netbeans. Please help me with this. Thank you.

Saumyaraj
  • 1,220
  • 3
  • 15
  • 37

6 Answers6

35

It should be located in YOURPROJECT\web\WEB-INF folder, so the full path will be: YOURPROJECT\web\WEB-INF\web.xml


EDIT (Aug 21, 2015)

Got a downvote with a comment from duffymo, that my answer is not correct.

comment

And I decided to illustrate my answer with step-by-step pictures to avoid any misunderstanding.

I am going to illustrate Netbeans behaviour in Linux (Ubuntu) and Windows (Windows 7) operating systems.

Linux:

  1. Let's create a simple Java Web project with default settings.

simple web project on linux system

  1. We're going to the project folder to inspect the contents of this folder:

web project folder on linux system

note, that web folder is there.

  1. Navigating further:

contents of web folder on linux system

web.xml file location on linux system, file is not created yet

You can create a web.xml file in this folder manually or do it using Netbeans via project context menu "New -> Create -> Other":

creating web.xml on linux system in netbeans #1

creating web.xml on linux system in netbeans #2

Now, we're navigating YOURPROJECT\web\WEB-INF\ folder to see, that web.xml is there:

created web.xml file

The same rules are correct for windows operating system, check the pictures below:

web-application project folder on windows

contents of web-application folder on windows

You can create web.xml here:

web.xml file location on windows

or use Netbeans, as I described above.

Community
  • 1
  • 1
  • 2
    Create web.xml in this folder by yourself. Here is the link to sample web.xml file: http://docs.oracle.com/cd/E19146-01/821-1830/abxii/index.html –  Oct 11 '13 at 12:16
  • This is incorrect. There's no /web in that path. Should be context root with /WEB-INF directly underneath. – duffymo Aug 20 '15 at 22:44
  • 1
    @duffymo I've updated my answer. As you can see, there is a `web` folder, as I advised to OP. –  Aug 24 '15 at 08:13
  • My point is that it's a Netbeans only convention. The WAR file that results will have no such /web folder, just the context root. I'll reverse my downvote. – duffymo Aug 24 '15 at 08:54
  • @duffymo OP question was related to Netbeans IDE, so I provided guidelines for this particular IDE. –  Aug 24 '15 at 12:13
  • Agreed. I think it's important for people to understand what's going on underneath – duffymo Aug 24 '15 at 12:25
  • 2
    THANKS YOU VERY MUCH FRIEND! YOU SAVED MY LIFE! – M. Mariscal Apr 21 '16 at 10:13
30

I know it's so late but I had the same problem, so here's the solution below:

To create web.xml:

  1. Right click on your project
  2. Choose New
  3. Choose Other
  4. Box New file is opened, in Filter search web.xml
  5. You will get the file you want web.xml, then click next...then finish

(Tested on Netbean 7.4 JDK 7)

Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
Strength
  • 311
  • 3
  • 4
6

web.xml is optional in Java EE 6. So, by default it is not loaded in Netbeans. You need to manually load web.xml from Netbeans.

Masudul
  • 21,823
  • 5
  • 43
  • 58
2

You need to firstly create a servlet page then web.xml will be generated in WEB.INF/

Mehandi Hassan
  • 2,381
  • 4
  • 16
  • 20
2

Try to right click the project and select New -> Other-> Web -> Standard Deployment Descriptor (web.xml) -> Next -> Finish. Follow that and it will be created in Configuration Files.

Video tutorial: https://www.youtube.com/watch?v=UAMOeHtPwrc

Demitrian
  • 3,200
  • 1
  • 24
  • 41
0

You could use context-parameters in your web.xml

In you normal java class you read this this static fields.

<?xml version="1.0" encoding="UTF-8"?>
<web-app ...>
...
<context-param>
    <description>directory where the document would be uploaded</description>
    <param-name>directory</param-name>
    <param-value>/tmp</param-value>
</context-param>
...
</web-app>

And you can access this context parameter with ServletContext.getInitParameter function.

If you are using Servlet 3.0 specification you can use annotations(http://docs.oracle.com/javaee/6/api/javax/servlet/annotation/package-summary.html). I think that @WebInitParam is what are you looking for.

user987339
  • 10,519
  • 8
  • 40
  • 45