SpringMVC-Spring-Hibernate项目搭建之一-- 搭建maven 项目 & servlet的demo

一. 搭建maven项目 

1. 新建maven项目,选择maven Project --> Next

  

 

2. 勾选 Create a simple project --> Next

  

 

3. 填写 Group Id 和 Artifact Id 选择Packaging 为war 之后Finish

  

  构建完成之后项目结构如下所示:

  

4. 选中项目 右键 properties ,选择 Project Facets 勾选 Dynamic Web Module 点击 左下角 Further configuration available...将项目转换成 Dynamic项目架构;

   

  勾选 Generate web.xml deployment descriptor点击OK按钮,自动生成 web.xml文件

  

  转换成 Dynamic Web项目之后的项目结构如下所示:

    

5. 调整项目结构:将 META-INF 和 WEB-INF 复制到webapp目录下,删除WebContent,最终Web项目结构如下所示:

  

二. 配置pom.xml文件 

 1.  <properties>标签中定义变量名,下文中可以直接引用;

 2.  <dependencies>加入依赖的 jar包

<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.daydayup</groupId> <artifactId>ssh</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <jdk.version>1.7</jdk.version> <spring.version>4.2.5.RELEASE</spring.version> <hibernate.version>5.1.0.Final</hibernate.version> </properties> <dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> </dependencies> </project>

 

三. 编写一个简单的web demo 

 1. 定义一个servlet  继承HttpServlet -- 只有继承Servlet才能识别到(HttpServlet 是 Servlet的子类);

package com.ggd;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {

    private static final long serialVersionUID = -7853701805213269376L;

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        
        response.getOutputStream().write("Haha Servlet success!!!".getBytes());
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        
    }
}

 2. web.xml 文件的配置如下  

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    
    <servlet>
        <servlet-name>helloWorld</servlet-name>
        <servlet-class>com.ggd.HelloWorld</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>helloWorld</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

3. 启动tomcat, 访问浏览器http://localhost:8080/ssh/123

  --最后‘123’是随便输入的<url-pattern>标签中是'/', 浏览器可以识别到所有以http://localhost:8080/ssh/开头的URL地址

  

  自此,一个简单的servlet demo实现

 

posted @ 2016-04-28 15:19  小小小飞鱼  阅读(264)  评论(0编辑  收藏  举报