编程知识 cdmana.com

Java Web Course Design - student information management system (JSP + servlet + MySQL)

JavaWeb curriculum design , adopt Jsp+Servlet+MySql Design implementation , Function is relatively simple , Users can log in and register to enter the management interface to manage student information , Class information , Course information, etc . Page query is realized , Add information 、 Modify the information 、 Delete the information 、 Select Delete and other functions , Here is the running interface :

 Insert picture description here

Related technology :

Servlet+JSP+Bootstrap+Jquery+MYSQL

Project directory :

Back end code part :
 Insert picture description here
Front page section :
 Insert picture description here
relevant jar package :
 Insert picture description here




Database table structure :

create database StudentInfo;
use StudentInfo;
drop table if exists student;
drop table if exists user;
drop table if exists class;
drop table if exists course;

# User table 
create table user(
	id int auto_increment primary key,
	username nvarchar(20) null,
    password nvarchar(20) null,
    name nvarchar(20) null,
	gender nchar(10) null,
	age int null,
	classno nvarchar(20) null,
	phone nvarchar(11) unique null,
	email nvarchar(30) null
);

# Student list 
create table student(
	id int auto_increment primary key,
	name nvarchar(20) null,
	gender nvarchar(10) null,
	age int null,
	classno nvarchar(20) null,
	phone nvarchar(11) unique null,
	email nvarchar(30) null
);

# Class table 
create table class(
	id int auto_increment primary key,
	cno nvarchar(10) null,
	classname nvarchar(30) null,
	department nvarchar(30) null
);

# The curriculum 
create table course(
	id int auto_increment primary key,
	courseno nvarchar(20) null,
    coursename nvarchar(20) null,
    type nvarchar(8) null,
	period int null,
    credit double null
);

User information sheet :User
 Insert picture description here
Student information sheet :Student
 Insert picture description here
Class information table :Class
 Insert picture description here
Course information sheet :Course
 Insert picture description here






Function display :

The user login :
 Insert picture description here
User registration :
 Insert picture description here
Login successful :
 Insert picture description here
Click the user name in the upper left corner to complete your personal information :
 Insert picture description here






The information management part realizes pagination display , increase 、 Delete 、 Change 、 check 、 Batch deletion

Student information management :
 Insert picture description here
Class information management :
 Insert picture description here
Course management :
 Insert picture description here




Design steps

More code , Just show the more important parts :

1. Database connection implementation

The database connection part uses druid Database connection pool , First, configure the database connection pool :druid.properties

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mydb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
username=root
password=123456
initialSize=5
maxActive=10
maxWait=3000

Then write the database connection tool class :

/**
 * JDBC Tool class   Use Durid Connection pool 
 */
public class JDBCUtils {
   
   

    private static DataSource ds ;
    static {
   
   
        try {
   
   
            //1. Load profile 
            Properties pro = new Properties();
            // Use ClassLoader Load profile , Get byte input stream 
            InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
            pro.load(is);

            //2. Initialize connection pool object 
            ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
   
   
            e.printStackTrace();
        } catch (Exception e) {
   
   
            e.printStackTrace();
        }
    }

    /**
     *  Get connection pool object 
     */
    public static DataSource getDataSource(){
   
   
        return ds;
    }

    /**
     *  Get the connection Connection object 
     */
    public static Connection getConnection() throws SQLException {
   
   
        return  ds.getConnection();
    }
}

Here we use JdbcTemplate To connect data to operate the database :

private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

2. Implement persistence layer (Dao)

Write the persistence interface :

/**
 *  User operated DAO
 */
public interface UserDao {
   
   

}

Implement persistence layer interface :

public class UserDaoImpl implements UserDao {
   
   

    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

}

3. Implement the business layer (Service)

Write business layer interface :

/**
 *  User management business interface 
 */
public interface UserService {
   
   

}

Implement business layer interface :

public class UserServiceImpl implements UserService {
   
   
    private UserDao dao = new UserDaoImpl();

}

