[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 879
  • Last Modified:

Configuring NetBeans3.6

hi experts ,

I have started using NetBeans and its a wonderful tool. I have a question though.

I have written a java program (which is one of the 10 programs in the whole project) .

The objective of this project is to download/upload/refresh/delete files from/to  the server using a Swing GUI.

The below given java program(which is the main program in the front-end) connects the server using the URL .the program is shown below:


--------------------------------------------------FileExchangeApplicaiton.java---------------------------
//This java program checks the server URL (using "server" object)

package FileExchange;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;

public class FileExchangeApplication
{
private FileExchangeApplication()
{
}
public static void main(String[] args)
{
URL server;
try
{
if(args.length >0)
{
server = new URL(args[0] + "FileExchange/FileExchangeServlet");
//FileExchange is name of package and FileExchangeServlet is name of servlet
}
else
{
server = new URL("http://localhost:8080/FileExchange/"+"FileExchangeServlet");
//default URL using port 8080
}
}
catch (MalformedURLException mue)
{
System.out.println("Error in using FileExchangeApp");
System.out.println("Usage:\n"+"java FileExchangeApp [ServerURL]");
mue.printStackTrace();
return;
}
JFrame frame = new FileExchangeView(server,"File Exchange Application");
 // the FileExchangeView java program has the GUI.
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}});
frame.pack();
frame.setVisible(true);
}
}

-------------------------------------------------------------------
Now, The problem in above program is with the server URL : SInce i am new to using NetBeans3.6 , i am not sure if the server address that i gave is correct:

 server = new URL(args[0] + "FileExchange/FileExchangeServlet"); //FileExchangeServlet is name of main servlet
 server = new URL("http://localhost:8080/FileExchange/"+"FileExchangeServlet") //the default server URL.

1) any idea what the "server URL"  should be in NetBeans.
2) Also ,should i start tomcat with in NetBeans3.6 or does NetBeans takes care of it. If i have to start Tomcat , how do i do it?

any help greatly appreciated!

Thanks,
J


0
jaggernat
Asked:
jaggernat
  • 32
  • 25
1 Solution
 
petmagdyCommented:
why don't u go to the servlet FileExchangeServlet in ur project and try to run it first, it should open in ur browser, which will run tomcat automatically and will show the address in the browser
0
 
jaggernatAuthor Commented:
ok...
I compiled it first (F9) ..no problem
 ..then how do i execute...i cannot do F6....If i did , it will say :No main() found.

How do i execute the servlet from NetBeans.

thanks
0
 
petmagdyCommented:
Frankly i don't have netbeans on my machine now, but I can imagine if u went to the Web Module, created any empty jsp that says hello, then on this jsp right menu and search for run or excute web module or something like that it will run in browser after starting tomcat and show u the address u want
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
petmagdyCommented:
ok I installed netbeans now the tomcat default url is:

http://localhost:8084/

and to run tomcat just, go to ur web module root file mount and just (right menu ---> Execute)
0
 
jaggernatAuthor Commented:
what do you exactly mean by WebModule ??

in my net beans , i have created a single package called "File Exchange" under the "mounted directory" and stored all my java files in that package.

0
 
