• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

How to call a main method of a class with constructor from a servlet class?

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

I have a full code in java which works well, it has 4 classes, in main class I have constructor which call another class. I want to run it in servlet application. I wrote all codes and the forms in servlet and I am looking for a way to call main method in servlet class, is it possible? and does it handle constructor method too? If not how should write the main method and constructor in servlet.



I really appreciate if anyone can helps me.
 
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, it's just an ordinary static method and you can certainly call static methods from a servlet. But it was probably a mistake to design your code that way. Creating an object of some class and calling one of its methods would be better, and you already have code which does that:


 
Nikolas cage
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Paul,
Actually I made a simple mistake in the code. I made it shorter and simpler.


I am not good in java yet, do you recommend to change the structure? How should I call it in servlet? If I put the main method in doGet method, then how should I handle the constructor?

Thanks,
Meisam
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're writing that class for the purpose of using it in a servlet, then it doesn't need a main method. Just get rid of that.
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and if you plan to use it in a servlet then don't use relative paths in the file names. The servlet's working directory isn't going to be what you used while testing that code and you aren't even going to know what it is. So use the full path to the files.
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nikolas cage wrote:... in main class I have constructor which call another class...



As many beginners in Java do, it looks like you think that "calling a class" is a Java concept. But it isn't. You don't call classes in Java. You call methods, which belong to classes. Typically you need an instance of the class to call its methods, but if the methods are static then you just need the class.

So if you want to call those methods from a servlet, you just need to create an instance of the class and call the methods. I already pointed out earlier in the thread that you already have code which does that, and that you should simply put that code into your servlet.
 
Bartender
Posts: 20842
125
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:
As many beginners in Java do, it looks like you think that "calling a class" is a Java concept. But it isn't. You don't call classes in Java. You call methods, which belong to classes.



It bears repeating, With emphasis.

Also, you don't have a "main" class or method in a web application. A web application isn't a program that runs, it's a collection of functions (servlets) that get called in response to URL requests. A web application has no main process, nor even its own thread - it gets loaned a thread from a thread pool when a servlet needs to run (note that JSPs become servlets thanks to the jsp compiler in the webserver). And it has to give the thread back the instant the servlet's request-handler method gets done.
 
Nikolas cage
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, thank you Paul and Tim for replying,
I know in servlet we do not need main method. I have problem to change Main class and servlet to call method in the Main class. I am trying to revise the classes.



And for servlet:



I appreciate you for helping.
Meisam
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nikolas cage wrote:I have problem to change Main class and servlet to call method in the Main class.



Then it would help if you told us what the problem is.
 
Nikolas cage
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Then it would help if you told us what the problem is.



I do not know how I should complete the code I already posted.
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:... You already have code which does that:


 
Nikolas cage
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does not work.

I am trying to skip/remove Main class.



It's not working yet.
 
Paul Clapham
Marshal
Posts: 24594
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nikolas cage wrote:It does not work.



That's not surprising. For one thing you haven't written a servlet.

However it's not a good idea to just say "It does not work" and hope somebody can see why. So how about if you tell us what makes you think it doesn't work. Did you get error messages? What did you try to make it run? And so on.
 
Nikolas cage
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Paul,
I am working on that in two ways, first make it from the base in the servlet, and change the main method to servlet and use of other classes. I am learning servlet and working on this everyday. I appreciate you for your attention but I prefer work on that by myself and ask from a person who helps without making me feel obligated.
 
Rancher
Posts: 4117
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what the error is with that code above, without some pointers, but one thing does jump out at me.
You need to make sure that AnotherClassName is threadsafe, since you are making it an attribute of a servlet and so it will be called by mulitple threads.
 
Saloon Keeper
Posts: 5581
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Nikolas cage wrote:
FileOutputStream fileOut = new FileOutputStream("report/userTrendsIWS-" + dateFormat.format(date) + ".xlsx");


Web apps have no concept of a relative path (or rather, they have no standard concept of what the home directory is) - you need to use absolute paths instead.
 
Nikolas cage
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I changed it as below


And I have also changed the methods of class AnotherClass to static. It prints 4, makes the file in folder “report”, and makes error like as:

<4-Jul-2019 9:33:11 o'clock AM EDT> <Error> <HTTP> <BEA-101017> <[ServletContext@15258489[app:excel1 module:excel1 path:null spec-version:3.0], request: weblogic.servlet.internal.ServletRequestImpl@1dbe7a1[
GET /excel1/Main HTTP/1.1
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.3.6 (java 1.5)
Accept-Encoding: gzip,deflate

]] Root cause of ServletException.
java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Workbook
at process.Main.doGet(Main.java:63)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:844)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:280)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:254)
Truncated. see log file for complete stacktrace
Caused By: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Workbook
at weblogic.utils.classloaders.GenericClassLoader.findLocalClass(GenericClassLoader.java:335)
at weblogic.utils.classloaders.GenericClassLoader.findClass(GenericClassLoader.java:302)
at weblogic.utils.classloaders.ChangeAwareClassLoader.findClass(ChangeAwareClassLoader.java:64)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
Truncated. see log file for complete stacktrace
>

It seems it can’t run AnotherClass which provides information inside of excel sheet.
 
Tim Moores
Saloon Keeper
Posts: 5581
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So class Main uses POI - that comes with a lot of dependent jar files. Put all those into the WEB-INF/lib directory of your web app.
 
Nikolas cage
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did but it didn't change anything, because the error is from this line:
 
Tim Moores
Saloon Keeper
Posts: 5581
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frankly, I don't know how to help if you obscure class and method names, and don't post full error messages and stack traces.
 
Nikolas cage
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have changed the names to make it easier for reader! The error says the problem happened in the first line of calling another class' method. Anyway thank you.
 
Tim Moores
Saloon Keeper
Posts: 5581
144
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not making it easier, it makes it harder by removing information needed to speculate about the causes of the issue.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!