浅谈Response , 【Spring5.0框架&&Druid 连接池】完成 web页面登录案例

Response ;

 

 


声明,使用的Spring5.0框架,JDK9 ,Mysql8版本数据库,Druid连接池,Juery3.0框架&bootstrap;

案例分析:

登录页面login.html使用post 请求,输入userName和password 在Mysql数据库中查找,查找结果2种:1,找到,使用 转发至登陆成功页面;2,没找到,提示用户登录失败;

① web页面搭建

② java 分层开发,配置applicationContext.xml  ,然后 创建 登录的 servlet ,转发的  servlet  2个,login成功和 login失败的;

项目目录结构如下:

test 中 是servlet   ;  web中 的 config  是 juery 框架, bootstrap框架等 ;login.html 登录页面【在web 目录下,需要在web.xml中配置 :】

 

<welcome-file-list>
    <welcome-file>login.html</welcome-file>
</welcome-file-list>

 

(1)bean类,和数据库中  table对应,【 id int ,userName  vachar, password varchar】;

package com.baidu.bean;
/**
 * @auther SyntacticSugar
 * @data 2018/9/27 0027下午 4:09
 */
public class User {
    private int id;
    private String userName;
    private String password;
    //setter
    //tostring
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", userName='" + userName + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

dao层;

package com.baidu.dao;
import com.baidu.bean.User;
import java.util.List;
public interface UserDao {
    List<User> selectUserByUser(User user);
}

daoImpl层;

package com.baidu.daoImpl;
import com.baidu.bean.User;
import com.baidu.dao.UserDao;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
/**
 * @auther SyntacticSugar
 * @data 2018/9/27 0027下午 4:22
 */
public class UserDaoImpl implements UserDao {
    private JdbcTemplate jdbcTemplate;
    // setter
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    @Override
    public List<User> selectUserByUser(User user) {
       // List<User> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class), id);
        String sql="select * from t_user where userName=?and password=?;";
        List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class), user.getUserName(), user.getPassword());
        return list; //通过userName和password查询出来的数据可能为多个
    }
}

service层;

package com.baidu.service;
import com.baidu.bean.User;
import java.util.List;
/**
 * @auther SyntacticSugar
 * @data 2018/9/27 0027下午 4:35
 */
public interface UserService {
    List<User> login(User user);
}

serviceImpl层;

package com.baidu.serviceImpl;
import com.baidu.bean.User;
import com.baidu.dao.UserDao;
import com.baidu.service.UserService;
import java.util.List;

/**
 * @auther SyntacticSugar
 * @data 2018/9/27 0027下午 4:36
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao;
    //  提供  setter 以便于注入
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    @Override
    public List<User> login(User user) {
        List<User> list = userDao.selectUserByUser(user);//对dao层的数据进行判断
        return list;
    }
}

配置文件;

连接池,本测试加载了jdbc.properties ;

jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=UTF-8&useSSL=false
jdbc.username=root
jdbc.password=root

filters=stat
initialSize=2
maxActive=300
maxWait=60000
timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=300000
validationQuery=SELECT 1
testWhileIdle=true
testOnBorrow=false
testOnReturn=false
poolPreparedStatements=false
maxPoolPreparedStatementPerConnectionSize=200
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/reba?serverTimezone=UTC&characterEncoding=UTF-8&useSSL=false
jdbc.username=root
jdbc.password=root

配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context.xsd">
    <!--加载  druid  配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true"/>
    <bean id="userService" class="com.baidu.serviceImpl.UserServiceImpl">
        <property name="userDao" ref="userDao"></property>
    </bean>
    <bean id="userDao" class="com.baidu.daoImpl.UserDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg index="0" ref="dataSource"></constructor-arg>
    </bean>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClass}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>      <!-- more bean definitions go here -->
</beans>

创建login的servlet,login 成功以及失败的servlet;

登录的;

package com.baidu.test;
import com.baidu.bean.User;
import com.baidu.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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(name = "ServletUserLogin", urlPatterns ="/u")
public class ServletUserLogin extends HttpServlet {
    private  UserService userService;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);//
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        userService = (UserService) context.getBean("userService");
        //防止乱码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //获取到service,和  bean
        //获取请求数据
        User user = new User();

        String username = request.getParameter("userName");
        String password = request.getParameter("password");
        user.setUserName(username);
        user.setPassword(password);
        List<User> list = userService.login(user);
        System.out.println(list);

        //判断 list
        //有这个账户就转发至 success,没有就error
        if (list != null && list.size() >0) {//跳转
            System.out.println(list);
            //request.getRequestDispatcher("/success.html").forward(request, response);
            request.setAttribute("username",username );
            request.getRequestDispatcher("/success").forward(request,response );
        } else {
           // request.getRequestDispatcher("/error.html").forward(request, response);
            System.out.println("没有这个账户,请从新输入");
            request.getRequestDispatcher("/error").forward(request,response );
        }
    }
}

失败的;

package com.baidu.test;
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;
@WebServlet(name = "Error", urlPatterns = "/error")
public class Error extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);//
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");//
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().print("登录失败");
    }
}

成功的;

package com.baidu.test;
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;
@WebServlet(name = "Success", urlPatterns = "/success")
public class Success extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);//
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 从域获取消息
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        Object username = request.getAttribute("username");//从request域中获取信息
        response.getWriter().print(username+"登陆成功");
    }
}

启动登录页面的 servlet  ,点击登录页面,输入userName 和 password ,登录;

 ①测试

登录成功:

 


②测试

登录失败;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值