• 学生管理servlet


    下载链接

    目录结构如下:

    edu.lishan.bean>>
    
            Student.java
    edu.lishan.servlet>>
    
            LoginServlet.java
    edu.lishan.util>>
    
            DbConnect.java
    WebRoot>>
    
            index.jsp
            loginAction.jsp
            stuAdd.jsp

    Student.java

    package edu.lishan.bean;
    
    public class Student {
        private int id;
        private String name;
        private String sex;
        private String specialty;
        private String grade;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }
        public String getSpecialty() {
            return specialty;
        }
        public void setSpecialty(String specialty) {
            this.specialty = specialty;
        }
        public String getGrade() {
            return grade;
        }
        public void setGrade(String grade) {
            this.grade = grade;
        }
        
    
    }

    LoginServlet.java

    package edu.lishan.servlet;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    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 javax.servlet.http.HttpSession;
    
    import edu.lishan.util.DbConnect;
    
    @WebServlet(urlPatterns="/loginServer")
    public class LoginServlet extends HttpServlet{
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            // TODO Auto-generated method stub
            this.doPost(req, resp);
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException{
            resp.setContentType("text/html;charset=utf-8");
            req.setCharacterEncoding("utf-8");
            PrintWriter out = resp.getWriter();
            String admin = req.getParameter("admin");
            String pwd = req.getParameter("pwd");
            if(admin.equals("admin")&& pwd.equals("123")){
                Connection conn = DbConnect.getConnection();
                String sql = "select * from student";
                PreparedStatement psmt = null;
                ResultSet rs = null;
                try {
                    psmt = conn.prepareStatement(sql);
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                     rs = psmt.executeQuery();
                     HttpSession session = req.getSession();
                     session.setMaxInactiveInterval(7200);
                     session.setAttribute("rs", rs);
                     
                     resp.sendRedirect("loginAction.jsp");
                     
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
                
            }
            
            
            
        }
    
    }

    DbConnect.java

    package edu.lishan.util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    public class DbConnect {
        public static Connection conn;
        public static Connection getConnection(){
            
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //创建连接对象
            try {
                conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test"
                                                   ,"root", "root");
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return conn;
        }
    
    }

    index.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
       <title>学生管理系统</title>
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
      </head>
      
      <body>
            <center>
                管理员登录界面
                <form action="loginServer" method="post">
                    <table>
                        <Tr>
                            <td>用户名:</td>
                            <td><input type="text" name="admin"></td>
                        </Tr>
                        <Tr>
                            <td>密码:</td>
                            <td><input type="text" name="pwd"></td>
                        </Tr>
                        <tr>
                            <td colspan="2" align="center"><input type="submit" value="登录"></td>
                        </tr>
                    </table>
                </form>
            </center>
      </body>
    </html>

    loginAction.jsp

    <%@page import="java.sql.ResultSet"%>
    <%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
    <%
        ResultSet rs = (ResultSet) session.getAttribute("rs");
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>学生管理系统</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <link rel="stylesheet" type="text/css" href="table.css"/>
    </head>
    <body>
        <center>
            <table class="gridtable">
                <tr>
                    <td align="center" colspan="6">学生信息</td>
                </tr>
                <tr>
                    <td>姓名</td>
                    <td>性别</td>
                    <td>专业</td>
                    <td>年级</td>
                    <td align="center" colspan="2">操作</td>
                </tr>
                <%
                    while (rs.next()) {
                %>
                <tr>
                    <td><%=rs.getString("name")%></td>
                    <td><%=rs.getString("sex")%></td>
                    <td><%=rs.getString("specialty")%></td>
                    <td><%=rs.getString("grade")%></td>
                    <td>
                        <a href="selectServlet?id=<%=rs.getInt("id")%>">修改 </a>  
                    </td>
                    <td>
                        <a href="deleteServlet?id=<%=rs.getInt("id")%>" onClick="return confirm('确定要删除吗?');">删除 </a>
                    </td>
                </tr>
                <%
                    }
                %>
                <tr>
                    <td align="center" colspan="6">
                        <a href="stuAdd.jsp">添加 </a>
                    </td>
                </tr>
            </table>
        </center>
    </body>
    </html>

    stuAdd.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>添加学生信息</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        
        <link rel="stylesheet" type="text/css" href="table.css"/>
      </head>
      <body>
      <form action="addServlet" method="post">
      <center>
            <table class="gridtable">
                <tr>
                    <td align="center" colspan="2">添加学生信息</td>
                </tr>
                <tr>
                    <td>姓名</td><td><input type="text" name="name"/></td>
                </tr>
                <tr>
                    <td>性别</td>
                    <td>
                        <input type="radio" name="sex" value="" checked="checked"/><input type="radio" name="sex" value=""/></td>
                </tr>
                <tr>
                    <td>专业</td><td><input type="text" name="specialty"/></td>
                </tr>
                <tr>
                    <td>年级</td><td>
                    <select name="grade">
                         <option value="大一">大一</option>
                         <option value="大二">大二</option>
                         <option value="大三">大三</option>
                         <option value="大四">大四</option>
                    </select>
                    </td>
                </tr>
                <tr>
                    <td align="center" colspan="2"><input type="submit" value="添加"/></td>
                </tr>
        </table>
        </center>
        </form>
      </body>
    </html>

    完!!

  • 相关阅读:
    [LeetCode]Binary Tree Inorder Traversal
    [LeetCode]Binary Tree Preorder Traversal
    [LeetCode]Number of 1 Bits
    [LeetCode]Best Time to Buy and Sell Stock IV
    第四章 线程
    第三章 进程描述和控制
    第二章 操作系统概述
    第一章 计算机系统概述
    Qt创建对话框的三种方法
    strdup函数
  • 原文地址:https://www.cnblogs.com/yangchas/p/11167476.html
Copyright © 2020-2023  润新知