Java學習之Idea搭建Servlet項目


准備:

用Idea、maven搭建一個簡單的Servlet項目,環境要求(版本不要求統一):JDK1.8、Tomcat8.5.42、Maven3.5.3、Idea2017、Mysql5.5。

搭建(以Idea為例):

Idea:

1、點擊file-new-project,會彈出右圖窗口

 2、選中maven,勾選“create from archetype”,然后並選中找到“maven-archetype-webapp”,點擊next,然后填上GroupId和ArtifactId。

補充:

groupId 

定義了項目屬於哪個組,舉個例子,如果你的公司是mycom,有一個項目為myservlet,那么groupId就應該是com.mycom.myservlet. 

artifacted 
定義了當前maven項目在組中唯一的ID(名稱),比如myservlet。 

 

 3、點擊上圖的流程后就會創建一個 如下圖結構的項目雛形。

 

 Pom.xml文件中的版本是默認的,可以根據實際要求調整,例如默認的JDK是1.7,我用的是1.8。

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.mycom.myservlet</groupId>
  <artifactId>myservlet</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>myservlet Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>myservlet</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>
Pom.xml文件

 

 部署Tomcat

1、點擊右上角的“Edit Configurations”,會彈出下圖窗口。

2、點擊加號、選中“tomcat server”,選“local”

 

3、修改一下Tomcat的基本信息

 

 

 4、點擊OK,右上角變成可tomcat運行。一般默認debug運行,便於調試。

 結果(因為默認index.jsp只有helloworld,所以顯示helloworld):

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM