1. 程式人生 > >Java專案案例之---登入註冊和增刪改查(jsp+servlet)

Java專案案例之---登入註冊和增刪改查(jsp+servlet)

登入註冊和增刪改查(jsp+servlet)

(一)功能介紹

1.使用者輸入正確的密碼進行登入

2.新使用者可以進行註冊

3.登入後顯示學生的資訊表

4.可以新增學生

5.可以修改學生已有資訊

6.可以刪除已有學生

7.可以顯示登入的使用者學號(每個頁面都可以通過呼叫session顯示)

8.可以記錄瀏覽量

(二)頁面展示

登入頁面

 

註冊頁面

 

學生資訊頁面

 

新增頁面

 

新增之後的學生資訊表

 

修改頁面

 

修改之後的學生資訊表

 

刪除之後的學生資訊表

 

(三)專案程式碼

 

package entity;

public class Student {
     private String sno;
     private String password;
     private String name;

    public Student(String sno, String password, String name) {
        this.sno = sno;
        this.password = password;
        this.name = name;
    }

    public String getSno() {
        return sno;
    }

    public void setSno(String sno) {
        this.sno = sno;
    }

    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;
    }
}

 

package util;



import entity.Student;



import java.util.HashMap;



public class StudentUtil {

    public static HashMap<String, Student> map=new HashMap<String,Student>();

    static{

        map.put("101",new Student("101","123","lili"));

        map.put("102",new Student("102","123","lisa"));

        map.put("103",new Student("103","123","coco"));

        map.put("104",new Student("104","123","mark"));

    }

    public static boolean log(Student stu){

        boolean b=false;

        for(String s:map.keySet()){

            if(map.get(s).getSno().equals(stu.getSno())&&map.get(s).getPassword().equals(stu.getPassword())){

                b=true;

            }

        }

        return b;

    }

    public static boolean reg(Student stu){

        map.put(stu.getSno(),stu);

        return true;

    }

}

 

package servlet;



import entity.Student;

import org.omg.CORBA.Request;

import util.StudentUtil;



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;

@WebServlet("/LoginServlet")

public class LoginServlet extends HttpServlet {

    @Override

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String sno=req.getParameter("sno");

        String password=req.getParameter("password");

        Student s=new Student(sno,password,null);

        boolean b=StudentUtil.log(s);

        if(b==true){

            req.setAttribute("sno",sno);

            req.getRequestDispatcher("logon.jsp").forward(req,resp);

        }else{

            req.getRequestDispatcher("error.jsp").forward(req,resp);

        }

    }



    @Override

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doPost(req, resp);

    }

}

 

package servlet;



import entity.Student;

import util.StudentUtil;



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;

@WebServlet("/RegisterServlet")

public class RegisterServlet extends HttpServlet {

    @Override

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String sno=req.getParameter("sno");

        String password=req.getParameter("password");

        String name=req.getParameter("name");

        StudentUtil.reg(new Student(sno,password,name));

        req.getRequestDispatcher("/index.jsp").forward(req,resp);

    }



    @Override

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doPost(req, resp);

    }

}

 

package servlet;



import entity.Student;

import util.StudentUtil;



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;

@WebServlet("/InsertServlet")

public class InsertServlet extends HttpServlet {

    @Override

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String sno=req.getParameter("sno");

        String password=req.getParameter("password");

        String name=req.getParameter("name");

        StudentUtil.map.put(sno,new Student(sno,password,name));

        req.getRequestDispatcher("student.jsp").forward(req,resp);

    }



    @Override

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doPost(req, resp);

    }

}

 

package servlet;



import util.StudentUtil;



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;

@WebServlet("/UpdateServlet")

public class UpdateServlet extends HttpServlet {

    @Override

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String sno=req.getParameter("sno");

        String password=req.getParameter("password");

        String name=req.getParameter("name");

        StudentUtil.map.get(sno).setName(name);

        StudentUtil.map.get(sno).setPassword(password);