petmagdyCommented:
ok u have to create a web module (file --> New ---> Jsps & Servlets --> Web module
and specify context path to be /FileExchange

this will create a 2 New File Systems but for the same directory, one will contain ur jsps and WEB-INF/web.xml and other called /web-inf/classes that should contain ur servlets packages and classes

make sure to put ur servlet in the second file system with its same package path and open web.xml in the first fille system and put the servlet definition inside it like this:
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
     version="2.4">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
    <welcome-file>
            index.jsp
        </welcome-file>
    <welcome-file>
            index.html
        </welcome-file>
    <welcome-file>
            index.htm
        </welcome-file>
    </welcome-file-list>
     <servlet>
        <servlet-name>FileExchangeServlet</servlet-name>
        <servlet-class>mypackage.FileExchangeServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>FileExchangeServlet</servlet-name>
        <url-pattern>/FileExchangeServlet</url-pattern>
    </servlet-mapping>
   
</web-app>

now u r ready to run ur servlet, go to the servlet in ur File System hierarchy and click right menu and Execute, it will run the servlet in the browser and will show u the address

Good luck
0
 
jaggernatAuthor Commented:
ok,,i have a question..

my project consists of overall 15 java/ class files. in three categories.
client side java files (using java swing)
server side java files (using java servlets)
Serializable java files which are used by client and server side (call them middle layer files)


i have the main package called FileExchange(as i said earlier, which USED TO contains all the 15 java files)
Now , my question is should i move all the 15 java files (including client , server and middle layer) files to Web-INF\classes location.

or
should i just move  the server side and middle layer files to WEB-INF\classes and keep the client files in the FileExchage package.

Since FileExchange package and WEB_INF\classes  are two seperate directories , i am worried if they will communicate.

thanks
0
 
petmagdyCommented:
Only move the Server Side, mainly ur servlet and all Classes that servlet depends on
0
 
jaggernatAuthor Commented:

when i execute ...is it still saying

---------------------------------------
java.lang.NoSuchMethodError: main
Exception in thread "main"

-----------------------------------
i guess its treating the file as a java file and looking for main().

am i missing something?

thanks
0
 
petmagdyCommented:
ok stand on ur servlet and, right menu and select properties, in the execution section make sure that the Executer and debbuger is "J2EE Server Execution"
0
 
jaggernatAuthor Commented:
this is my web.xml seting:



<servlet>
        <servlet-name>FileExchangeServlet</servlet-name>
        <servlet-class>FileExchangeServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>FileExchangeServlet</servlet-name>
        <url-pattern>/FileExchangeServlet</url-pattern>
    </servlet-mapping>
0
 
petmagdyCommented:
is ur servlet on the root package?
0
 
jaggernatAuthor Commented:
it throws error stating:

"J2EE server Execution cannot be used for this project. please change to default executor and try again"


thanks
0
 
petmagdyCommented:
ok try this:

Take a copy of the source file FileExchangeServlet.java away, delete the file from ur project, in this node create a servlet using (File--> new---> ...... --> Servlet) create with the same name, copy back the code and try
0
 
petmagdyCommented:
make sure to create the servlet into web-inf/classes file system
0
 
jaggernatAuthor Commented:
there is no file- > new -> servlet .

do u mean file- > new -> jsp&servlet .
0
 
petmagdyCommented:
yes it is  file- > new -> jsp&servlet --> Servlet
0
 
jaggernatAuthor Commented:
For class name , when i enter FileExchangeServlet, it says class must be in a package (error)

0
 
petmagdyCommented:
ok first stand on web-inf/classes then right menu --> New --> java Package, call it as for example FileExchange.servlets
then on the newly create package node stand on and create ur servlet
but now ur web.xml Servlet definition will look like this:

<servlet>
        <servlet-name>FileExchangeServlet</servlet-name>
        <servlet-class>FileExchange.servlets.FileExchangeServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>FileExchangeServlet</servlet-name>
        <url-pattern>/FileExchangeServlet</url-pattern>
    </servlet-mapping>
0
 
jaggernatAuthor Commented:
this is my javafile / classname                                                     FileExchangeServlet
This is my web module (ie the mounted directory)                        C:\winnt\profiles\jay\Desktop\src

this is the location                                    C:\winnt\profiles\jay\Desktop\src\WEB-INF\classes\FileExchangeServlet

0
 
petmagdyCommented:
did u try my last comment?
0
 
jaggernatAuthor Commented:
>>>>>>>then on the newly create package node stand on and create ur servlet

how?

0
 
petmagdyCommented:
stand on the node web-inf/classes/FileExchange/servlets
then right menu --> new --> all templates --> jsp&servlet --> Servlet

or select the node web-inf/classes/FileExchange/servlets
then Main Menu --> File --> new --> jsp&servlet --> Servlet

very easy :)
0
 
