在IntelliJ中如何创建新的Servlet文件?

huangapple go评论83阅读模式
英文:

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

问题

我是JSP编程的新学习者,所以我选择了Intellij作为我的集成开发环境。

我安装了Intellij Ultimate、来自Oracle的JDK 14,以及Tomcat 9,然后搜索如何创建新项目。

但是,大多数解释如何创建新的Servlet项目的帖子都说:

"当你创建新项目时,你可以在Java窗格的附加库和框架下找到Web服务复选框。"

甚至JetBrains也是这么说的,(链接:https://www.jetbrains.com/help/idea/preparing-to-develop-a-web-service.html),但我找不到它。

(插入图片)

当然,我检查了我的插件是否已启用。

不管怎样,我找到了另一种创建它的方式,就是选择Java Enterprise,在点击下一步后,从库和框架中选择“Web profile”。

但是现在,另一个问题出现了,因为我在src或src/main/java上右键单击,找不到“new - servlet 项目

(插入图片)

为什么会发生这些事情?即使我找到了创建Servlet文件的方法,也可能会出现其他类似的问题,我想。

JSP项目有什么先决条件吗?还是因为Intellij已经升级了?我想知道为什么我的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.

在IntelliJ中如何创建新的Servlet文件?

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

在IntelliJ中如何创建新的Servlet文件?

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.

答案1

得分: 1

我发现了这种方式,希望它对你有用,就像对我一样。首先,我建议你添加对Maven框架的支持,或者你可以直接下载所需依赖的JAR文件,用于创建和管理servlets Java Servlet API

添加Maven框架支持

在项目工具窗口中,你需要选择要为其添加Maven框架支持的模块,右键单击它,然后选择Add Framework support。就像这样:

添加Maven支持

在新窗口中,你只需要从可用框架列表中选择Web Application和Maven。选择Web Application后,它会创建一个web包和其web.xml配置文件。在被询问时,接受创建web.xml文件以继续。完成这些步骤后,你的项目结构可能看起来像这样:

添加Maven和Web应用程序框架后的项目结构

向pom.xml添加Java Servlet API依赖

现在你需要打开pom.xml文件。这个文件可以帮助你添加和管理项目依赖,因为它允许你从Apache Maven仓库中检索它们,而不是手动下载JAR文件并将其添加到项目中。你需要添加下面的依赖:

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

在完成上述步骤后,你的pom文件应该如下所示:

添加Java Servlet API依赖

作为最后一步,我们需要通过按下Ctrl + Alt + Shift + S或者通过导航到File > Project Structure来打开Project Structure设置。在窗口的左侧,我们将找到一个Project Settings部分。我们需要进入Facets,你可能会找到一个名为Web的facet。在窗口底部,你应该看到一个名为Source Roots的选项。你需要通过点击复选框来启用它,然后点击Apply,最后点击OK。你需要确保在此部分显示的路径确实是指向你项目的src > main > java文件夹的路径。

将文件夹标记为源根

完成这些步骤后,你应该能够进入项目中的任何包并创建一个servlet,如下所示:New > Servlet

创建servlet

**注意:**如果在创建pom.xml文件时出现语法错误,你需要做的唯一的事情就是将<groupId></groupId>标签中的内容更改为你想要用来标识项目的完全限定名称。

为你的项目指定限定名称

**注意:**我无法找到一种永久配置src > main > java文件夹作为源根的方法,因此每次你关闭并重新打开项目时,你需要转到Project Structure (Ctrl + Alt + Shift + S) > Facets > Web,然后再次将其标记为源根。如果你找到了一种永久解决此问题的方法,我将很感激你分享出来。

更多信息

英文:

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

答案2

得分: 1

  1. 右键点击 src\java > 标记目录为 > 源代码根目录
  2. 前往 文件 > 项目结构...
  3. 选择 模块
  4. 点击 Web(项目名称)
  5. 勾选 src\main\java,位于源代码根目录下。
英文:
  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.

在IntelliJ中如何创建新的Servlet文件?

答案3

得分: 0

首先,您可以转到 src > main > java,然后右键单击:

在IntelliJ中如何创建新的Servlet文件?

然后选择 New > Create New Servlet。

英文:

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

在IntelliJ中如何创建新的Servlet文件?

and then New > Create New Servlet.

huangapple
  • 本文由 发表于 2020年9月12日 06:03:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63854849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定