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>

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

  • 相关阅读:
    CSS
    CSS样式
    CentOS/Ubuntu 搭载环境所遇问题
    XHTML 注意的地方
    HTML 全局属性 全局事件属性
    shell命令之---Linux文件权限
    shell命令之---使用Linux环境变量
    shell命令之---处理数据文件
    shell命令之---检测磁盘空间
    shell命令之---文件内容查看
  • 原文地址:https://www.cnblogs.com/longying2008/p/5055528.html
Copyright © 2011-2022 走看看