zoukankan      html  css  js  c++  java
  • 使用 IDEA 创建 Maven Web 项目 (三) 编写一个简单的 WEB 应用

    编写 Servlet 类

    首先,需要在 java 目录下,创建一个名为 org.smart4j.chapter1 的包。然后,在该包下创建一个 HelloServlet  的类,代码如下:

    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;
    
    /**
     * Created by wgc on 2015/12/15.
     */
    @WebServlet("/hello")
    public class HelloServlet extends HttpServlet {
    
        @Override
        protected 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);
        }
    }

    编写 JSP 页面

    在 webapp 目录下创建 jsp 目录,并在该目录下创建 hello.jsp , 代码如下:

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Hello</title>
    </head>
    <body>
    <h1>Hello!</h1>
    <h2>当前时间:${currentTime}</h2>
    </body>
    </html>

    至此,所有的代码编写完毕。

  • 相关阅读:
    mac 外接显示屏的坑
    ssh 多秘钥管理和坑
    CircleCI 前端自动部署
    jest 事件测试
    await Vue.nextTick() 的含义分析
    Jest 里面需要注意的几个小细节
    element 库 date-picker 的 disabledDate 的坑
    jest 提示 Unexpected identifier 的解决方案
    preventDefault 和 stopPropagation
    数据库:Flask-SQLAlchemy
  • 原文地址:https://www.cnblogs.com/longying2008/p/5055528.html
Copyright © 2011-2022 走看看