一个简陋的注册功能

controller层

public class RegisterServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        /**
         * 1.获取参数
         * 2.
         *     可用:
         *       调用service保存到数据库
         *
         *     不可用:
         *       跳回注册页面
         */
        //1.获取参数
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=UTF-8");


        String username = req.getParameter("username");

        System.out.println(username);
        String phone = req.getParameter("phone");
        System.out.println(phone);

        String password = req.getParameter("password");
        System.out.println(password);
//        resp.getWriter().write("注册成功了");


         UserDaoImpl userDaoImpl= new UserDaoImpl();
         UserService userService=new UserServiceImpl(userDaoImpl);

        userService.add(new User(username,phone,password));


        //跳回注册页面
//       req.getRequestDispatcher("/html/register.html").forward(req,resp);


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}

service层
userService接口

public interface UserService {
    //添加用户
    int  add(User user);
}

UserServiceImpl实现类

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    public UserServiceImpl() {

    }

    @Override
    public int  add(User user) {

        int add=userDao.insert(user);
        return add;

//        userDao.insert(user);

    }
}

dao层
userDao接口

public interface UserDao {
    int insert(User user);
 }

userDaoImpl实现类

public class UserDaoImpl implements UserDao {
    @Override
    public int insert(User user) {
        String username=user.getUsername();
        String phone=user.getPhone();
        String password = user.getPassword();
        String sql="INSERT INTO tb_user(username,phone,password)VALUES(?,?,?)";
        int i= DbUtil.execteUpdate(sql,username,phone,password);
        return i;
    }
}

entity实体类

public class User {
    private  String username;
    private  String phone;
    private  String password;

    public User(){}

    public User(String username, String phone, String password) {
        this.username = username;
        this.phone = phone;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

util工具类

public class DbUtil {
    private  static Properties properties;

    static{
        properties=new Properties();
        try {
            properties.load(new FileInputStream("resources/db.properties"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public  static Connection getConn(){

        Connection connection=null;
        try {
              //注册驱动
              Class.forName("com.mysql.jdbc.Driver");
             //读取db.properties配置文件,得到一个Input流
              InputStream resourceAsStream =  DbUtil.class.getClassLoader().getResourceAsStream("db.properties");
            //从流中加载数据
            properties.load(resourceAsStream);
            connection = DriverManager.getConnection(properties.getProperty("url"),
                    properties.getProperty("user"), properties.getProperty("password"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }


    public static int execteUpdate(String sql,Object...paras){
        Connection conn=getConn();
        int i=0;
        try {
            PreparedStatement preparedStatement = conn.prepareStatement(sql);

            if (paras!=null && paras.length>0){
                for (int j=0;j<paras.length;j++){
                    preparedStatement.setObject(j+1,paras[j]);
                }
            }
            i=preparedStatement.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

web界面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<div>
   <form action="/aaa/reg" method="post">
        姓名:<input type="text" name="username" placeholder="username"><br>
        电话:<input type="text" name="phone" placeholder="phone"><br>
        密码:<input type="password" name="password" placeholder="password"><br>
        <input type="submit" value="注册">
    </form>
</div>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
坦克大战是一个经典的游戏,下面我来写一个基于Python的简单版坦克大战游戏代码。这个游戏是基于Pygame库实现的,需要先安装Pygame库。 ```python import pygame import random # 初始化Pygame pygame.init() # 定义游戏界面大小 screen_width = 640 screen_height = 480 # 创建游戏界面 screen = pygame.display.set_mode((screen_width, screen_height)) # 设置游戏界面标题 pygame.display.set_caption('坦克大战') # 加载坦克和子弹图片 tank_image = pygame.image.load('tank.png') bullet_image = pygame.image.load('bullet.png') # 定义坦克类 class Tank: def __init__(self, x, y): self.x = x self.y = y self.speed = 5 self.direction = random.choice(['up', 'down', 'left', 'right']) self.image = tank_image self.rect = self.image.get_rect() self.rect.x = self.x self.rect.y = self.y def move(self): if self.direction == 'up': self.y -= self.speed elif self.direction == 'down': self.y += self.speed elif self.direction == 'left': self.x -= self.speed elif self.direction == 'right': self.x += self.speed # 边界检测 if self.x < 0: self.x = 0 elif self.x > screen_width - self.rect.width: self.x = screen_width - self.rect.width if self.y < 0: self.y = 0 elif self.y > screen_height - self.rect.height: self.y = screen_height - self.rect.height self.rect.x = self.x self.rect.y = self.y def fire(self): return Bullet(self.x, self.y, self.direction) # 定义子弹类 class Bullet: def __init__(self, x, y, direction): self.x = x self.y = y self.speed = 10 self.direction = direction self.image = bullet_image self.rect = self.image.get_rect() self.rect.x = self.x self.rect.y = self.y def move(self): if self.direction == 'up': self.y -= self.speed elif self.direction == 'down': self.y += self.speed elif self.direction == 'left': self.x -= self.speed elif self.direction == 'right': self.x += self.speed # 边界检测 if self.x < 0 or self.x > screen_width or self.y < 0 or self.y > screen_height: return False self.rect.x = self.x self.rect.y = self.y return True # 创建坦克和子弹 player_tank = Tank(100, 100) enemy_tank = Tank(400, 300) bullets = [] # 游戏主循环 running = True while running: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: bullets.append(player_tank.fire()) # 移动坦克和子弹 player_tank.move() enemy_tank.move() for bullet in bullets: if not bullet.move(): bullets.remove(bullet) # 绘制游戏界面 screen.fill((255, 255, 255)) screen.blit(player_tank.image, player_tank.rect) screen.blit(enemy_tank.image, enemy_tank.rect) for bullet in bullets: screen.blit(bullet.image, bullet.rect) pygame.display.update() # 退出Pygame pygame.quit() ``` 在这个游戏中,我们定义了两个类:`Tank`表示坦克,`Bullet`表示子弹。在游戏主循环中,我们不断处理事件、移动坦克和子弹、绘制游戏界面。玩家可以按空格键发射子弹。这个游戏还很简陋,你可以根据自己的需要进行扩展和完善,例如添加音效、敌方坦克、障碍物等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值