赞
踩
本人的博客地址www.aogu181.top
本文章仅供参考,适合新手搭建JavaWeb,学习此文章来打打基础还是可以的,如果有错误或者写的不好的地方,请多多指教。
最后本项目只是提供一个框架和思路,对前端界面不做美化
目录
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是配置文件
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');
结果如下图所示,如需插入多个可自行选择,
完整项目放在最后,注意事项和解释全放在代码段里面了
代码如下:
- <%--
- 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" %>
- <html>
- <head>
- <title>登入界面</title>
- </head>
- <body>
- <%--如果 condition() 函数返回为true时才提交表单--%>
- <form action="checkLogin.jsp" method="post" onsubmit="return condition()">
- <%--这里的 name 和 id 可以随便取名字,但最好和数据库的字段保持一致--%>
- 学号:<input type="text" name="id" id="id"><br>
- 密码:<input type="password" name="password" id="password"><br>
- <input type="submit" value="登入">
- </form>
- <script>
- function condition() {
- var id_1 = document.getElementById("id").value;//获取id为id的值
- var pwd_2 = document.getElementById("password").value;//获取id为password的值;
- if(id_1==""){
- alert("学号不能为空!");
- return false;
- }
- if(pwd_2==""){
- alert("密码不能为空!");
- return false;
- }
- return true;
- }
-
- </script>
- </body>
- </html>
代码如下:
- <%--
- 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" %>
- <html>
- <head>
- <title>学生信息</title>
- </head>
- <body>
- <table border="2px" align="center" cellspacing="0">
- <tr>
- <td>学号</td>
- <td>姓名</td>
- <td>密码</td>
- <td width="200px"><a href="add.jsp">增加学生</a> <a href="searchStudent.jsp">查询学生</a></td>
- </tr>
- <%
- List<Student> list = StudentDao.getList();
- Iterator<Student> iter = list.iterator();
-
- while (iter.hasNext()) {
- Student student = iter.next();
- %>
- <tr>
- <td width="75px"><%=student.getId()%>
- </td>
- <td width="75px"><%=student.getName()%>
- </td>
- <td width="75px"><%=student.getPassword()%>
- </td>
- <td width="120px">
- <%--将 id 参数传过去 --%>
- <a href="delete.jsp?id=<%=student.getId() %>">删除</a>
- <%--
- 因为修改是要先 获取学号 得到全部信息
- 在修改所以传了一个参数 ?id=<%=student.getId()%>
- --%>
- <a href="updateStudent.jsp?id=<%=student.getId()%>">修改</a>
- </td>
- </tr>
- <tr>
-
- </tr>
- <%
- }
- %>
- </table>
- </body>
- </html>
- <%--
- 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" %>
- <html>
- <head>
- <title>添加学生</title>
- </head>
- <body>
- <form action="addCheck.jsp" method="post" onsubmit="return condition()">
- 学号:<input type="text" id="id" name="id"><br>
- 姓名:<input type="text" id="name" name="name"><br>
- 密码:<input type="password" id="password" name="password"><br>
- <input type="submit" value="提交">
- <input type="reset" value="重置">
- </form>
-
- <script>
- function condition() {
- var id_1 = document.getElementById("id").value;//获取id为id的值
- var name_2 = document.getElementById("name").value;//获取id为name的值
- var pwd_3 = document.getElementById("password").value;//获取id为password的值;
-
- if(id_1==""){
- alert("学号不能为空!");
- return false;
- }
- if(name_2==""){
- alert("姓名不能为空!");
- return false;
- }
- if(pwd_3==""){
- alert("密码不能为空!");
- return false;
- }
- return true;
- }
- </script>
- </body>
- </html>
代码如下:
- <%--
- 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" %>
- <html>
- <head>
- <title>修改学生信息</title>
- </head>
- <body>
- <%
- int id = Integer.parseInt(request.getParameter("id"));//获取学号
- Student stu = StudentDao.getStudent(id);//根据学号获取完整的对象
- %>
- <form action="updateCheck.jsp?id=<%=id%>" method="post">
- 学号: <input type="text" name="id" value="<%=stu.getId()%>"><br>
- 姓名: <input type="text" name="name" value="<%=stu.getName()%>"><br>
- 密码: <input type="text" name="password" value="<%=stu.getPassword()%>"><br>
- <input type="submit" value="修改">
- <input type="reset" value="重置">
- </form>
- </body>
- </html>
代码如下:
- <%--
- 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" %>
- <html>
- <head>
- <title>检查添加学生信息</title>
- </head>
- <body>
- <%
- 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");
- %>
- </body>
- </html>
代码如下:
- 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;
- }
- }
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<Student> getList() {
- Connection conn = null;
- PreparedStatement stmt = null;
- ResultSet rs = null;
- List<Student> 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<Student> getStudentList(int id) {
- Connection conn = null;
- PreparedStatement stmt = null;
- ResultSet rs = null;
- List<Student> 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,可根据自己需求添加
- 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<Student> 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中配置
- <filter>
- <filter-name>charsetEncodingFilter</filter-name>
- <filter-class>com.filter.EncodingFilter</filter-class>
- <init-param>
- <param-name>CharsetEncoding</param-name>
- <param-value>UTF-8</param-value>
- </init-param>
- </filter>
-
- <filter-mapping>
- <filter-name>charsetEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
(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中配置
- <filter>
- <filter-name>PrivateFilter</filter-name>
- <filter-class>com.filter.PrivateFilter</filter-class>
- </filter>
-
- <filter-mapping>
- <filter-name>PrivateFilter</filter-name>
- <!-- 需要过滤的路径-->
- <url-pattern>/private/*</url-pattern>
- </filter-mapping>
最后大致就大功完成了,感谢支持。喜欢了可以帮忙点个赞哦
最后附上我自己学习时做的班费管理系统班费管理系统
本篇文章的源码也附上链接:源码
提取码:hx38
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。