Summary: The Create new servlet
doesn't show up in the File > New > ...
menu and I'm convinced this did work in older versions of InteliJ.
I'm using InteliJ 2024.1.1 Utlimate.
I've created a maven project and added this dependency:
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
Clicked "Reload all maven projects".
Everything seems to be working as I'm used to from older versions of IntelliJ: src/main/java
is marked as sources root, src\main\webapp\WEB-INF\web.xml
is detected as deployment descriptor, src/main/webapp
is detected as web resource directory.
Now I'd expect File > New > ...
to contain Create new servlet
.
I've even found a template that I would think is (or was) used by this shortcut:
Settings > File and Code Templates > Other > Web > Java code templates > Servlet Class.java
Some of the answers in this very similar question imply that this should work: Where can i find to create new servlet file with intellij?
InteliJ's documentation says I should create a template myself: https://www.jetbrains.com/help/idea/creating-and-configuring-web-application-elements.html#elements-templates
Am I doing something wrong or do I really need to add a new file template myself?
IntelliJ 2023.1 dropped these templates as they were rarely used nowadays.
See this issue-tracking page:
Cannot create servlet, web filter, web listener after 2023.1 update
On that issue page you will find attached three .java files used as templates.
If you would like to still use them please go to "File | Settings | Editor | File and Code Templates" and add a custom template.
Or you can simply copy-paste into your project, and edit to suit your needs, deleting the #
lines at top.
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
#parse("File Header.java")
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "${Class_Name}", value = "/${Class_Name}")
public class ${Class_Name} extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
#parse("File Header.java")
import javax.servlet.*;
import javax.servlet.annotation.*;
@WebFilter(filterName = "${Class_Name}")
public class ${Class_Name} implements Filter {
public void init(FilterConfig config) throws ServletException {
}
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
chain.doFilter(request, response);
}
}
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end
#parse("File Header.java")
import javax.servlet.*;
import javax.servlet.http.*;
public class ${Class_Name} implements ServletContextListener, HttpSessionListener, HttpSessionAttributeListener {
public ${Class_Name}() {}
@Override
public void contextInitialized(ServletContextEvent sce) {
/* This method is called when the servlet context is initialized(when the Web application is deployed). */
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
/* This method is called when the servlet Context is undeployed or Application Server shuts down. */
}
@Override
public void sessionCreated(HttpSessionEvent se) {
/* Session is created. */
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
/* Session is destroyed. */
}
@Override
public void attributeAdded(HttpSessionBindingEvent sbe) {
/* This method is called when an attribute is added to a session. */
}
@Override
public void attributeRemoved(HttpSessionBindingEvent sbe) {
/* This method is called when an attribute is removed from a session. */
}
@Override
public void attributeReplaced(HttpSessionBindingEvent sbe) {
/* This method is called when an attribute is replaced in a session. */
}
}
You can also find many examples on Stack Overflow and elsewhere on the Web.