4. Realize the function of presentation layer

Write the presentation layer :

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
   
   
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
   
       /*
        * Code
        */
    }

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

5. Because of the presentation layer Servlet Too much , We can do simple extraction

To write BaseServlet class , And then by the other servlet Inherit

public class BaseServlet extends HttpServlet {
   
   
	
	private static final long serialVersionUID = 1L;

	public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
   

	   try {
   
   
		   //  Get the request ID 
           String methodName = request.getParameter("method");
           //  Gets the bytecode object of the specified class 
           Class<? extends BaseServlet> clazz = this.getClass();// there this It means inheritance BaseServlet object 
           //  Get the bytecode object of the method through the bytecode object of the class 
           Method method = clazz.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
           //  Let the method execute 
           method.invoke(this, request, response);

       } catch (Exception e) {
   
   
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
    }
}

5. Write the corresponding front page : With user_login.jsp For example

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html> 
<html lang="zh-CN">
  <head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <title> Administrator login </title>

    <!-- 1.  Import CSS The overall style of  -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <!-- 2. jQuery Import , It is recommended to use 1.9 Version above  -->
    <script src="js/jquery-2.1.0.min.js"></script>
    <!-- 3.  Import bootstrap Of js file  -->
    <script src="js/bootstrap.min.js"></script>
    <script type="text/javascript">
    
        // Switch verification code 
        function refreshCode(){
    
    
        	//1. Get captcha image object 
        	var vcode=document.getElementById("vcode");
        	//2. Set up src attribute , Time stamp 
        	//2. Set up src attribute , Time stamp 
            vcode.src = "${pageContext.request.contextPath}/checkCodeServlet?time="+new Date().getTime();
    	
        }
    </script>
  </head>
  <body>
  	<div class="container" style="width: 400px;">
  		<h3 style="text-align: center;"> Administrator login </h3>
        <form action="${pageContext.request.contextPath}/loginServlet" method="post">
	      <div class="form-group">
	        <label for="user"> user name :</label>
	        <input type="text" name="username" class="form-control" id="user" placeholder=" Please enter a user name "/>
	      </div>
	      
	      <div class="form-group">
	        <label for="password"> password :</label>
	        <input type="password" name="password" class="form-control" id="password" placeholder=" Please input a password "/>
	      </div>
	      
	      <div class="form-inline">
	        <label for="vcode"> Verification Code :</label>
	        <input type="text" name="verifycode" class="form-control" id="verifycode" placeholder=" Please enter the verification code " style="width: 120px;"/>
	        <a href="javascript:refreshCode();">
	        <img src="${pageContext.request.contextPath}/checkCodeServlet" title=" Can't see clearly, click refresh " id="vcode"/>
	        </a>
	      </div>
	      <hr/>
	      <div class="form-group" style="text-align: center;">
	        <input class="btn btn btn-primary" type="submit" value=" Sign in ">
	       </div>
	  	</form>
		
		<!--  Error display information box  -->
	  	<div class="alert alert-warning alert-dismissible" role="alert">
		    <button type="button" class="close" data-dismiss="alert" >
		  	<span>&times;</span></button>
		    <strong>${login_msg}</strong>
		</div>
  	</div>
  </body>
</html>

Run a screenshot :
 Insert picture description here
Test the login function , Find the problem of Chinese garbled code ( Direct inheritance HttpServlet There will be no , Inherit BaseServlet There will be )

6. Write a filter to solve the problem of Chinese garbled

@WebFilter("/*")
public class CharchaterFilter implements Filter {
   
   

	protected String encoding; 
	
	@Override
	public void destroy() {
   
   
		// TODO  Automatically generated method stubs 
		
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
   
   
		// TODO  Automatically generated method stubs 
		HttpServletRequest request=(HttpServletRequest)req;
		HttpServletResponse response=(HttpServletResponse)res;
		
		String method=request.getMethod();
		
		if(method.equalsIgnoreCase("post")){
   
   
			request.setCharacterEncoding("utf-8");
		}
		
		response.setContentType("text/html;charset=utf-8");
		
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
   
   
		// TODO  Automatically generated method stubs 
		
	}

}

