IDEA 社区版使用maven 和 tomcat7-maven-plugin运行servlet demo程序

STEP1:

选择创建Maven项目,勾选Create from archetype中的org.apache.maven.archetype:maven archetype-webapp
IDEA 社区版使用maven 和 tomcat7-maven-plugin运行servlet demo程序_第1张图片
IDEA 社区版使用maven 和 tomcat7-maven-plugin运行servlet demo程序_第2张图片

选择maven settings.xml文件的位置
IDEA 社区版使用maven 和 tomcat7-maven-plugin运行servlet demo程序_第3张图片

构建好的项目结构如下图所示:
在这里插入图片描述

STEP2

在pom.xml文件中添加tomcatc插件

        
          org.apache.tomcat.maven
          tomcat7-maven-plugin
          2.1
          
            8080
            /
            UTF-8
            tomcat7
          
        

除此以外,添加上servlet的jar包

    
      javax.servlet
      servlet-api
      2.5
      provided
    
    
    javax.servlet.jsp
    jsp-api
    2.1
    provided
    

配置tomcat命令
IDEA 社区版使用maven 和 tomcat7-maven-plugin运行servlet demo程序_第4张图片

STEP3

添加hello.java文件

package com.zx;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class hello extends HttpServlet {

    private String message;

    public void init() throws ServletException
    {

        message = "Hello my first servlet";
    }

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
            throws ServletException, IOException
    {

        response.setContentType("text/html");


        PrintWriter out = response.getWriter();
        out.println("

" + message + "

"); } public void destroy() { } }

IDEA 社区版使用maven 和 tomcat7-maven-plugin运行servlet demo程序_第5张图片
进而在web.xml 文件中添加servlet映射

  
    hello
    com.zx.hello
  

  
    hello
    /hi
  

使用maven 进行构建
IDEA 社区版使用maven 和 tomcat7-maven-plugin运行servlet demo程序_第6张图片
运行tomcat

IDEA 社区版使用maven 和 tomcat7-maven-plugin运行servlet demo程序_第7张图片
在浏览器中
IDEA 社区版使用maven 和 tomcat7-maven-plugin运行servlet demo程序_第8张图片

你可能感兴趣的:(Java)