简体   繁体   中英

Where can i find to create new servlet file with intellij?

I'm new learner of JSP programming, so I choose intellij as my IDE.

I installed Intellij Ultimate, JDK14 from oracle,and Tomcat 9, and then searched for creating new project.

But, most of the posts which explain how to create a new servlet project says that

"when you create new project, you can find Web service checkbox under Additional Libraries and Frameworks in Java Pane."

Even JetBrains' are saying the same thing, ( https://www.jetbrains.com/help/idea/preparing-to-develop-a-web-service.html ), but I couldn't find it.

在此处输入图像描述

Of course I checked whether my plugins are enabled.

Anyway, I found another way to create it by selecting Java Enterprise, and after clicking Next, checking "Web profile" from Libraries and Frameworks.

But now, Another problem occurs because I cannot find "new - servlet project with right clicking on the src or src/main/java

在此处输入图像描述

Why are these things happening? Even if I find some way to create servlet file, maybe there will be other problems like this, I think.

Is there any prerequiste for JSP project? or is it just because intellij has been ungraded? I wanna know the reason why my intellij is different with others.

First, you can go to src > main > java, then right click:

图片

and then New > Create New Servlet.

I found this way I hope it works for you as it did for me. First I would recommend you to add support for Maven framework or you just could download the JAR file of the needed dependency for creating and managing servlets Java Servlet API .

Adding Framework support for Maven

On the Project tool window, you have to select the module you want to add Maven Framework support to, right-click it and select Add Framework support . Just like this:

Adding Maven support

On the new window you just select Web Application and Maven from the list of available Frameworks. When selecting Web Application it creates a web package and its web.xml configuration file. When asked, accept creating the web.xml file to proceed. After these steps your project structure might look like this:

Project structure after adding Maven and Web application frameworks

Adding Java Servlet API dependency to pom.xml

Now you need to open the pom.xml file. This file helps you to add and manage your project dependencies as it allows you to retrieve them from the Apache Maven repository instead of downloading the JAR files and adding them manually to your project. You need to add the next dependency:

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
    </dependency>
</dependencies>

Your pom file should look like this after the previous step:

Adding Java Servlet API dependency

As final step, we need to open the Project Structure settings by pressing Ctrl + Alt + Shift + S or by navigating through File > Project Structure . Here we are going to find a Project Settings section on the left side of the window. We need to go to Facets and you may find a facet named Web . On the bottom of the window you should see an option called Source Roots . You have to enable it by clicking the checkbox, then click Apply and finally OK . You have to make sure that path shown in this section is indeed the path to your projects' src > main > java folder.

Marking folder as source root

Once you've done that, you should be able to go to any package in your project and create a servlet as follows: New > Servlet .

Creating servlet

Note: if you got a syntax error when pom.xml file was created the only thing you need to do is to change the <groupId></groupId> tag content to a fully qualified name you want your project to be identified by.

Given your project a qualified name

Note: I wasn't able to find a way to configure permanently the src > main > java folder as source root, so every time you close and reopen your project you need to go to Project Structure (Ctrl + Alt + Shift + S) > Facets > Web and mark it as source root again. If you find a way to do this permanently I would appreciate you to share your method.

More info on

  1. right click src\java > Mark directory as > source root
  2. go to File > Project Structure...
  3. select Facets
  4. click Web(project name)
  5. check src\main\java under source root.

截屏

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM