test request&&response 代码实现

时间:2021-10-01 16:48:09

使用工具 IDEA  创建一个登录页面和后方数据库连接

test request&&response 代码实现

1.编写login.html文件  导入到web文件夹下

test request&&response 代码实现

设置配置文件  druid.properties

test request&&response 代码实现

导入jar包  放置到web文件夹下的WEB-INF中  lib

level:Add as  Library

test request&&response 代码实现

test request&&response 代码实现

2.创建数据库环境

create database db1;

use db1;

create table user(

  id int primary key auto_increment,

  username varchar(32) unique not null,

  password varchar(20)   not null

)

3.创建包package  domain 创建类User

public class User {
private int id;
private String username;
private String password; 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 + '\'' +
'}';
}
}

ALT+INS+FN快捷键  Getter and Setter  toString()

test request&&response 代码实现

4.创建package dao创建类Userdao 提供login()方法

UserDao.java:操作数据库user表的类

package cn.itcast.dao;

import cn.itcast.domain.Userr;
import cn.itcast.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.JDBCType;

/*操作数据库中Use表的类
* */
public class Uesrdao {
//声明JDBCTemplate对象来功用
private JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());

/*登录方法
*LoginUser只有用户名和密码
* return User 包括用户全部数据,没有查询到,返回null值
*
*
* */
public Userr login(Userr loginUesr){
try {
//1.编写sql
String sql = "select * from user where username=? and password=?";
//调用querry方法
Userr user = template.queryForObject(sql,
new BeanPropertyRowMapper<Userr>(Userr.class),
loginUesr.getUsername(), loginUesr.getPassword());
return user;
}
catch (DataAccessException e){
e.printStackTrace();
return null;
}

}

}

创建package util创建一个class文件 JDBCUtils

package cn.itcast.util;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/*JDBC的工具类,使用 Durild连接池*/
public class JDBCUtils {

private static DataSource ds;
static {

try {
//加载配置文件
Properties pro=new Properties();
//使用ClassLoader来加载配置文件,获取字节输入流
InputStream is=JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);
//初始化连接池对象
try {
ds=DruidDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}

}

/*获取连接池对象*/
public static DataSource getDataSource(){
return ds;
}
/*获取Connection对象*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();

}

}
test 文件用来测试Dao

package cn.itcast.test;
import cn.itcast.dao.Uesrdao;
import cn.itcast.domain.Userr;
import org.junit.Test;

public class UserDaoTest {
@Test
public void testLogin(){
Userr loginuser=new Userr();
loginuser.setUsername("superbab");
loginuser.setPassword("123");

Uesrdao dao=new Uesrdao();
Userr user=dao.login(loginuser);

System.out.println(user);
}

}

此时输出的结果:test request&&response 代码实现

此时距离成功已经很近了

我们在cn.itcast目录下创建一个web.servlet目录 用来存放servlet文件

loginservlet

package cn.itcast.web.servlet;

import cn.itcast.dao.Uesrdao;
import cn.itcast.domain.Userr; 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("/loginServlet")
public class loginservlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码
req.setCharacterEncoding("utf-8");
//获取请求参数
String username = req.getParameter("username");
String password = req.getParameter("password");
//3.封装user对象
Userr loginuser=new Userr();
loginuser.setUsername(username);
loginuser.setPassword(password);
//调用userdao的login()
Uesrdao dao=new Uesrdao();
Userr user=dao.login(loginuser);
//5判断user
if(user==null){
//登录失败
req.getRequestDispatcher("/failServlet").forward(req,resp);
}else
{
//登录成功
//存储数据
req.setAttribute("user",user);
//转发
req.getRequestDispatcher("/successServlet").forward(req,resp);
} } @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}

failservlet:

package cn.itcast.web.servlet;

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("/failServlet")
public class failServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//给页面写一句话
//设置页面编码
response.setContentType("text/html;charset=utf-8");
//输出
response.getWriter().write("登录失败,用户名或密码错误");
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}

successservlet

package cn.itcast.web.servlet;

import cn.itcast.domain.Userr;

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("/successServlet")
public class successServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取request域中
Userr user=(Userr)request.getAttribute("user");

//设置编码
response.setContentType("text/html;charset=utf-8");
//输出
response.getWriter().write("登录成功!"+user.getUsername()+"欢迎您");
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}

好了,我们开启tomcat服务器 ,输入URL 注意虚拟目录+servlet的路径

test request&&response 代码实现

大功告成!