jaggernatAuthor Commented:
is it

server --> right menu --> new -> java main class


0
 
petmagdyCommented:
nooooooooo
0
 
petmagdyCommented:
please just follow my comments
please note that EE is not a place for Step by Step guidness but to give u useful tips that u can make use off
0
 
jaggernatAuthor Commented:
stand on the node web-inf/classes/FileExchange/servlets
then right menu --> new --> all templates --> jsp&servlet --> Servlet

ok ... i did the above


then what
0
 
petmagdyCommented:
now Complete the Code from ur old file, but don't change this line:

package  FileExchange.servlets

and then modify web.xml as I told u before to specify the servlet new package, then compile ur servlet and try to execute it as I told u before
0
 
jaggernatAuthor Commented:
ok,,,i get an error on "configure server deployment " window.

It says "the name must be unique across servlets and filters"

0
 
petmagdyCommented:
can u please post ur web.xml and make sure their is no other servlet called the same name
0
 
jaggernatAuthor Commented:
ok..i pasted my code in the editor..also included the package "  package FileExchange.servlets;"

so now my FileExchangeServlet
is in
WEB-INF\classes\FileExchange\servlets

then?
0
 
petmagdyCommented:
know ur servlet definition in the web.xml should be like this:

<servlet>
        <servlet-name>FileExchangeServlet</servlet-name>
        <servlet-class>FileExchange.servlets.FileExchangeServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>FileExchangeServlet</servlet-name>
        <url-pattern>/FileExchangeServlet</url-pattern>
    </servlet-mapping>

thats it and then run the Servlet, select it then right menu --> Execute, it shall open in browser do u can get the URL of the Servlet u want
0
 
jaggernatAuthor Commented:
yes web.xml is perfect

i cant run the servlet because of errors.. the servlet depends on other classes and they are in the
web-inf\classes  and NOT web-inf\classes\FileExchange\servlets

...
0
 
petmagdyCommented:
then u r on the right track now, move those classes to a new package inside (web-inf\classes)  for example FileExchange so they are in (web-inf\classes\FileExchange) and change their package to be:

package  FileExchange;

in ur servlet:

import FileExchange.*;

and then compile all the classes and run again




0
 
jaggernatAuthor Commented:
excellent ..its starts up Tomcat.

thanks for the help so far..i greatly appreciate it.
0
 
petmagdyCommented:
welcome :)
0
 
jaggernatAuthor Commented:
petmagdy,,i might have to leave my office now..its 5.00 PM in chicago...but will definetelty get back tomorrow same time (as we started today) to finish the rest of the problem.

i once again appreciate your efforts ..u are really an expert!!!

thanks,
J


0
 
petmagdyCommented:
thank u welcome any time
0
 
petmagdyCommented:
sorry again, It seems I must go to bed ;) the URL is:

http://utenti.lycos.it/yanorel6/2/

Read the title "A Multithreaded Server Framework"
0
 
petmagdyCommented:
sorry jaggernat the last comment is not for u :)
0
 
jaggernatAuthor Commented:
Tomcat 5.0.19 starts,, but the browser shows an error: here is the error report:

------------------------------------------------

HTTP STATUS 500

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet execution threw an exception
      org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:305)


root cause

java.lang.StackOverflowError


note The full stack trace of the root cause is available in the Tomcat logs.

-----------------------------------------------------------------------------------
0
 
petmagdyCommented:
jaggernat, u have to investigate the stack trace in tomcat logs to see what is wrong in ur servlet Code.

you will find the log at:

NetBeans3.6\jakarta-tomcat-5.0.19\logs

u can also debug ur application, put a break point inside ur servlet code, and then select ur servlet node, then go to menu debug --> Start Session --> run in debugger

Now the application will stop at ur break point and u can debug ur servlet

