JavaWeb入门案例-集合Servlet、tomcat、数据库连接池技术(idea版)
昨天晚上睡觉前暗自告诉自己明天一定要去写这篇感人的javaweb入门笔记(一个小型的用户登录案例的入门笔记)。
enmmmm Flag不能倒!!
首先我们来了解一下在idea中创建一个JavaApplication,各文件夹代表的都是什么意思。详见如图。
1.项目部署
通俗理解:一个项目是需要部署到服务器上,用户才能通过url访问服务器,得到服务器返回的一系列消息。用户发送请求给tomcat服务器,tomcat服务器自动从webapps目录下找所创建的项目下访问的html
常见系统架构:C/S B/S
常见服务器:Tomcat、jetty(淘宝,运行效率比Tomcat高)、resin(新浪,所有开源服务器软件中,运行效率最高的),由于Tomcat免费,所以我们学生党学习就用这个足够。
-
手动部署
在本地装上tomcat,并在系统环境变量配置好classpath后,打开tomcat目录,将项目部署在webapps下。
webapps->创建的项目->web_INF(自己创建的类)和index.html
webapps目录总结见下图。 -
idea配置tomcat实现自动部署
- idea集成tomcat 按照图示1234操作
- 项目与tomcat关联
第5步是系统自动生成的项目访问名称,自己可根据项目实际情况更改名称。但这里后缀_exploded表示开发版,表示开发者不断升级自己的程序不断添加新功能,尚未将项目集成的war包放在tomcat的webapps下,官方名词将这一过程称为热部署。用户可以通过http://localhost:8080/02Servlet_war_exploded/XX.html访问自己创建的项目。
2.项目构成
Dao:主要实现数据库的增删改查方法;
entity:实体类,创建getter、setter方法,toString方法;
service:控制逻辑层,实现整个逻辑,开启事务,将dao层数据库中的数据与servlet浏览器得到的数据进行比较;
servlet:主要是实现HttpServlet类中的doGet、doPost方法,接受浏览器传过来的数据;
utils:DBUtils类 主要是创建数据连接池,数据库事务操作的接口和方法定义;
-
utils包
utils下的DBUtils.java
package com.qf.servletProject.utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DBUtils {
public static DataSource ds;
private static final ThreadLocal<Connection> THREAD_LOCAL = new ThreadLocal<>();
static{
Properties pro =new Properties();
InputStream inputStream=DBUtils.class.getResourceAsStream("/database.properties");
try {
pro.load(inputStream);
//通过DruiDtaSourceFactory创建连接池
ds= DruidDataSourceFactory.createDataSource(pro);
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}
//开启事务
public static void begin(){
Connection connection=null;
try {
connection=getConnection();
connection.setAutoCommit(false);
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
closeAll(connection,null,null);
}
}
//提交事务
public static void commit(){
Connection connection=null;
try {
connection=getConnection();
connection.commit();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
closeAll(connection,null,null);
}
}
public static void rollback(){
Connection connection=null;
try {
connection=getConnection();
connection.rollback();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
closeAll(connection,null,null);
}
}
public static void closeAll(Connection connection, Statement statement, ResultSet resultSet) {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
THREAD_LOCAL.remove();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
-
servlet包
servlet下的LoginServlet.java
package com.qf.servletProject.servlet;
import com.qf.servletProject.entity.Admin;
import com.qf.servletProject.service.AdminService;
import com.qf.servletProject.service.impl.AdminServiceImpl;
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.io.PrintWriter;
@WebServlet(value = "/login")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//使接受的数据和发送的数据都是以utf-8的格式
req.setCharacterEncoding("UTF-8");
resp.setContentType("utf-8");
//1.收参
String username=req.getParameter("username");
String password=req.getParameter("password");
//2.调用业务逻辑
AdminService adminService=new AdminServiceImpl();
Admin admin=adminService.login(username,password);
//3.处理结果
PrintWriter printWriter=resp.getWriter();
if(admin!=null){
//响应给客户端一个结果页面,页面显示登录成功
printWriter.println("响应显示在浏览器的页面");
}else{
printWriter.println("登录账户或密码错误!");
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
-
service包
service下的AdminService.java
package com.qf.servletProject.service;
import com.qf.servletProject.entity.Admin;
import java.util.List;
public interface AdminService {
public Admin login(String username, String password);
public List<Admin> showAllAdmin();
}
service下的impl包下的AdminServiceImpl.java
package com.qf.servletProject.service.impl;
import com.qf.servletProject.Dao.AdminDao;
import com.qf.servletProject.Dao.impl.AdminDaoImpl;
import com.qf.servletProject.entity.Admin;
import com.qf.servletProject.service.AdminService;
import com.qf.servletProject.utils.DBUtils;
import java.util.List;
public class AdminServiceImpl implements AdminService {
private AdminDao adminDao=new AdminDaoImpl();
@Override
public Admin login(String username, String password) {
Admin result=null;
try {
DBUtils.begin();//开启事务
Admin admin=adminDao.select(username);
if(admin!=null){
if(admin.getPassword().equals(password)){
result=admin;
}
}
DBUtils.commit();
}catch (Exception e){
DBUtils.rollback();
e.printStackTrace();
}
return result;
}
@Override
public List<Admin> showAllAdmin() {
return null;
}
}
-
entity包
entity下的Admin
package com.qf.servletProject.entity;
public class Admin {
private String username;
private String password;
private String address;
private String phone;
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;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Admin{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", address='" + address + '\'' +
", phone='" + phone + '\'' +
'}';
}
}
-
Dao包
Dao下的AdminDao.java
package com.qf.servletProject.Dao;
import com.qf.servletProject.entity.Admin;
import java.util.List;
public interface AdminDao {
public int insert(Admin admin);
public int delete(String username);
public int update(Admin admin);
public Admin select(String username);
public List<Admin> selectAll();
}
Dao下的impl下的AdminDaoImpl.java
package com.qf.servletProject.Dao.impl;
import com.qf.servletProject.Dao.AdminDao;
import com.qf.servletProject.entity.Admin;
import com.qf.servletProject.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class AdminDaoImpl implements AdminDao {
private QueryRunner queryRunner=new QueryRunner();
@Override
public int insert(Admin admin) {
return 0;
}
@Override
public int delete(String username) {
return 0;
}
@Override
public int update(Admin admin) {
return 0;
}
@Override
public Admin select(String username) {
try {
Admin admin=queryRunner.query(DBUtils.getConnection(),"select * from admin where username=? ",new BeanHandler<Admin>(Admin.class),username);
return admin;
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return null;
}
@Override
public List<Admin> selectAll() {
try {
List<Admin> admins=queryRunner.query(DBUtils.getConnection(),"select * from admin",new BeanListHandler<Admin>(Admin.class));
return admins;
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return null;
}
}
database.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql//localhost:3306//myemployee
username=root
password=123456
initialSize=10
maxActive=20
minIdle=5
maxWait=3000
本文地址:https://blog.csdn.net/kexiaoleqq/article/details/109802846
上一篇: sso单点登录的原理详解
下一篇: 深入浅出RxJava (二:操作符)