• 通过自动回复机器人学Mybatis:原始版本(包括JDBC、pom.xml等)


    imooc视频学习笔记 ----> URL:http://www.imooc.com/learn/154

    list.jsp

    <%@ page contentType="text/html; charset=UTF-8" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    %>
    
    <!-- basePath = http://localhost:8080/MicroMessage/ -->
    <!-- Eclipse注释快捷键 Ctrl + Shift + / -->
    <!-- request.getScheme() 返回当前链接使用的协议;一般应用返回http;SSL返回https -->
    <!-- request.getContextPath()  返回站点的根目录 -->
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <meta http-equiv="X-UA-Compatible"content="IE=9; IE=8; IE=7; IE=EDGE" />
            <title>内容列表页面</title>
            <link href="<%= basePath %>resources/css/all.css" rel="stylesheet" type="text/css" />
        </head>
        <body style="background: #e1e9eb;">
            <form action="<%= basePath %>List.action" id="mainForm" method="post">
                <div class="right">
                    <div class="current">当前位置:<a href="javascript:void(0)" style="color:#6E6E6E;">内容管理</a> &gt; 内容列表</div>
                    <div class="rightCont">
                        <p class="g_title fix">内容列表 <a class="btn03" href="#">新 增</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class="btn03" href="#">删 除</a></p>
                        <table class="tab1">
                            <tbody>
                                <tr>
                                    <td width="90" align="right">指令名称:</td>
                                    <td>
                                        <input name="command" type="text" class="allInput" value=""/>
                                    </td>
                                    <td width="90" align="right">描述:</td>
                                    <td>
                                        <input name="description" type="text" class="allInput" value=""/>
                                    </td>
                                    <td width="85" align="right"><input type="submit" class="tabSub" value="查 询" /></td>
                                   </tr>
                            </tbody>
                        </table>
                        <div class="zixun fix">
                            <table class="tab2" width="100%">
                                <tbody>
                                    <tr>
                                        <th><input type="checkbox" id="all" onclick="#"/></th>
                                        <th>序号</th>
                                        <th>指令名称</th>
                                        <th>描述</th>
                                        <th>操作</th>
                                    </tr>
                                    <c:forEach varStatus="status" var="message" items="${messageList}">
                                        <tr <c:if test="${status.index % 2 == 1}">style="background-color:#ECF6EE;"</c:if>>
                                            <td><input type="checkbox" /></td>
                                            <td>${status.index + 1 }</td>
                                            <td>${message.command}</td>
                                            <td>${message.description}</td>
                                            <td>
                                                <a href="#">修改</a>&nbsp;&nbsp;&nbsp;
                                                <a href="#">删除</a>
                                            </td>
                                        </tr>
                                    </c:forEach>
                                </tbody>
                            </table>
                            <div class='page fix'><b>4</b><a href='###' class='first'>首页</a>
                                <a href='###' class='pre'>上一页</a>
                                当前第<span>1/1</span><a href='###' class='next'>下一页</a>
                                <a href='###' class='last'>末页</a>
                                跳至&nbsp;<input type='text' value='1' class='allInput w28' />&nbsp;&nbsp;
                                <a href='###' class='go'>GO</a>
                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </body>
    </html>

    listServlet.java

    package com.imooc.servlet;
    
    import java.io.IOException;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.imooc.bean.Message;
    
    // ctrl+shift+x 转为大写 ctrl+shift+y 
    // 断点调试 
    // 表示当前实现继续运行直到下一个断点,快捷键为F8。
    // 表示运行下一行代码,快捷键为F6
    // 表示退出当前方法,返回到调用层,快捷键为F7
    /**
     * 
     * 列表页面初始化控制
     *
     */
    @SuppressWarnings("serial")
    public class ListServlet extends HttpServlet {
        private static final String JDBC_DRIVER = "org.gjt.mm.mysql.Driver";
        private static final String DB_URL = "jdbc:mysql://localhost:3306/miro_message";
        
        private static final String USER = "root";
        private static final String PASS = "pass";
        
        private static Connection conn = null;
    
        static {
            try {
                Class.forName(JDBC_DRIVER);
                conn = DriverManager.getConnection(DB_URL, USER, PASS);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            // 从前台拿数据
            req.setCharacterEncoding("UTF-8");
            String command = req.getParameter("command");
            String description = req.getParameter("description");
            
            // sql拼接
            StringBuilder sqlBuilder = new StringBuilder();
            sqlBuilder.append("select ID, COMMAND, DESCRIPTION, CONTENT from MESSAGE where 1=1");
            
            List<String> paramList = new ArrayList<>();
            
            if (command != null && !"".equals(command.trim())) {
                // 第一次进入页面的时候command是为空的,加上trim()是为了除掉空格
                sqlBuilder.append(" and COMMAND=?");
                paramList.add(command);
            }
            
            if (description != null && !"".equals(description.trim())) {
                sqlBuilder.append(" and DESCRIPTION like '%' ? '%'");
                // like是近似查询,mysql字符串拼接不用+,用空格就Ok
                // 例如'%三%' 会把唐三藏啊,张三,等有三的记录全查出来
                // %可匹配任意类型和长度的字符,0到n
                paramList.add(description);
            }
            
            String sql = sqlBuilder.toString();
            
            PreparedStatement prep = null;
            ResultSet result = null;
            List<Message> messageList = new ArrayList<>();
            try {
                prep = conn.prepareStatement(sql);
                for (int i = 0; i != paramList.size(); ++i) {
                    prep.setString(i+1, paramList.get(i)); 
                }
                result = prep.executeQuery();
                while (result.next()) {
                    Message message = new Message();
                    messageList.add(message);
                    
                    message.setId(result.getString("ID"));
                    message.setCommand(result.getString("COMMAND"));
                    message.setDescription(result.getString("DESCRIPTION"));
                    message.setContent(result.getString("CONTENT"));
                }
                
                req.setAttribute("messageList", messageList);
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            req.setCharacterEncoding("UTF-8");
            req.getRequestDispatcher("/WEB-INF/jsp/back/list.jsp").forward(req, resp);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            // TODO Auto-generated method stub
            this.doGet(req, resp);
        }
    }

     web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
      <display-name>MicroMessage</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      
      <servlet>
          <servlet-name>ListServlet</servlet-name>
          <servlet-class>com.imooc.servlet.ListServlet</servlet-class>
      </servlet>
      
      <servlet-mapping>
          <servlet-name>ListServlet</servlet-name>
          <url-pattern>/List.action</url-pattern>
      </servlet-mapping>
    </web-app>

    pom.xml

    <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>MicroMessage</groupId>
      <artifactId>MicroMessage</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <packaging>war</packaging>
      <dependencies>
      <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.0.4</version>
    </dependency>
          <dependency>
          <groupId>org.apache.taglibs</groupId>
          <artifactId>taglibs-standard-spec</artifactId>
          <version>1.2.5</version>
        </dependency>
        <dependency>
          <groupId>org.apache.taglibs</groupId>
          <artifactId>taglibs-standard-impl</artifactId>
          <version>1.2.5</version>
        </dependency>
        <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>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
      <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
          <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
            <configuration>
              <source>1.8</source>
              <target>1.8</target>
            </configuration>
          </plugin>
          <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
              <warSourceDirectory>WebContent</warSourceDirectory>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </project>
  • 相关阅读:
    错误、异常与自定义异常
    关于使用第三方库、代码复用的一些思考
    [Scheme]一个Scheme的Metacircular evaluator
    [Scheme]Understanding the Yin-Yang Puzzle
    [Lua]50行代码的解释器,用来演示lambda calculus
    将jar包安装到本地仓库
    PowerDesigner安装教程(含下载+汉化+破解)
    Jmeter如何录制APP客户端脚本
    jdk1.8 stream 求和
    VMware的快照和克隆总结
  • 原文地址:https://www.cnblogs.com/xkxf/p/7081415.html
Copyright © 2020-2023  润新知