This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes IDEs, Version Control and other tools and the fly likes Default netbeans servlet with JSP file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Engineering » IDEs, Version Control and other tools
Bookmark "Default netbeans servlet with JSP file" Watch "Default netbeans servlet with JSP file" New topic
Author

Default netbeans servlet with JSP file

Jerry Richmond
Greenhorn

Joined: Mar 24, 2012
Posts: 4
Friends,

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

Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5779
    
    7

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


JBoss In Action
Jerry Richmond
Greenhorn

Joined: Mar 24, 2012
Posts: 4
Peter,

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
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5779
    
    7

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Default netbeans servlet with JSP file
 
Similar Threads
running a servlet
JSP Page Translation + Tomcat
JSF - Problems with NetBeans 6.9.1
Simple question from Beginner on Using NetBeans
NetBeans cant find web pages folder