7. Write a list page , And implement the function of response on the back-end code

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>
<head>

	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
    <title> Website background management </title>
 
    <!-- Bootstrap -->
    <link rel="stylesheet" href="css/bootstrap.min.css">
	<!-- jQuery (Bootstrap  All of the  JavaScript  Plug ins all depend on  jQuery, So it has to be in the front ) -->
	<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
	<!--  load  Bootstrap  All of the  JavaScript  plug-in unit . You can also load only a single plug-in as needed . -->
	<script type="text/javascript" src="js/bootstrap.min.js"></script>
    <style type="text/css">
        @media (min-width: 768px) {
    
    
           #slider_sub{
    
    
               width: 200px;
               margin-top: 51px;
               position: absolute;
               z-index: 1;
               height: 600px;
           }
            .mysearch{
    
    
                margin: 10px 0;
            }
            .page_main{
    
    
                margin-left: 205px;
            }
        }
    </style>
    <script>
        function deleteStudent(id){
    
    
            // User security tips 
            if(confirm(" Are you sure you want to delete ?")){
    
    
                // Access path 
                location.href="${pageContext.request.contextPath}/student?method=delStudent&id="+id;
            }
        }

        window.onload = function(){
    
    
            // Add click event to delete selected button 
            document.getElementById("delSelectedStudent").onclick = function(){
    
    
                if(confirm(" Are you sure you want to delete the selected entry ?")){
    
    

                   var flag = false;
                    // Determine whether there are selected items 
                    var cbs = document.getElementsByName("id");
                    for (var i = 0; i < cbs.length; i++) {
    
    
                        if(cbs[i].checked){
    
    
                            // There is an entry selected 
                            flag = true;
                            break;
                        }
                    }

                    if(flag){
    
    // There are items selected 
                        // Form submission 
                        document.getElementById("form").submit();
                    }

                }

            }
            //1. Get the first one cb
            document.getElementById("firstCb").onclick = function(){
    
    
                //2. Get all the... In the list below cb
                var cbs = document.getElementsByName("uid");
                //3. Traverse 
                for (var i = 0; i < cbs.length; i++) {
    
    
                    //4. Set these cbs[i] Of checked state  = firstCb.checked
                    cbs[i].checked = this.checked;

                }

            }

        }

    </script>
