zoukankan      html  css  js  c++  java
  • 1.Hello World

    Create a new Maven webapp project in Eclipse

    1. File->New->Maven Project,保持默认选项。Click "Next" to continue


    2. 选择Maven项目的原型(archetype),Catalog选择Internal,Artifact Id选择maven-archetype-webapp。Click "Next" to continue


    3. 填写Artifact Id等信息,如下。Click "Finish"。
    4. Maven项目创建完成。由于servlet-api的jar没有添加到classpath,eclipse会提示编译错误。在pom.xml中添加该jar包的依赖。另外,在src/main目录下新建java目录。

      pom.xml
       1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
       3     <modelVersion>4.0.0</modelVersion>
       4     <groupId>com.xuan.flight.tutorial</groupId>
       5     <artifactId>hello-world-web</artifactId>
       6     <packaging>war</packaging>
       7     <version>1.0.0</version>
       8     <name>hello-world-web Maven Webapp</name>
       9     <url>http://maven.apache.org</url>
      10     <dependencies>
      11         <dependency>
      12             <groupId>junit</groupId>
      13             <artifactId>junit</artifactId>
      14             <version>4.8.1</version>
      15             <scope>test</scope>
      16         </dependency>
      17         <dependency>
      18             <groupId>javax.servlet</groupId>
      19             <artifactId>javax.servlet-api</artifactId>
      20             <version>3.1.0</version>
      21             <scope>provided</scope>
      22         </dependency>
      23     </dependencies>
      24     <build>
      25         <finalName>hello-world-web</finalName>
      26     </build>
      27 </project>

    Add Hello World Servlet

    1. Create Servlet, then click "Finish"。此时Servlet的配置信息已经自动添加到web.xml文件中

      web.xml
      <!DOCTYPE web-app PUBLIC
       "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
       "http://java.sun.com/dtd/web-app_2_3.dtd" >
      <web-app>
        <display-name>Archetype Created Web Application</display-name>
        <servlet>
          <servlet-name>HelloWorld</servlet-name>
          <display-name>HelloWorld</display-name>
          <description></description>
          <servlet-class>com.xuan.flight.tutorial.web.HelloWorld</servlet-class>
        </servlet>
        <servlet-mapping>
          <servlet-name>HelloWorld</servlet-name>
          <url-pattern>/HelloWorld</url-pattern>
        </servlet-mapping>
      </web-app>
       
    2. Edit the source code: "HelloWorld.java"

      HelloWorld.java
       1 package com.xuan.flight.tutorial.web;
       2 import java.io.IOException;
       3 import javax.servlet.ServletException;
       4 import javax.servlet.http.HttpServlet;
       5 import javax.servlet.http.HttpServletRequest;
       6 import javax.servlet.http.HttpServletResponse;
       7 /**
       8  * Servlet implementation class HelloWorld
       9  */
      10 public class HelloWorld extends HttpServlet
      11 {
      12     private static final long serialVersionUID = 1L;
      13     /**
      14      * @see HttpServlet#HttpServlet()
      15      */
      16     public HelloWorld()
      17     {
      18         super();
      19         // TODO Auto-generated constructor stub
      20     }
      21     /**
      22      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
      23      *      response)
      24      */
      25     protected void doGet(HttpServletRequest request, HttpServletResponse response)
      26         throws ServletException, IOException
      27     {
      28         response.getWriter().println("Hello World");
      29         response.getWriter().append("Served at: ").append(request.getContextPath());
      30     }
      31     /**
      32      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
      33      *      response)
      34      */
      35     protected void doPost(HttpServletRequest request, HttpServletResponse response)
      36         throws ServletException, IOException
      37     {
      38         // TODO Auto-generated method stub
      39         doGet(request, response);
      40     }
      41 }
    3. 进入项目所在目录(pom.xml文件所在位置),运行命令mvn clean package。在target目录下生成了文件hello-world-web.war。
    4. 将hello-world-web.war文件copy到${TOMCAT_HOME}/webapps目录下,重启tomcat。
    5. 在浏览器中访问http://localhost:8080/hello-world-web/HelloWorld,可以看到如下输出。

      Hello World
      Served at: /hello-world-web
     
  • 相关阅读:
    Sokect简单入门(1)TCP协议一
    Tomcat内存溢出 解决方法
    服务器配置 ssl 证书,Nginx配置
    优秀的博客文章记录
    SpringBoot实现优雅的关机
    关于 redis 的 数据类型 和 内存模型
    微信, qq 支付宝 等相关开发 资源 记录
    设计模式 之 单列设计模式
    Swagger
    MQ服务器奔溃解决过程
  • 原文地址:https://www.cnblogs.com/xxuan/p/6739633.html
Copyright © 2011-2022 走看看