I apologize if this is a very basic question but it is throwing me.
I have JAVA-EE with Netbeans and the EE plugins. I am using Netbeans to create a simple HelloWorld type new Servlet from scratch and in the process of doing so, it also creates a file that has a name like index.jsp too under WEB-INF. Ok, fine. However, when I try to select "Run Project" under the RUN menu in Netbeans, it runs the index.JSP file content and not the code in the Servlet (both the JSP and the Servlet have a print statement within them with different "hello world" messages I edited which is how I know which one is running.)
So, if I click on "Run Project", the HTML web page that opens displays the contents from the index.JSP file.
That is, out of the box with no editing by me other than to change the two 'hello world' messages, after I create a new Servlet, I see that when I click on "Run Project" - all I get is the index.JSP file running in the browser.
However, if instead, I right-click the actual class_name.java file (under Source Packages) and then select "Run File" - then it runs my actual Servlet (and not the JSP apparently).
So, I can fix this by designating the 'welcome page' in the web.xml descriptor file to point to my project name (HelloWorld instead of index.jsp) file but I am wondering why does Netbeans generate both a JSP and a Servlet page and only run the JSP file when I do a "Run Project" command? That seems counter productive or at least counter-intuitive. But perhaps this is because of my lack of sufficient JAVA knowledge (or Netbeans knowledge).
Oh, and I tried deleting the index.JSP file since as best I can tell I don't need it but then the browser cannot find a page to run. (good ole 404 error).
Any ideas on how these two files are "supposed" to work together?
Thanks - Jerry
p.s. - If I make ANY kind of changes to the index.jsp file, Netbeans will no longer run my project (not even with the 'welcome page' set up in web.xml). I just get a message that says "Saving Index" (which it does NOT actually do). I know this because I can open it in a different editor and it hasn't changed. Also, when I go to close Netbeans, it hasn't changed - and I can't actually save it. bummer...
It's been a while since I went through this, but if I recall correctly, this is basic web application behavior. By default (that is, unless otherwise specified in the welcome list in the web.xml file), the index.jsp is run (unless you also have an index.html file, in which case it comes first). Personally, I always specify a welcome list (which is why I am a little fuzzy on what happens when one isn't there).
Thank you for the reply. Perhaps that is the way it is meant to operate indeed. It just seemed kind of odd to me but I am very new to JAVA so perhaps as you say that is just how it's intended to be created. I just was not sure how they could expect the Servlet code to run if only the JSP file is running (which seems to be the case). Thanks for the reply!
Jerry Richmond wrote:I just was not sure how they could expect the Servlet code to run if only the JSP file is running (which seems to be the case)
Actually, that is not the case. Both the JSP and the servlet are "deployed". That means that they are both registered with the web server (most likely Tomcat) and are available to take requests. If you have enough requests come in simultaneously for both of them, then will both be running at the same time. Of course, gaining access to a servlet is a little trickier because you either need to reference it in the web.xml, or if you are using the servlet 3.0 spec, then there are default rules for accessing it (even without a web.xml).
Note that there is no such thing as "running" a JSP or a servlet. Web applications are different animals from standard Java applications and are not, and cannot, be "run" in that way. What NetBeans is doing behind your back is packaging the web application and giving it to the web server, and it is the web server that is run.
Confusions like this happen less often if you don't rely on an IDE to do all of the work for you and thus hide the details. If you really want to learn servlets and JSPs, I suggest finding a book or tutorial that requires only the use of a text editor and the command line. You can still use NetBeans, but only as a text editor.