• Java基础入门day65


    day65

    web项目

    页面设计

    仿照小米官网,将首页保存到本地为一个html页面,再将html页面保存为jsp页面,在项目中的web.xml文件中配置了欢迎页

    
     TypesServlet
    
    package com.saas.servlet;
    ​
    import com.saas.entity.Types;
    import com.saas.service.ITypesService;
    import com.saas.service.impl.TypesServiceImpl;
    ​
    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.util.List;
    ​
    @WebServlet("/TypesServlet")
    public class TypesServlet extends HttpServlet {
    ​
     private ITypesService typesService = new TypesServiceImpl();
    ​
     @Override
     protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         List types = typesService.getAllTypes();
    ​
         req.setAttribute("types", types);
    ​
         req.getRequestDispatcher("types.jsp").forward(req, resp);
     }
    }
    package com.saas.service;
    ​
    import com.saas.entity.Types;
    ​
    import java.util.List;
    ​
    public interface ITypesService {
     List getAllTypes();
    }
    package com.saas.service.impl;
    ​
    import com.saas.dao.ITypesDao;
    import com.saas.dao.impl.TypesDaoImpl;
    import com.saas.entity.Types;
    import com.saas.service.ITypesService;
    ​
    import java.util.List;
    ​
    public class TypesServiceImpl implements ITypesService {
    ​
     private ITypesDao typesDao = new TypesDaoImpl();
    ​
     @Override
     public List getAllTypes() {
         return typesDao.getAllTypes();
     }
    }
    package com.saas.dao;
    ​
    import com.saas.entity.Types;
    ​
    import java.util.List;
    ​
    public interface ITypesDao {
     List getAllTypes();
    }
    package com.saas.dao.impl;
    ​
    import com.saas.dao.ITypesDao;
    import com.saas.entity.Types;
    import com.saas.util.DruidUtil;
    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.BeanListHandler;
    ​
    import java.sql.SQLException;
    import java.util.List;
    ​
    public class TypesDaoImpl implements ITypesDao {
    ​
     private QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());
    ​
    ​
     @Override
     public List getAllTypes() {
         try {
             return qr.query("select * from types", new BeanListHandler(Types.class));
         } catch (SQLException e) {
             throw new RuntimeException(e);
         }
     }
    }
    package com.saas.util;
    ​
    import com.alibaba.druid.pool.DruidDataSource;
    import com.alibaba.druid.pool.DruidDataSourceFactory;
    ​
    import javax.sql.DataSource;
    ​
    public class DruidUtil {
    ​
     private static Env env = Env.getInstance();
    ​
     public static DataSource getDataSource()
     {
         try {
             DruidDataSource dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(env);
    ​
             dataSource.setUrl(env.getProperty("url"));
             dataSource.setUsername(env.getProperty("user"));
             dataSource.setPassword(env.getProperty("pass"));
             dataSource.setDriverClassName(env.getProperty("driver"));
    ​
             return dataSource;
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
     }
    }
    package com.saas.util;
    ​
    import java.io.IOException;
    import java.util.Properties;
    ​
    public class Env extends Properties {
    ​
     private static final long serialVersionUID = 1L;
    ​
     private static Env env = new Env();
    ​
     private Env() {
         super();
         try {
             load(getClass().getResourceAsStream("/db.properties"));
         } catch (IOException e) {
             throw new RuntimeException(e);
         }
     }
    ​
     public static Env getInstance() {
         return env;
     }
    ​
     public static String get(String key) {
         return env.getProperty(key);
     }
    }
    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/mi?characterEncoding=utf-8
    user=root
    pass=Abc@1234
    页面代码

    将页面展示出来

  • 相关阅读:
    USB到UART桥接控制器——GP232RNL
    uniapp的实战总结大全
    Vulnhub: Masashi: 1靶机
    PCIe ECAM机制
    【码蹄集新手村600题】强制类型转换的重要性
    esp32发布机器人电池电压到ros2(micro-ros+CoCube)
    接口测试主要测试哪方面?需要哪些技能?要怎么学习?
    js中高级部分知识点总结第二篇
    java毕业设计晨曦文学社在线投稿系统的设计与实现mybatis+源码+调试部署+系统+数据库+lw
    关于flink重新提交任务,重复消费kafka的坑
  • 原文地址:https://blog.csdn.net/zpz2001/article/details/139833633