</head>
<body>
    <nav class="navbar navbar-default navbar-static-top">
        <div class="navbar-header">
            <!-- The zoom  -->
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#slider_sub">
                <span class="sr-only"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a href="#" class="navbar-brand"> Student information management </a>
        </div>
        <ul class="nav navbar-nav navbar-right" style="margin-right: 25px">
            <li><a href="${pageContext.request.contextPath}/user?method=findUser&id=${user.id}"><span class="badge" style="background: #C12E2A">${user.username}</span></a></li>
            <li><a href="${pageContext.request.contextPath}/user?method=destroyUser"><span class="glyphicon glyphicon-off"></span>&nbsp; Cancellation </a></li>
        
        </ul>
 
        <!--  Sidebar   -->
        <div class="navbar-default navbar-collapse" id="slider_sub">
            <ul class="nav">
                <li>

			        <form class="form-inline" action="${pageContext.request.contextPath}/student?method=findAll" method="post">
			            
			            <!-- Search for  -->
	                    <div class="input-group mysearch">
	                        <input type="text" name="name" value="${condition.name[0]}" class="form-control" id="exampleInputName2" >
	                        <span class="input-group-btn">
	                            <button type="submit" class="btn btn-default">
	                                <span class="glyphicon glyphicon-search"></span>
	                            </button>
	                        </span>
	 
	                    </div>
			        </form>

                    
                </li>
                <li><a href="${pageContext.request.contextPath}/index.jsp" class="collapse" data-toggle="collapse"> home page <span class="glyphicon glyphicon-chevron-right pull-right"></span></a>
                     <!-- <ul id="sub2" class="nav collapse">
                        <li><a href="#"><span class="glyphicon glyphicon-info-sign"></span>&nbsp; information management </a></li>
                        <li><a href="#"><span class="glyphicon glyphicon-user"></span>&nbsp; Student management </a></li>
                    </ul> -->
                </li>
                <li><a href="${pageContext.request.contextPath}/student?method=findAll"  class="collapse" data-toggle="collapse"> Student management <span class="glyphicon glyphicon-chevron-right pull-right"></span></a>
                    
                </li>
                <li><a href="${pageContext.request.contextPath}/classno?method=findAll"  class="collapse" data-toggle="collapse"> Class management <span class="glyphicon glyphicon-chevron-right pull-right"></span></a>
                    
                </li>
                <li><a href="${pageContext.request.contextPath}/course?method=findAll"  class="collapse" data-toggle="collapse"> Course management <span class="glyphicon glyphicon-chevron-right pull-right"></span></a>
                    
                </li>
                <li><a href="${pageContext.request.contextPath}/about.jsp"  class="collapse" data-toggle="collapse"> About us <span class="glyphicon glyphicon-chevron-right pull-right"></span></a>
                    
                </li>
            </ul>
        </div>
    </nav>
 
    <!--  The primary area  -->
    <div class="page_main" style="background: rgba(92,88,116,0.34)">
        <ol class="breadcrumb">
            <li><a href="#"> Management home page </a></li>
            <li><a href="#"> Student management </a></li>
            <li><a href="#"> Student information </a></li>
        </ol>
 
        <div class="row">
            <div class="col-md-12 col-sm-3">
                <div class="panel panel-default">
                    <!-- <div class="panel-header"> Student information </div> -->
                    
                    <div class="panel-body table-responsive">
                    
				
				    <div style="float: right;margin: 5px;">
				
				        <a class="btn btn-primary" href="${pageContext.request.contextPath}/student_add.jsp"> Add student </a>
				        <a class="btn btn-primary" href="javascript:void(0);" id="delSelectedStudent"> Delete selected </a>
				
				    </div>
                    
                    <form id="form" action="${pageContext.request.contextPath}/student?method=delSelectedStudent" method="post">
                        <table class="table table-striped table-hover" style="margin: 10px;">
                            <thead>
	                            <tr>
	                            	<th><input type="checkbox" id="firstCb"></th>
	                            	<th> Number </th>
	                                <th> full name </th>
	                                <th> Gender </th>
	                                <th> Age </th>
	                                <th> class </th>
	                                <th> Telephone </th>
	                                <th> mailbox </th> 
	                     
	                            </tr>
                            </thead>
                            
                            <c:forEach items="${pb.list}" var="student" varStatus="s">
					            <tr>
					                <td><input type="checkbox" name="id" value="${student.id}"></td>
					                <td>${student.id}</td> 
					                <td>${student.name}</td>
					                <td>${student.gender}</td>
					                <td>${student.age}</td>
					                <td>${student.classno}</td>
					                <td>${student.phone}</td>
					                <td>${student.email}</td>
					                <td><a class="btn btn-default btn-sm" href="${pageContext.request.contextPath}/student?method=findStudent&id=${student.id}"> modify </a>&nbsp;
					                    <a class="btn btn-default btn-sm" href="javascript:deleteStudent(${student.id});"> Delete </a></td>
					            </tr>

					        </c:forEach>
					        
                          
                        </table>
                        </form>
     
                        
                        
                        <nav aria-label="Page navigation" class="pull-right">
                            <ul class="pagination" style="margin-top: 10px;">
   
                                <c:if test="${pb.currentPage == 1}">
				                    <li class="disabled">
				                </c:if>
				
				                <c:if test="${pb.currentPage != 1}">
				                    <li>
				                </c:if>
				
				                    <a href="${pageContext.request.contextPath}/student?method=findAll&currentPage=${pb.currentPage - 1}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Previous">
				                        <span aria-hidden="true">&laquo;</span>
				                    </a>
				                    </li>
				
				                <c:forEach begin="1" end="${pb.totalPage}" var="i" >
				
				                    <c:if test="${pb.currentPage == i}">
				                        <li class="active"><a href="${pageContext.request.contextPath}/student?method=findAll&currentPage=${i}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}">${i}</a></li>
				                    </c:if>
				                    <c:if test="${pb.currentPage != i}">
				                        <li><a href="${pageContext.request.contextPath}/student?method=findAll&currentPage=${i}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}">${i}</a></li>
				                    </c:if>
				
				                </c:forEach>
				
				                <li>
				                    <a href="${pageContext.request.contextPath}/student?method=findAll&currentPage=${pb.currentPage + 1}&rows=5&name=${condition.name[0]}&address=${condition.address[0]}&email=${condition.email[0]}" aria-label="Next">
				                        <span aria-hidden="true">&raquo;</span>
				                    </a>
				                </li>
				                <span style="font-size: 25px;margin-left: 5px;">
				                    	 common ${pb.totalCount} Bar record , common ${pb.totalPage} page 
				                </span>
                            </ul>
                        </nav>
                    </div>
                </div>
            </div>
 
        </div>
    </div>

