Java基础入门day65

day65

web项目

页面设计

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

<welcome-file-list>
 <welcome-file>TypesServlet</welcome-file>
</welcome-file-list>
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> 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<Types> 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<Types> getAllTypes() {
     return typesDao.getAllTypes();
 }
}
package com.saas.dao;
​
import com.saas.entity.Types;
​
import java.util.List;
​
public interface ITypesDao {
 List<Types> 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<Types> getAllTypes() {
     try {
         return qr.query("select * from types", new BeanListHandler<Types>(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
页面代码

将页面展示出来

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值