zoukankan      html  css  js  c++  java
  • 从零开始写JavaWeb框架(第一章节)

    买了本《从零开始写JavaWeb框架》

    因为是第一次用IDEA,期间遇到很多问题,比如:怎么在IDEA中配置tomcat:

    在IDEA界面的右上角点击:

     

    点击+,选择Maven

    到了如下界面:

    在Name中输入tomcat,在Command line中输入tomcat7:run,然后点击apply。

    然后就可以运行了。

    第一章节如下:

    pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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>org.smart4j</groupId>
        <artifactId>chapter1</artifactId>
        <version>1.0.0</version>
        <packaging>war</packaging>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        </properties>
        <dependencies>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.2</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
                <scope>runtime</scope>
            </dependency>
        </dependencies>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.18.1</version>
                    <configuration>
                        <skipTests>true</skipTests>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                    <configuration>
                        <path>/${project.artifactId}</path>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </project>

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
             version="3.0">
    </web-app>

    HelloServlet.java

    package org.smart4j.chapter1;
    
    /**
     * Created by sahara on 2016/3/13.
     */
    import java.io.IOException;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @WebServlet("/hello")
    public class HelloServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String currentTime = dateFormat.format(new Date());
            req.setAttribute("currentTime", currentTime);
            req.getRequestDispatcher("/WEB-INF/jsp/hello.jsp").forward(req, resp);
        }
    }

    hello.jsp

     <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     <html>
       <head>
         <title>Hello</title>
       </head>
       <body>
            <h1>${currentTime}</h1>
       </body>
     </html>

    最后:

  • 相关阅读:
    基于ZooKeeper和Thrift构建动态RPC调用
    菜鸡的Java笔记 api 文档
    菜鸡的Java笔记 第二十四
    菜鸡的Java笔记 第二十三
    菜鸡的Java笔记 第二十二
    菜鸡的Java笔记 实践
    Python 函数 参数传递
    python 函数的定义及调用语法,map 方法,函数嵌套递归
    java关键字
    菜鸡的Java笔记 第二十一 final 关键字
  • 原文地址:https://www.cnblogs.com/XJJD/p/7532361.html
Copyright © 2011-2022 走看看