</body>
</html>

 Insert picture description here

8. Realize the function of deleting and modifying different information , And further improve

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!--  The language of the web page  -->
<html lang="zh-CN">
    <head>
        <!--  Specify character set  -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title> Modify student information </title>

        <link href="css/bootstrap.min.css" rel="stylesheet">
        <script src="js/jquery-2.1.0.min.js"></script>
        <script src="js/bootstrap.min.js"></script>
        
    </head>
    <body>
        <div class="container" style="width: 400px;">
        <h3 style="text-align: center;"> Modify student information </h3>
        <form action="${pageContext.request.contextPath}/student?method=updateStudent" method="post">
            <!--   Hidden fields   Submit id-->
            <input type="hidden" name="id" value="${student.id}">

          <div class="form-group">
            <label for="name"> full name :</label>
            <input type="text" class="form-control" id="name" name="name"  value="${student.name}" readonly="readonly" placeholder=" Please enter a name " />
          </div>

          <div class="form-group">
            <label> Gender :</label>
              <c:if test="${student.gender == ' male '}">
                  <input type="radio" name="gender" value=" male " checked /> male 
                  <input type="radio" name="gender" value=" Woman "  /> Woman 
              </c:if>

              <c:if test="${student.gender == ' Woman '}">
                  <input type="radio" name="gender" value=" male "  /> male 
                  <input type="radio" name="gender" value=" Woman " checked  /> Woman 
              </c:if>


          </div>

          <div class="form-group">
            <label for="age"> Age :</label>
            <input type="text" class="form-control" value="${student.age}" id="age"  name="age" placeholder=" Please enter age " />
          </div>

          <div class="form-group">
            <label for="classno"> class :</label>
            
            <input type="text" id="classno" class="form-control" value="${student.classno}" name="classno" placeholder=" Please input class information "/>
          
          </div>

          <div class="form-group">
            <label for="phone"> Telephone </label>
            <input type="text" id="phone" class="form-control" value="${student.phone}" name="phone" placeholder=" Please enter the phone number "/>
          </div>

          <div class="form-group">
            <label for="email">Email:</label>
            <input type="text" id="email" class="form-control" value="${student.email}" name="email" placeholder=" Please enter email address "/>
          </div>

             <div class="form-group" style="text-align: center">
                <input class="btn btn-primary" type="submit" value=" Submit " />
                <input class="btn btn-default" type="reset" value=" Reset " />
             </div>
        </form>
        </div>
    </body>
</html>

 Insert picture description here

版权声明
本文为[osc_ dg0eswo7]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225094529788c.html

Scroll to Top