        req.getRequestDispatcher("student.jsp").forward(req,resp);

    }



    @Override

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doPost(req, resp);

    }

}
package servlet;



import util.StudentUtil;



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;

@WebServlet("/DeleteServlet")

public class DeleteServlet extends HttpServlet {

    @Override

    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

       String sno= req.getParameter("sno");

        StudentUtil.map.remove(sno);

        req.getRequestDispatcher("student.jsp").forward(req,resp);

    }



    @Override

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doPost(req, resp);

    }

}

 

 

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>

    <title>Title</title>

</head>

<body>

<form action="LoginServlet" method="post">

    <div class="log">

        <h2>登入</h2>

        學號:<input type="text" name="sno"><br>

        密碼:<input type="password" name="password"> <br>

        <input type="submit" value="確認登入">

        <a href="register.jsp" >註冊</a>

    </div>

</form>

</body>

</html>

 

 

register.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>

    <title>Title</title>

</head>

<body>

<form action="RegisterServlet" method="post">

    學號:<input type="text" name="sno"><br>

    密碼:<input type="text" name="password"><br>

    姓名:<input type="text" name="name"><br>

    <input type="submit" value="確認">

</form>

</body>

</html>

 

 

logon.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>

    <title>Title</title>

</head>

<body>

<%

    session.setAttribute("sno",request.getAttribute("sno"));

    Object o=application.getAttribute("count");

    if(o==null){

        application.setAttribute("count",1);

    }else{

        int c=Integer.parseInt(o.toString());

        application.setAttribute("count",c+1);

    }

%>

<%

request.getRequestDispatcher("student.jsp").forward(request,response);

%>

</body>

</html>

 

 
student.jsp
<%@ page import="util.StudentUtil" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>

    <title>Title</title>

</head>

<body>



<a href="insert.jsp">增加</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<span>使用者:<%=session.getAttribute("sno")%></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

<span>瀏覽量:<%=application.getAttribute("count")%></span>

<table>

    <tr>

        <td>學號</td>

        <td>密碼</td>

        <td>姓名</td>

        <td></td>

    </tr>

    <%

      for(String s: StudentUtil.map.keySet()){%>

          <tr>

              <td><%=StudentUtil.map.get(s).getSno()%></td>

              <td><%=StudentUtil.map.get(s).getPassword()%></td>

              <td><%=StudentUtil.map.get(s).getName()%></td>

              <td><a href="update.jsp?sno=<%=StudentUtil.map.get(s).getSno()%>&&password=<%=StudentUtil.map.get(s).getPassword()%>&&name=<%=StudentUtil.map.get(s).getName()%>">修改</a>

                  <a href="DeleteServlet?sno=<%=StudentUtil.map.get(s).getSno()%>">刪除</a></td>

          </tr>

     <% } %>

</table>



</body>

</html>

 

 
error.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>

    <title>Title</title>

</head>

<body>

賬戶名或密碼錯誤

<a href="index.jsp">返回登入</a>

</body>

</html>

 

 

insert.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>

<head>

    <title>Title</title>

</head>

<body>

<form action="InsertServlet" method="post">

    學號:<input type="text" name="sno"/><br>

    密碼:<input type="text" name="password"/><br>

    姓名:<input type="text" name="name"/><br>

    <input type="submit" value="確認新增">

</form>



</body>

</html>

 

 

update.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="UpdateServlet" method="post">
    <table>
        <tr>
            <td>學號</td>
            <td><input type="text" name="sno" value="<%=request.getParameter("sno")%>"></td>
        </tr>
        <tr>
            <td>密碼</td>
            <td><input type="text" name="password" value="<%=request.getParameter("password")%>"></td>
        </tr>
        <tr>
            <td>姓名</td>
            <td><input type="text" name="name" value="<%=request.getParameter("name")%>"></td>
        </tr>
    </table>
    <input type="submit" value="確認修改">
</form>
</body>
</html>

&n