IntelliJ IDEA运行第一个web项目

1. 配置pom文件,添加maven依赖

        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">

4.0.0

org.smart4j

chapter1

1.0-SNAPSHOT

UTF-8

org.apache.maven.plugins

maven-compiler-plugin

3.6.2

1.8

1.8

org.apache.maven.plugins

maven-surefire-plugin

2.22.0

true

war

javax.servlet

javax.servlet-api

3.1.0

javax.servlet.jsp

jsp-api

2.1

javax.servlet

jstl

1.2

2. 右击pom.xml,点击Reimport,下载依赖库

3. 编写servlet类

package org.smart4j.chapter1;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

@WebServlet("/hello")

public class HelloServletextends HttpServlet {

@Override

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

DateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String currentTime = dateFormat.format(new Date());

request.setAttribute("currentTime", currentTime);

request.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(request, response);

}

}

4. 编写jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

hello

Hello!

${currentTime}

5. 点击IDEA运行按钮,部署至tomcat

6. 在输入http://localhost:8080/chapter1_war/hello测试


你可能感兴趣的:(IntelliJ IDEA运行第一个web项目)