Please note that my participation stops at providing useful tips as the Expert exchange (EE) nature implies, not step by step support, u know step by step requires more time participation and advanced communication facilities like chat rooms
0
 
jaggernatAuthor Commented:
the file in the log folder is not a text file. its a file called dummy.

0
 
petmagdyCommented:
ok in ur servlet do this:

try
{

}
catch(Exception e)
{
   e.printStackTrace();
}

soo the complete stack trace so u will be able to say the stack trace in ur Netbeans
0
 
jaggernatAuthor Commented:
i already have a try- catch statement in my servlet.
this is my servlet: actually the servlet is initiated from the client swing application.

package FileExchange.servlets;

import javax.servlet.http.HttpServlet;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import FileExchange.*;

public class FileExchangeServlet extends HttpServlet
{
      private RequestHandlerFactory requestHandlerFactory = null;
      public void init() throws ServletException
      {
            String directory = getServletContext().getRealPath("/")+"WEB-INF" + File.separator +"files"+ File.separator;
            RequestHandler.setStringFileBase(directory);

            requestHandlerFactory = RequestHandlerFactory.getInstance();
      }
      public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      {
            try
            {
                  BaseRequest baseObject =(BaseRequest) readSerializedObject(request);
                  RequestHandler handler =(RequestHandler) requestHandlerFactory.getHandler(baseObject.getRequestType());
                  handler.setHttpObjects(request,response);

                  handler.respond(baseObject);
            }
            catch(ClassNotFoundException cnfe)
            {
                  cnfe.printStackTrace();
            }
            catch (IOException ioe)
            {
                  ioe.printStackTrace();
            }
      }
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
      {
            doGet(request,response);
      }

 private Object readSerializedObject (HttpServletRequest httpServletRequest) throws IOException, ClassNotFoundException
      {
       ObjectInputStream ois = new ObjectInputStream(httpServletRequest.getInputStream());
       Object readObject = ois.readObject();
       return readObject;
      }
}

0
 
petmagdyCommented:
now I know why u have this exception because of the code in ur init()  function

do not use getRealPath() any more it is debricated in new versions of all Servlet engines it returns null, watch ur self try this:
String realpath = getServletContext().getRealPath("/");
System.out.println("Real Path: " + realpath);
0
 
petmagdyCommented:
sorry jaggernat getRealPath() still in tomcat 5.0 return non null value
0
 
petmagdyCommented:
hhhh, i got ur problem that cause stack over flow because of this:

>>     public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
     {
          doGet(request,response);
     }
it should be:
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
     {
          doPost(request,response);
     }

0
 
jaggernatAuthor Commented:
u know what!..i am getting the same error report:
------------------------------
HTTP STATUS 500

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet execution threw an exception
     org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:305)


root cause

java.lang.StackOverflowError


note The full stack trace of the root cause is available in the Tomcat logs.
------------------------------------------------------------------------

0
 
petmagdyCommented:
jaggernat,
do u saw my last 2 comments?
0
 
jaggernatAuthor Commented:
yep...excellent..

I am overwhelmed.
Thank you sir.
0
 
petmagdyCommented:
finally :)
welcome any time
0
 
jaggernatAuthor Commented:
actually i got the code from a book..cant imagine that even books have errors :-(

full points to u. :-)

0
 
jaggernatAuthor Commented:
could you please suggest me some good book for  servlets/jsp . for starters...

thanks
0
 
jaggernatAuthor Commented:
hi ,
one final question.

what exactly is the difference between

  doGet(request,response);
and
   doPost(request,response);

what exactly is stackoverflow
0
 
petmagdyCommented:
doGet process the GET METHOD HTTP Requests
doPost process the POST METHOD HTTP Requests

stackoverflow means, u program entered an infinite loop of calling the same function, which caused the JVM stack to overflow
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 32
  • 25
Tackle projects and never again get stuck behind a technical roadblock.
Join Now