Java Web項目案例之---登錄和修改(JSP)


登錄和修改(JSP)

通過案例學習jsp的常用知識點:

1.創建一個Map集合,用於存放學生信息,將學生信息存入Map中

2.通過page將需要的包導入

3.用request.getParameter通過name得到輸入框的內容
4.session生命周期在整個會話期間,整個會話中都可以得到session中存放的信息,可以用於存放登錄學生的學號,使每個頁面都可以得到學號
5.application生命周期在整個web容器的生命期間,可以用於記錄此項目的瀏覽人數,可以得到application中存放的數據
6. 點擊修改,將所點擊的學生信息傳給另一個頁面

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 {

    //創建一個Map集合,用於存放學生信息

    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"));

    }



    /**

     * 用於檢測登錄時用戶輸入的賬戶和密碼

     * @param stu

     * @return 返回布爾類型的數據

     */

    public static boolean login(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;

                break;

            }

        }

        return b;

    }

}

 

 

index.jsp

<%--

  Created by IntelliJ IDEA.

  User: Administrator

  Date: 2019/7/22 0022

  Time: 上午 9:04

  To change this template use File | Settings | File Templates.

--%>

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

<html>

<head>

    <title>Title</title>

    <style>

        .log{

            align-content: center;

            text-align: center;

        }

    </style>

</head>

<body>

<!--登錄頁面-->

<form action="loginController.jsp" method="post">

    <div class="log">

    <h3>登錄</h3>

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

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

    <input type="submit" value="確定"/></div>

</form>



</body>

</html>

 

 

loginController.jsp

<%@ page import="util.StudentUtil" %><%--

  Created by IntelliJ IDEA.

  User: Administrator

  Date: 2019/7/22 0022

  Time: 上午 9:04

  To change this template use File | Settings | File Templates.

--%>

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

<!--通過page將需要的包導入 -->

<%@page import="entity.Student" %>

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

<%@ page import="javafx.application.Application" %>

<html>

<head>

    <title>Title</title>

    <style>



        table{

            align-content: center;

            text-align: center;

            width: 500px;

            height: 200px;

            border: 2px solid #8ea4ff;

        }

        tr,td{

            border: 2px solid #8ea4ff;



        }

    </style>

</head>

<body>

<%



    String sno=request.getParameter("sno");//用request.getParameter通過name得到輸入框的內容

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

    Student stu=new Student(sno,password,null);//創建一個Student對象

    boolean b=StudentUtil.login(stu);//調用StudentUtil的login方法檢測學號和密碼

    if(b){

        //session生命周期在整個會話期間,整個會話中都可以得到session中存放的信息

        //此處用於存放登錄學生的學號,使每個頁面都可以得到學號

        session.setAttribute("count",sno);

        //application生命周期在整個web容器的生命期間

        //此處用於記錄此項目的瀏覽人數

        //得到application中存放的數據

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

        if(o==null){//如果application中沒有數據,則向application中存入1

            application.setAttribute("num",1);

        }else{

            //如果application中已經存在數據,則存放的數據加1

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

            application.setAttribute("num",num+1);

        }



%>

<h2>學生信息</h2>

<h3 style="text-align: right">登錄賬號:<%=session.getAttribute("count")%></h3>

<h3 style="text-align: right">瀏覽量:<%=application.getAttribute("num")%></h3>



<hr>



<table>

    <tr>

        <td>學號</td>

        <td>密碼</td>

        <td>姓名</td>

        <td></td>

    </tr>

    <%

        for(Student st:StudentUtil.map.values()){

    %>

    <tr>

        <td><%=st.getSno()%></td>

        <td><%=st.getPassword()%></td>

        <td><%=st.getName()%></td>

        <!--點擊修改,將所點擊的學生信息傳給另一個頁面-->

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

    </tr>

    <%}%>

</table>
<%}else{
//拋出異常
throw new Exception("賬戶或密碼錯誤");
}%>
</body> </html>

 

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2019/7/22 0022
  Time: 下午 5:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%=exception.getMessage()%>
</body>
</html>

 

 

update.jsp

<%--

  Created by IntelliJ IDEA.

  User: Administrator

  Date: 2019/7/22 0022

  Time: 上午 10:10

  To change this template use File | Settings | File Templates.

--%>

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

<html>

<head>

    <title>Title</title>

    <style>

        table{

            width: 400px;

            height: 150px;

            border:2px solid #8ea4ff;

            text-align: center;



        }

        tr,td{

            border:2px solid #8ea4ff;

        }

    </style>

</head>

<body>

<h3 style="text-align: right">登錄賬號:<%=session.getAttribute("count")%></h3>

<form action="updateController.jsp" method="post">

    <h2>修改信息</h2>

    <hr>

<table>

    <tr>

        <td style="width: 30%">

            學號

        </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>

 

 

updateController.jsp

<%--

  Created by IntelliJ IDEA.

  User: Administrator

  Date: 2019/7/22 0022

  Time: 上午 10:18

  To change this template use File | Settings | File Templates.

--%>

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

<%@page import="entity.Student" %>

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

<html>

<head>

    <title>Title</title>

    <style>



        table{

            align-content: center;

            text-align: center;

            width: 500px;

            height: 200px;

            border: 2px solid #8ea4ff;

        }

        tr,td{

            border: 2px solid #8ea4ff;



        }

    </style>

</head>

<body>

<%

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

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

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

    for(String s:StudentUtil.map.keySet()){//遍歷map中所有的鍵

        if(StudentUtil.map.get(s).getSno().equals(sno)){

            StudentUtil.map.get(s).setSno(sno);

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

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

        }

    }

%>

修改成功!

<h2>學生信息</h2>

<h3 style="text-align: right">登錄賬號:<%=session.getAttribute("count")%></h3>

<hr>



<table>

    <tr>

        <td>學號</td>

        <td>密碼</td>

        <td>姓名</td>

        <td></td>

    </tr>

    <%

        for(Student st:StudentUtil.map.values()){

    %>

    <tr>

        <td><%=st.getSno()%></td>

        <td><%=st.getPassword()%></td>

        <td><%=st.getName()%></td>

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

    </tr>

    <%}%>

</table>

</body>

</html>
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM