• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

How to add servlet in Eclipse using Maven ?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear community,

I am still following the course 'Learn Maven and Ant' on Udemy. I have gotten past the Ant part and I am more or less able to follow along with the Maven part.

We have created a new Maven project 'proj-a' with the artifact 'maven-archetype-webapp'.
In src/main/java we created a package 'company.com.proj_a'.
Then the teacher proceeds creating a servlet next to that. For that he right-clicks on the package 'company.com.proj' → other and finds a servlet in there.
However, if I do the same I don't see a servlet in there anywhere.
I have looked up two articles about that and in both cases servlet just happens to be listed when one right-clicks on something in the project tree.

So, how can I add a servlet to a package in Eclipse ?
 
Saloon Keeper
Posts: 28701
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to be clear: are you expecting that "servlet" should be a choice when right-clicking on the package name in the Eclipse workspace pane?

It might - IF you've got the right perspective chosen, but I'll admit that the differences between perspectives can be a little non-obvious at times and I usually don't pay attention to which perspective I'm working in.

However, right-clicking on the package in a Java perspective should give you a context menu that includes "New Class", and that would open a dialog for you where you could simply type "HttpServlet" as the base class and it will, I believe, list the packages that implement it (should be javax.http and jakarta.http or something like that). Once you have the base class and any interfaces you want to implement chosen, type in your desired class name, select any other options you like, and click the OK/Create button.

Maven doesn't come into play here. This is all Eclipse GUI stuff.
 
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I imagine you need some sort of "web application" or "Java EE" perspective. Tim, could you explain how to switch to something like that? I don't use Eclipse.
 
Tim Holloway
Saloon Keeper
Posts: 28701
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm pretty useless here, since, like I mentioned, I don't pay over-close attention to what perspective I'm using as long as I can find what I need. Plus, I'm not actively working with Eclipse at the moment, so I'm relying on my unreliable memory.

I never did use most of those options. Some came in after I'd already formed habits without them, and quite a few of them are features that I simply get into fights with.

As far as perspectives in general go, you can select them off the Window or View menu (I forget which - think it's Window and View and Perspective are sub-menus).
 
Marnix Gheyssen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the two articles are found I think servlet was listed direcly in the right-click menu. In the Udemy course the teacher had to look for it in the dialog window that appeared after he selected ‘Other…’.

In eclipse I choose Window → Perspective → Open Perspective → Other…
Among the perspectives listed I open ‘Java (default).
The arrangement changes and is now different from the arrangement the teacher has.
Still no servlet is listed in the right-click menu of the ‘company.com.proj_a’ (I forgot the _a in my previous post), nor in the Other… window of the menu.

So, I right-click ‘company.com.proj_a’ and choose New Class. In the following dialog window I don’t see anything called ‘base class’. There is something called superclass with the value java.lang.object. So I fill in ‘HttpServlet’ in there. I don’t see any packages listed. I choose the same name as the teacher, namely ProjectATest and click Finish. I get a class of only three lines - the teacher got a lot more lines - and still don’t see any packages listed.

I choose Java Browsing perspective. Arrangement does not change and I still can’t find any servlet.
 
Tim Holloway
Saloon Keeper
Posts: 28701
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Superclass" is what I meant, but if you typed in "HttpServlet", you should have been given some auto-complete options.

Unless.

Eclipse can be downloaded in multiple flavors, called "spins". Last time I looked, I think there were both a plain Java and a Java EE spin as well as special spins for other languages.

They're all the same Eclipse. The only difference is what plugins are or are not pre-installed. You can always add/remove plugins, though.

I'm thinking what what you are probably missing is a reference copy of the JEE or Jakarta EE API classes that Eclipse can use to help its internal compiler and do auto-complete functions on.

Now in my case, I actually "steal" them from my copy of Tomcat (No versions of Eclipse come with its own webapp server). What I do is define Eclipse variables that reference my TOMCAT_HOME/lib libraries and ensure that the project in question includes those variables.
 
Marshal
Posts: 4808
602
VSCode Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

From the menu bar, follow: Window - Perspective - Open Perspective - Other ... Java EE




After adding a perspective, it should show in the menu bar


There should also be a new widget in the menu bar to add new Java EE artifacts, which includes Servlet



 
Marnix Gheyssen
Greenhorn
Posts: 15
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I change the perspective to ‘java (default)’ and the arrangement changes to what looks like the arrangement of before I first changed the perspective to ‘java (default)’. No ‘java EE (default)’ perspective is available.

My menu bar looks like this :


[There is supposed to be an image here showing the pertinent button missing.]

I searched on how to add a perspective and found www.dark-hamster.com/programming/java/ide/how-to-install-j2ee-perspective-in-eclipse. So, I installed the plugin ‘Web, XML, Java EE and OSGi Enterprise Development’. That took 8 minutes and I had to trust something unsigned, unknown, unIDed and without validity dates.

There still is no ‘Java EE (default)’ perspective, so I open the ‘Java EE’ perspective.

Lo and behold. I can find a ‘Servlet’ to add now to my package.

Thank you for the help.
eclipse-menu-bar.png
[Thumbnail for eclipse-menu-bar.png]
 
Tim Holloway
Saloon Keeper
Posts: 28701
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Congratulations!

Unfortunately, a lot of the Eclipse plugin stuff is unsigned, etc. I'm not at all sure that there is, in fact, an authoritative security signing agency for Eclipse.

Some plugin features may not show until you restart Eclipse after installing. But I'm glad to hear that you got what you wanted!
 
Marnix Gheyssen
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, I continue the lesson, select ‘Servlet’ and Click the Next button. Nothing happens. When the teacher clicks that another dialog window appears.
Double-clicking ‘Servlet’ generates the error message window with the message:
“An error has occurred. See error log for more details.
Path for project must have only one segment.”

I have found what looks like the pertinent error log, but it is long, so I will only post the first 20 lines :


 
Stephan van Hulst
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you Google "Path for project must have only one segment", you might find a couple of things to try to solve the issue.
 
reply
    Bookmark Topic Watch Topic
  • New Topic