项目实战(javaweb)

项目实战

一.搭建一个maven项目

maven模板 webapp
把pom文件多余的删掉,只留gav和打包方式war
main文件下创建java,resource并分配文件类型
更新web.xml配置文件为4.0版本

二.配置Tomcat

配置deployment

三测试项目是否可以启动

四.导入项目需要用的jar包

servlet,jsp,mysql驱动

五创建项目包结构(业务层.dao层…)

六.在pojo中编写实体类(和数据库对应的)

ORM映射:表-类映射

七.编写基础公共类.

​ 1.数据库配置文件

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/smbms?useUnicode=true&charsetacterEncoding =utf8&useSSl=true
username=root
password=1234

2.编写数据库公共类

package com.wst.dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

//操作数据库的公共类
public class BaseDao {
    private  static String driver;
    private  static String url;
    private  static String username;
    private  static String password;
//静态代码块,类加载的时候就初始化了.
    static{
        //通过类加载器读取对应的资源
       InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties");
        Properties properties = new Properties();
    try {
        properties.load(is);
    } catch (IOException e) {
        e.printStackTrace();
    }
    //从流中拿出来
    driver=properties.getProperty("driver");
    url=properties.getProperty("url");
    username=properties.getProperty("username");
    password=properties.getProperty("password");

}
//获取数据库连接
    public static Connection getConnection(){
        Connection connection=null;
        try {
            Class.forName(driver);
             connection = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }
    //编写公共查询类,
    public static ResultSet execute(Connection connection,String sql,Object[] params,ResultSet resultSet,PreparedStatement preparedStatement)
            throws SQLException {

        preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i+1,params[i]);
        }
        //预编译的后边的executeQuery();不需要填executeQuery(sql);了
        resultSet = preparedStatement.executeQuery();
        return resultSet;
    }
    //编写增删改查公共方法
    public static int execute(Connection connection,String sql,Object[] params,PreparedStatement preparedStatement)
            throws SQLException {

        preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i+1,params[i]);
        }
        int updateRows = preparedStatement.executeUpdate();
        return updateRows;
    }
    //释放资源
    public static  boolean closeResource(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){
       boolean flag = true;
        if(resultSet!=null){//在运行
            try {
                resultSet.close();
                //gc回收
                resultSet=null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag=false;
            }
        }

        if(preparedStatement!=null){
            try {
                preparedStatement.close();
                //gc回收
                preparedStatement=null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag=false;
            }
        }

        if(connection!=null){
            try {
                connection.close();
                //gc回收
                connection=null;
            } catch (SQLException e) {
                e.printStackTrace();
                flag=false;
            }
        }
          return flag;
    }
}

3.编写字符编码过滤器,实现Filter接口,在web.xml注册 ,(/*)全滤

八.导入静态资源

(js,css,image)

九.登录功能实现

1编写前端页面
2web.xml中设置首页

<!--    设置欢迎页面-->
    <welcome-file-list>
        <welcome-file>login.jsp</welcome-file>
    </welcome-file-list>

3,编写dao接口userDao
4.编写dao接口的实现类userDaoimpl(调用数据库公共类来查询)
5.业务层接口
6业务层接口实现类
7.编写servlet,web.xml中注册servlet
运行,ok.

十.注销功能

1.LogoutServlet(移除Session返回注销页面)
2.注册web.xml.

十一.登录拦截优化(过滤器)(servlet是继承,Filter是实现接口)

编写过滤器,在web.xml中注册.
十二.密码修改
1.UserDao接口添加方法
2.UserDao接口的实现类userDaoimpl添加方法的实现
3.业务层接口 增加方法
4.业务层接口实现类 增加方法
5.编写servlet,web.xml中注册servlet
6.实现复用userServlet中提取出方法
public void updatePwd(HttpServletRequest req, HttpServletResponse resp)

十二.用户管理页面.(难点)

1.1导入分页的工具类
1.2用户页面的导入
2.1获取用户数量

添加东西:
UserDao
UserDaoImpl
UserService
UserServiceImpl
十三获取角色操作
为了职责统一,可以把角色的操作单独放在一个包中,和pojo类对应

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值