本人的博客地址www.aogu181.top
本文章仅供参考,适合新手搭建JavaWeb,学习此文章来打打基础还是可以的,如果有错误或者写的不好的地方,请多多指教。
最后本项目只是提供一个框架和思路,对前端界面不做美化
目录
开发工具与环境
工具包
功能说明
项目结构
*** 作步骤
1.创建数据库
(一)创建数据库
(二)创建数据表
(三)插入数据
2.创建JavaWeb界面
(一)登入界面
(二)主页界面
(三)增加学生界面
(四)修改界面
(五)查询界面
3.创建Java类实现功能
(一)创建对象类
(二)创建数据访问层(Dao层)
(三)创建servlet
(四)配置过滤器
源码
开发工具与环境
1.IntelliJ IDEA 2021.2.2
2.MySQL 8.0.20
3.jdk 1.8.0_144
4.Tomcat
工具包因为项目需要连接数据库,所以需要一个连接数据库的jar包
本项目用的是mysql8.0所以jar对应的就是8.0版本,如图所示,需要的自取jar包,提取码:miek
不同数据库版本对应的jar包是不一样的,具体jar包的下载地址:Jar包下载,怎么下载这里就不多介绍了
功能说明1.登入功能
2.增
3.删
4.改
5.查
项目结构项目结构如图所示:
编译器的不同具体也不完全相同,但大致一样就行
src下创建com.公司名. xxx 的形式
bean包下放需要 *** 作的对象
dao包下面放对需要 *** 作对象的 *** 作,例如增删改查
filter包下放过滤器,一般是放编码过滤器和权限过滤器
servlet包下放servlet对象
private包下放需要权限的页面
lib包下放需要导入的库(jar包)
web.xml是配置文件
*** 作步骤 1.创建数据库 (一)创建数据库create database rg56;
(二)创建数据表
这里设置了 id 为主键且不为空,其他设置根据自己的需求更改
create table stuno(
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name CHAR(30),
password CHAR(30),
);
最终如下图所示:
(三)插入数据INSERT INTO stuno(id, name,password)
VALUES (1,'小方','123456');
结果如下图所示,如需插入多个可自行选择,
2.创建JavaWeb界面完整项目放在最后,注意事项和解释全放在代码段里面了
(一)登入界面代码如下:
<%--
Created by IntelliJ IDEA.
User: HARD
Date: 2021/12/12
Time: 16:20
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
登入界面
<%--如果 condition() 函数返回为true时才提交表单--%>
(二)主页界面
代码如下:
<%--
Created by IntelliJ IDEA.
User: HARD
Date: 2021/12/12
Time: 16:18
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.bean.Student" %>
<%@ page import="com.dao.StudentDao" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Iterator" %>
学生信息
学号
姓名
密码
增加学生 查询学生
<%
List list = StudentDao.getList();
Iterator iter = list.iterator();
while (iter.hasNext()) {
Student student = iter.next();
%>
<%=student.getId()%>
<%=student.getName()%>
<%=student.getPassword()%>
<%--将 id 参数传过去 --%>
">删除
<%--
因为修改是要先 获取学号 得到全部信息
在修改所以传了一个参数 ?id=<%=student.getId()%>
--%>
">修改
<%
}
%>
(三)增加学生界面
<%--
Created by IntelliJ IDEA.
User: HARD
Date: 2021/12/12
Time: 20:35
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
添加学生
(四)修改界面
代码如下:
<%--
Created by IntelliJ IDEA.
User: HARD
Date: 2021/12/12
Time: 21:05
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.dao.StudentDao" %>
<%@ page import="com.bean.Student" %>
修改学生信息
<%
int id = Integer.parseInt(request.getParameter("id"));//获取学号
Student stu = StudentDao.getStudent(id);//根据学号获取完整的对象
%>
(五)查询界面
代码如下:
<%--
Created by IntelliJ IDEA.
User: HARD
Date: 2021/12/12
Time: 20:42
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.bean.Student" %>
<%@ page import="com.dao.StudentDao" %>
检查添加学生信息
<%
request.setCharacterEncoding("UTF-8");
//获取来自 add.jsp 的表单
int id = Integer.parseInt(request.getParameter("id")) ;
String name = request.getParameter("name");
String password = request.getParameter("password");
//创建 student 对象
Student student = new Student();
student.setId(id);
student.setName(name);
student.setPassword(password);
StudentDao.add(student);
//添加完成就返回查看页面
response.sendRedirect("index.jsp");
%>
3.创建Java类实现功能
(一)创建对象类
代码如下:
package com.bean;
public class Student {
private int id;
private String password;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
(二)创建数据访问层(Dao层)
1.创建连接数据库 *** 作对象类,BaseDao.java
package com.dao;
import java.sql.*;
/*
* 连接数据库
*
* */
public class BaseDao {
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
Connection conn = null;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/rg56?useUnicode=yes&characterEncoding=utf8", "root", "131488");
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void closeAll(ResultSet rs,PreparedStatement pStmt,Connection conn){
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pStmt != null){
try {
pStmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
(二)创建 *** 作对象类
package com.dao;
import com.bean.Student;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/*
* 用来对学生进行 *** 作
* */
public class StudentDao {
//获取学生信息列表
public static List getList() {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
List list = new ArrayList<>();
try {
conn = BaseDao.getConnection();
stmt = conn.prepareStatement("SELECT * FROM stuno");
rs = stmt.executeQuery();
while (rs.next()) {
Student stu = new Student();
stu.setId(rs.getInt(1));
stu.setName(rs.getString(2));
stu.setPassword(rs.getString(3));
list.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
BaseDao.closeAll(rs, stmt, conn);
}
return list;
}
//增加学生信息
public static void add(Student stu) {
Connection con = null;
PreparedStatement pStmt = null;
ResultSet rs = null;
try {
con = BaseDao.getConnection();
pStmt = con.prepareStatement("insert into stuno(id,name,password) values(?,?,?)");
pStmt.setInt(1, stu.getId());
pStmt.setString(2, stu.getName());
pStmt.setString(3, stu.getPassword());
pStmt.executeUpdate();//更新数据
} catch (SQLException e) {
e.printStackTrace();
}
}
//根据学号删除学生信息
public static void delete(int id) {
Connection con = null;
PreparedStatement pStmt = null;
try {
con = BaseDao.getConnection();
pStmt = con.prepareStatement("delete from stuno where id=?");
pStmt.setInt(1, id);
pStmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
//获取单个学生对象
public static Student getStudent(int id) {
Student s = new Student();
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = BaseDao.getConnection();
stmt = conn.prepareStatement("select * from stuno where id=?");
stmt.setInt(1, id);
rs = stmt.executeQuery();
if (rs.next()) {
s.setId(rs.getInt("id"));
s.setName(rs.getString("name"));
s.setPassword(rs.getString("password"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
BaseDao.closeAll(rs, stmt, conn);
}
return s;
}
//修改功能
public static void updateStudent(Student student) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
conn = BaseDao.getConnection();
String sql = "UPDATE stuno SET id=?,name=?,password=? where id=?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, student.getId());
stmt.setString(2, student.getName());
stmt.setString(3, student.getPassword());
stmt.setInt(4, student.getId());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
BaseDao.closeAll(rs, stmt, conn);
}
}
//查询功能,根据模糊查询学号返回所有学生信息
public static List getStudentList(int id) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
List allStudent = new ArrayList<>();
try {
conn = BaseDao.getConnection();
stmt = conn.prepareStatement("select * from stuno where id like ?");
stmt.setString(1, "%" + id + "%");
rs = stmt.executeQuery();
while (rs.next()) {
Student stu = new Student();
stu.setId(rs.getInt(1));
stu.setName(rs.getString(2));
stu.setPassword(rs.getString(3));
allStudent.add(stu);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
BaseDao.closeAll(rs, stmt, conn);
}
return allStudent;
}
}
(三)创建servlet
我只把查找功能交给了servlet,可根据自己需求添加
package com.sevlet;
import com.dao.StudentDao;
import com.bean.Student;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
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;
import java.util.List;
/**
* /search 是注解也可以在 web.xml中配置servlet
*
*/
@WebServlet("/search")
public class SearchSevlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
int id = Integer.parseInt(req.getParameter("id"));
List list = StudentDao.getStudentList(id);
req.setAttribute("list", list);
req.getRequestDispatcher("searchStudent.jsp").forward(req, resp);
super.doPost(req, resp);
}
}
(四)配置过滤器
1.配置字符码过滤器
package com.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class EncodingFilter implements Filter {
private static String encoding; // 定义变量接收初始化的值
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// 设置字符编码链锁
request.setCharacterEncoding(encoding);
response.setCharacterEncoding(encoding);
chain.doFilter(request, response);
}
// 初始化
public void init(FilterConfig config) throws ServletException {
// 接收web.xml配置文件中的初始参数
encoding = config.getInitParameter("CharsetEncoding");
}
}
创建完了还需要在web.xml中配置
charsetEncodingFilter
com.filter.EncodingFilter
CharsetEncoding
UTF-8
charsetEncodingFilter
/*
(2)创建权限过滤器
package com.filter;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class PrivateFilter implements Filter {
private FilterConfig filterConfig;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpSession session = request.getSession();
//这里的 name 是登入成功后在登入成功界面加一个 session.setAttribute("name","xxx");
String name = (String) session.getAttribute("name");
if (name == null) {
if (request.getRequestURI().indexOf("../firstLogin.jsp") > -1) {
filterChain.doFilter(servletRequest, servletResponse);
} else {
response.sendRedirect("../firstLogin.jsp");
}
} else {
request.getRequestDispatcher("index.jsp").forward(request,response);
return;
}
}
@Override
public void destroy() {
}
}
同样需要在web.xml中配置
PrivateFilter
com.filter.PrivateFilter
PrivateFilter
/private/*
源码
最后大致就大功完成了,感谢支持。喜欢了可以帮忙点个赞哦
最后附上我自己学习时做的班费管理系统班费管理系统
本篇文章的源码也附上链接:源码
提取码:hx38
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)