整合營銷服務商

          電腦端+手機端+微信端=數(shù)據(jù)同步管理

          免費咨詢熱線:

          JSP中四種傳遞參數(shù)的方法(個人總結(jié),簡單實用)

          sp中四種傳遞參數(shù)的方法,我覺得總結(jié)一下,挺好的,以備后用!

          1、form表單

          2、request.setAttribute();和request.getAttribute();

          3、超鏈接:<a herf="index.jsp"?a=a&b=b&c=c>name</a>

          1、form表單

          form.jsp:

          <%@page contentType="text/html; charset=GB2312"%> 
          <html> 
           <head> 
           <title> 
           form.jsp file 
           </title> 
           </head> 
           
           <body style="background-color:lightblue"> 
           
           <h2 style="font-family:arial;color:red;font-size:25px;text-align:center">登錄頁面</h2> 
           
           <form action="result.jsp" method="get" align="center"> 
           姓名:<input type="text" name="name" size="20" value="" maxlength="20"><br/> 
           
           密碼:<input type="password" name="password" size="20" value="" maxlength="20"><br/> 
           
           <!--在愛好前空一個空格,是為了排版好看些--> 
           
           愛好:<input type="checkbox" name="hobby" value="唱歌">唱歌 
           <input type="checkbox" name="hobby" value="足球">足球 
           <input type="checkbox" name="hobby" value="籃球">籃球<br/><br/> 
           
           <input type="submit" name="submit" value="登錄"> 
           <input type="reset" name="reset" value="重置"><br/> 
           </form> 
           
           </body> 
          </html>
          

          result.jsp:

           1 <%@page language="java" import="java.util.*" pageEncoding="GB2312"%> 
           2 <html> 
           3 <head> 
           4 <title> 
           5 result.jsp file 
           6 </title> 
           7 </head> 
           8 
           9 <body bgcolor="ffffff"> 
          10 <% 
          11 request.setCharacterEncoding("GB2312"); 
          12 
          13 String name=request.getParameter("name"); 
          14 name=new String(name.getBytes("iso-8859-1"),"GB2312"); 
          15 
          16 String pwd=request.getParameter("password"); 
          17 String[] hobby=request.getParameterValues("hobby");//注意這里的函數(shù)是getParameterValues()接受一個數(shù)組的數(shù)據(jù) 
          18 
          19 %> 
          20 
          21 <% 
          22 if(!name.equals("") && !pwd.equals("")) 
          23 { 
          24 %> 
          25 
          26 您好!登錄成功!<br/> 
          27 姓名:<%=name%><br/> 
          28 密碼:<%=pwd%><br/> 
          29 愛好:<% 
          30 for(String ho: hobby) 
          31 { 
          32 ho=new String(ho.getBytes("iso-8859-1"),"GB2312"); 
          33 out.print(ho+" "); 
          34 } 
          35 %> 
          36 <% 
          37 } 
          38 else 
          39 { 
          40 %> 
          41 請輸入姓名或密碼! 
          42 <% 
          43 } 
          44 %> 
          45 </body> 
          46 </html>
          

          注意:form表單的提交方式為get,在參數(shù)傳遞時會遇到中文亂碼的問題,一個簡單的解決方法是,將接受到的字符串先轉(zhuǎn)換成一個byte數(shù)組,再用String構(gòu)造一個新的編碼格式的String,如:

          1 String name=request.getParameter("name"); 
          2 name=new String(name.getBytes("iso-8859-1"),"GB2312"); 
          

          如果form表單的提交方式為post,解決亂碼問題的簡單辦法是,使用 request.setCharacterEncoding("GB2312");設置request的編碼方式。

          為什么會出現(xiàn)中文亂碼問題呢?因為Tomcat服務器默認的系統(tǒng)編碼方式為iso- 8859-1,你傳遞參數(shù)給服務器時,使用的是默認的iso-8859-1的編碼方式,但是服務器向你返回信息時,是按page指令中設置的編碼方式, 如:<%@page language="java" import="java.util.*" pageEncoding="GB2312"%>,這樣就混合了兩種編碼方式,所以會出現(xiàn)亂碼,所以解決之道就是統(tǒng)一傳遞和接收的編碼方式。

          2、request.setAttribute()和request.getAttribute()

          set.jsp:

          <%@page contentType="text/html; charset=GB2312"%> 
          <html> 
           <head> 
           <title> 
           set.jsp file 
           </title> 
           </head> 
           
           <body style="background-color:lightblue"> 
           <% 
           request.setAttribute("name","心雨"); 
           %> 
           <jsp:forward page="get.jsp"/> 
           </body> 
          </html>
          

          get.jsp:

          <%@page contentType="text/html; charset=GB2312"%> 
          <html> 
           <head> 
           <title> 
           get.jsp file 
           </title> 
           </head> 
           
           <body style="background-color:lightblue"> 
           <% 
           out.println("傳遞過來的參數(shù)是:"+request.getAttribute("name")); 
           %> 
           </body> 
          </html> 
          

          request.setAttribute()和request.getAttribute()是配合<jsp:forward>或是include指令來實現(xiàn)的。

          3、超鏈接:<a herf="index.jsp?a=a&b=b&c=c">name</a>

          href.jsp:

          <%@page contentType="text/html; charset=GB2312"%> 
          <html> 
           <head> 
           <title> 
           href.jsp file 
           </title> 
           </head> 
           
           <body style="background-color:lightblue"> 
           <a href="getHerf.jsp?name=心雨&password=123">傳遞參數(shù)</a> 
           </body> 
          </html> 
          

          getHref.jsp:

          <%@page contentType="text/html; charset=GB2312"%> 
          <html> 
           <head> 
           <title> 
           getHref.jsp file 
           </title> 
           </head> 
           
           <body style="background-color:lightblue"> 
           <% 
           String name=request.getParameter("name"); 
           name=new String(name.getBytes("iso-8859-1"),"gb2312"); 
           
           out.print("name:"+name); 
           %> 
           <br/> 
           <% 
           out.print("password:"+request.getParameter("password")); 
           %> 
           </body> 
          </html> 
          

          這種傳遞參數(shù)的方法和form表單的get方式類似,是通過地址欄傳遞的參數(shù),其亂碼解決方法也和form 的get方式一樣。

          4、<jsp:param>

          param.jsp:

          <%@page contentType="text/html; charset=GB2312"%> 
          <html> 
           <head> 
           <title> 
           param.jsp file 
           </title> 
           </head> 
           
           <body style="background-color:lightblue"> 
           
           <%request.setCharacterEncoding("GB2312");%> 
           
           <jsp:forward page="getParam.jsp"> 
           <jsp:param name="name" value="心雨"/> 
           <jsp:param name="password" value="123"/> 
           </jsp:forward> 
           
           </body> 
          </html>
          

          getParam.jsp:

          <%@page contentType="text/html; charset=GB2312"%> 
          <html> 
           <head> 
           <title> 
           getParam.jsp file 
           </title> 
           </head> 
           
           <body style="background-color:lightblue"> 
           <% 
           String name=request.getParameter("name"); 
           out.print("name:"+name); 
           %> 
           <br/> 
           <% 
           out.print("password:"+request.getParameter("password")); 
           %> 
           </body> 
          </html> 
          

          這里發(fā)現(xiàn)了一個奇怪的問題,還是在中文亂碼的問題上,在form表單的例子中,如果傳遞方式為post,則只需要在接收參數(shù)的頁面設置request的編 碼方式就可以了,即request.setCharacterEncoding("GB2312");,注意是在接收參數(shù)的頁面,如果將該句放到form 表單里,那么不起作用,仍然是亂碼。而在本例中,為了使傳遞的參數(shù)不出現(xiàn)亂碼,卻是將 request.setCharacterEncoding("GB2312");放在發(fā)送參數(shù)的頁面中,才會正常顯示中文,放在接收參數(shù)的頁面中,不起 作用。也許這就是<jsp:param>和form表單傳遞參數(shù)不同的地方。為什么會有這個不同呢?可能是因為form表單中的參數(shù)是由客戶 端傳送到服務端上的,需要經(jīng)過一個request的打包過程,但是<jsp:param>傳遞的參數(shù)本身就是在服務器端的,不需要經(jīng)歷由客戶 端到服務端這么一個過程,但是服務器里的參數(shù)傳遞是這么回事呢?這個問題,我不知道了!真是知識是一個擴大的圓圈,你知道的越多,那么不知道的就越多!努 力吧!

          一、寫在前面

          二、效果圖

          三、實現(xiàn)思路

          四、實現(xiàn)代碼

          1、login總界面

          2、registercheck總代碼

          3、logoutcheck總代碼

          4、amendcheck總代碼


          相關文章

          一、寫在前面

          哈嘍~大家好,這篇呢我們來看看用 JSP 連接 MySQL 登入注冊項目實踐,這里就可能有人問了,唉?追桑~前些天不是寫了 jsp 登入注冊的項目嗎?怎么這次還在寫呢?哈哈,您別擔心,這次呢,肯定和上次不同,我們先來看看效果吧!

          二、效果圖

          數(shù)據(jù)庫界面

          感覺是不是不一樣了,哈哈哈,那么接下來我們來看看是怎么實現(xiàn)的。

          三、實現(xiàn)思路

          首先我們這里很明顯,有四個總頁面分別是 login(登入界面)、logout(注銷界面)、amend(修改界面)、register(注冊界面),這四個總頁面分別對應著檢查頁面(check)、成功頁面(success)、失敗頁面(fail)。建立之好后,通過 from 的 action 來進行跳轉(zhuǎn),我們先來看看 MySQL (數(shù)據(jù)庫)表名叫 login。

          我們這里數(shù)據(jù)庫共三列,第一列叫 name (用戶名)、pass(密碼)、time(注冊時間),name 與 pass 都是 int(整型) 類型的,time 是 varchar (可變長類型),如圖。

          四、實現(xiàn)代碼

          1、login總界面

          首先我們先有個頁面,有基本的用戶名框,密碼框,兩按鈕,一個注冊,一個注銷,通過 from進行跳轉(zhuǎn),代碼如下

           <form method="post" action="check.jsp">
                  <input type="text" name="user" style="width: 300px;height: 50px" placeholder="請輸入用戶名:"
                  > <br>
                  <input type="password" name="pass" style="width: 300px;height: 50px" placeholder="請輸入密碼:" > <br>
                  <button type="submit" style="width:80px;height:40px; font-size: 20px" class="clear">登錄</button>
                  <button type="reset" style="width:80px;height:40px; font-size: 20px" class="clear">重置</button>
                  <br>
                  沒有賬號?<a href="register.jsp">點擊注冊</a><br>
                  不想用了?<a href="logout.jsp">點擊注銷</a>
              </form>
          

          用 check 連接數(shù)據(jù)庫(如何連接數(shù)據(jù)庫,前面文章已經(jīng)給出了,有興趣的小伙伴可以看看前面寫的文章,也放在前面了) 同樣的道理,還是那五個步驟(這里就不過多的解釋,可以看看上面表格給出的文章),先來看看代碼。

          String user = request.getParameter("user"); // getParameter  與 getAttribute  區(qū)別
                  String pass = request.getParameter("pass");
                  // String getParameter(String name):根據(jù)參數(shù)名稱獲取參數(shù)值
                  // getAttribute()獲取的是服務器設置的數(shù)據(jù)。getAttribute() 方法返回指定屬性名的屬性值。
           
                  try {
                      Class.forName("com.mysql.cj.jdbc.Driver");
                      String url = "jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC";
                      String user1 = "root";
                      String pass1 = "123456";
                      Connection connection = DriverManager.getConnection(url,user1,pass1);
                      String sql = "select * from login where name=? and pass=?";
           
                      PreparedStatement ps = connection.prepareStatement(sql);
                      ps.setString(1,user);
                      ps.setString(2,pass);
                      ResultSet re = ps.executeQuery();
           
                      if (re.next()){
                          response.sendRedirect("loginsuccess.jsp");
                          session.setAttribute("user",user);
                      }else {
                          response.sendRedirect("loginfail.jsp");
                      }
           
                  } catch (ClassNotFoundException e) {
                      e.printStackTrace();
                  } catch (SQLException e) {
                      e.printStackTrace();
                  }
          

          這里 response.sendRedirect 跳轉(zhuǎn)了兩個頁面一個 loginsuccess 和 loginfail 的兩個界面,下面我們來看看,這兩個文件(其實很簡單)

          loginsuccess 代碼

           <div class="form">
                          <h2> <h22>登錄成功</h22><br>
                          </h2>
                          <fon>恭喜您成功登入 <br>    歡迎使用 <br>
                              <a class="a1" href="login.jsp">返回登入界面</a>
                          </fon>
                      </div>
          

          loginfail 代碼:

          <h2> <h22>登錄失敗</h22><br>
                          </h2>
                          <fon>寶~是不是賬號或密碼記錯惹? <br>
                              <a class="a1" href="login.jsp">返回登入界面</a><br>
                              <p1><a href="amend.jsp">點擊修改密碼</a></p1>
                          </fon>

          這里我們點擊運行看看效果

          這里我們用兩個升級 大裝備(html)(css) 來美化一下我們的頁面,這里我們頁面美化的,用的是這位大佬的頁面(博主名為鍵盤奏鳴曲),大家可以來看看,點擊鏈接

          HTML 代碼

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

          <head>

          <meta charset="UTF-8">

          <meta name="viewport" content="width=device-width, initial-scale=1.0">

          <link rel="stylesheet" href="css.css">

          <title>123</title>



          </head>

          <body>


          <section>

          <div class="color"></div>

          <div class="color"></div>

          <div class="color"></div>

          <div class="box">

          <div class="circle" style="--x:0"></div>

          <div class="circle" style="--x:1"></div>

          <div class="circle" style="--x:2"></div>

          <div class="circle" style="--x:3"></div>

          <div class="circle" style="--x:4"></div>

          <div class="container">

          <div class="form">

          <h2>登錄</h2>

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

          <div class="inputBox">

          <input type="text" placeholder="姓名" name="user">


          </div>

          <div class="inputBox">

          <input type="password" placeholder="密碼" name="pass">


          </div>

          <div class="inputBox">

          <input type="submit" value="登錄">


          </div>

          <p class="forget">不想用了?<a href="logout.jsp">

          點擊這里

          </a></p>

          <p class="forget">沒有賬戶?<a href="register.jsp">

          注冊

          </a></p>

          </form>

          </div>

          </div>

          </div>

          </section>

          </body>


          </html>

          CSS 代碼

          /*.center{*/
          /*    text-align:center;*/
          /*    margin-top: 50px;*/
          /*}*/
          .fon{
              font-size: 40px;
          }
          /*body{*/
          /*    background: url("images/image-2.jpg") no-repeat 0 0;*/
          /*    background-size: 100% 100%;*/
          /*    text-decoration:none;*/
          /*}*/
           
          /*input {*/
          /*    background-color: transparent;*/
          /*    outline: none;*/
          /*    color: black;*/
          /*}*/
          /*.clear{*/
          /*    opacity:0.3;*/
          /*}*/
           
          * {
              margin: 0;
              padding: 0;
              box-sizing: border-box;
          }
           
          /* 使用flex布局,讓內(nèi)容垂直和水平居中 */
           
          section {
              /* 相對定位 */
              position: relative;
              overflow: hidden;
              display: flex;
              justify-content: center;
              align-items: center;
              min-height: 100vh;
              /* linear-gradient() 函數(shù)用于創(chuàng)建一個表示兩種或多種顏色線性漸變的圖片 */
              background: linear-gradient(to bottom, #f1f4f9, #dff1ff);
          }
           
          /* 背景顏色 */
           
          section .color {
              /* 絕對定位 */
              position: absolute;
              /* 使用filter(濾鏡) 屬性,給圖像設置高斯模糊*/
              filter: blur(200px);
          }
           
          /* :nth-child(n) 選擇器匹配父元素中的第 n 個子元素 */
           
          section .color:nth-child(1) {
              top: -350px;
              width: 600px;
              height: 600px;
              background: #ff359b;
          }
           
          section .color:nth-child(2) {
              bottom: -150px;
              left: 100px;
              width: 500px;
              height: 500px;
              background: #fffd87;
          }
           
          section .color:nth-child(3) {
              bottom: 50px;
              right: 100px;
              width: 500px;
              height: 500px;
              background: #00d2ff;
          }
           
          .box {
              position: relative;
          }
           
          /* 背景圓樣式 */
           
          .box .circle {
              position: absolute;
              background: rgba(255, 255, 255, 0.1);
              /* backdrop-filter屬性為一個元素后面區(qū)域添加模糊效果 */
              backdrop-filter: blur(5px);
              box-shadow: 0 25px 45px rgba(0, 0, 0, 0.1);
              border: 1px solid rgba(255, 255, 255, 0.5);
              border-right: 1px solid rgba(255, 255, 255, 0.2);
              border-bottom: 1px solid rgba(255, 255, 255, 0.2);
              border-radius: 50%;
              /* 使用filter(濾鏡) 屬性,改變顏色。
              hue-rotate(deg)  給圖像應用色相旋轉(zhuǎn)
              calc() 函數(shù)用于動態(tài)計算長度值
              var() 函數(shù)調(diào)用自定義的CSS屬性值x*/
              filter: hue-rotate(calc(var(--x) * 70deg));
              /* 調(diào)用動畫animate,需要10s完成動畫,
              linear表示動畫從頭到尾的速度是相同的,
              infinite指定動畫應該循環(huán)播放無限次*/
              animation: animate 10s linear infinite;
              /* 動態(tài)計算動畫延遲幾秒播放 */
              animation-delay: calc(var(--x) * -1s);
          }
           
          /* 背景圓動畫 */
           
          @keyframes animate {
              0%, 100% {
                  transform: translateY(-50px);
              }
              50% {
                  transform: translateY(50px);
              }
          }
           
          .box .circle:nth-child(1) {
              top: -50px;
              right: -60px;
              width: 100px;
              height: 100px;
          }
           
          .box .circle:nth-child(2) {
              top: 150px;
              left: -100px;
              width: 120px;
              height: 120px;
              z-index: 2;
          }
           
          .box .circle:nth-child(3) {
              bottom: 50px;
              right: -60px;
              width: 80px;
              height: 80px;
              z-index: 2;
          }
           
          .box .circle:nth-child(4) {
              bottom: -80px;
              left: 100px;
              width: 60px;
              height: 60px;
          }
           
          .box .circle:nth-child(5) {
              top: -80px;
              left: 140px;
              width: 60px;
              height: 60px;
          }
           
          /* 登錄框樣式 */
           
          .container {
              position: relative;
              width: 400px;
              min-height: 400px;
              background: rgba(255, 255, 255, 0.1);
              display: flex;
              justify-content: center;
              align-items: center;
              backdrop-filter: blur(5px);
              box-shadow: 0 25px 45px rgba(0, 0, 0, 0.1);
              border: 1px solid rgba(255, 255, 255, 0.5);
              border-right: 1px solid rgba(255, 255, 255, 0.2);
              border-bottom: 1px solid rgba(255, 255, 255, 0.2);
          }
           
          .form {
              position: relative;
              width: 100%;
              height: 100%;
              padding: 50px;
          }
           
          /* 登錄標題樣式 */
           
          .form h2 {
              text-align: center;
              position: relative;
              color: #fff;
              font-size: 40px;
              font-weight: 600;
              letter-spacing: 5px;
              margin-bottom: 30px;
              cursor: pointer;
          }
           
          .form h2 h22 {
              top: -40px;
              text-align: center;
              position: relative;
              color: #fff;
              font-size: 40px;
              font-weight: 600;
              letter-spacing: 5px;
              margin-bottom: 30px;
              cursor: pointer;
          }
           
          .form .a1, .form p1 {
              bottom: -90px;
              left: 50px;
              position: relative;
              color: #fff;
              font-size: 18px;
              font-weight: 600;
              letter-spacing: 5px;
              /*margin-bottom: 10px;*/
              cursor: pointer;
              text-decoration: none;
          }
           
          .form p1 a{
           
              position: relative;
              color: #fff;
              font-size: 18px;
              font-weight: 600;
              letter-spacing: 5px;
              /*margin-bottom: 10px;*/
              cursor: pointer;
              text-decoration: none;
          }
           
          .form fon {
              top: -30px;
              left: 30px;
              position: relative;
              color: #fff;
              font-size: 28px;
              font-weight: 600;
              letter-spacing: 5px;
              margin-bottom: 30px;
              cursor: pointer;
          }
          /* 登錄標題的下劃線樣式 */
           
          .form h2::before {
              content: "";
              position: absolute;
              left: 0;
              bottom: -10px;
              width: 0px;
              height: 3px;
              background: #fff;
              transition: 0.5s;
          }
           
          .form h2 h22::before {
              content: "";
              position: absolute;
              /*left: 0;*/
              /*bottom: -10px;*/
              /*width: 0px;*/
              /*height: 3px;*/
              /*background: #fff;*/
              /*transition: 0.5s;*/
          }
           
          .form h2:hover:before {
              width: 53px;
          }
           
          .form .inputBox {
              width: 100%;
              margin-top: 20px;
          }
           
          /* 輸入框樣式 */
           
          .form .inputBox input {
              width: 100%;
              padding: 10px 20px;
              background: rgba(255, 255, 255, 0.2);
              outline: none;
              border: none;
              border-radius: 30px;
              border: 1px solid rgba(255, 255, 255, 0.5);
              border-right: 1px solid rgba(255, 255, 255, 0.2);
              border-bottom: 1px solid rgba(255, 255, 255, 0.2);
              font-size: 16px;
              letter-spacing: 1px;
              color: #fff;
              box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
          }
           
          .form .inputBox input::placeholder {
              color: #fff;
          }
           
          /* 登錄按鈕樣式 */
           
          .form .inputBox input[type="submit"],.form .inputBox input[type="reset"]  {
              background: #fff;
              color: #666;
              max-width: 100%;
              margin-bottom: 20px;
              font-weight: 600;
              cursor: pointer;
          }
           
          .forget {
              margin-top: 6px;
              color: #fff;
              letter-spacing: 1px;
          }
           
          .forget a {
              color: #fff;
              font-weight: 600;
              text-decoration: none;
          }
          

          同樣的道理我們來升級一下 loginsuccess 與 loginfail 。

          loginsuccess 代碼

          <%@ page contentType="text/html;charset=UTF-8" language="java" %>
          <html>
          <head>
              <title>登入成功界面</title>
              <link rel="stylesheet" href="css.css" type="text/css">
          </head>
          <body>
          <%--<div class="center">--%>
          <%--    <p class="fon">登入成功界面</p>--%>
          <%--    <p class="fon1">恭喜您成功登入,歡迎使用</p>--%>
          <%--    <a href="login.jsp">點擊退出,返回登入界面</a>--%>
          <%--</div>--%>
           
          <section>
              <div class="color"></div>
              <div class="color"></div>
              <div class="color"></div>
              <div class="box">
                  <div class="circle" style="--x:0"></div>
                  <div class="circle" style="--x:1"></div>
                  <div class="circle" style="--x:2"></div>
                  <div class="circle" style="--x:3"></div>
                  <div class="circle" style="--x:4"></div>
                  <div class="container">
                      <div class="form">
                          <h2> <h22>登錄成功</h22><br>
                          </h2>
                          <fon>恭喜您成功登入 <br>    歡迎使用 <br>
                              <a class="a1" href="login.jsp">返回登入界面</a>
                          </fon>
                      </div>
                  </div>
              </div>
          </section>
           
          </body>
          </html>
          

          loginfail 代碼

          <%@ page contentType="text/html;charset=UTF-8" language="java" %>
          <html>
          <head>
              <title>登入失敗界面</title>
              <link rel="stylesheet" href="css.css" type="text/css">
          </head>
          <body>
          <%--<div class="center">--%>
          <%--    <p class="fon">登入失敗界面</p>--%>
          <%--    <p class="fon1">對不起,您賬號或密碼有誤,請返回登入界面</p>--%>
          <%--    <a href="login.jsp">返回登入界面</a><br>--%>
          <%--    忘記密碼?<a href="amend.jsp">點擊修改密碼</a>--%>
          <%--</div>--%>
           
          <section>
              <div class="color"></div>
              <div class="color"></div>
              <div class="color"></div>
              <div class="box">
                  <div class="circle" style="--x:0"></div>
                  <div class="circle" style="--x:1"></div>
                  <div class="circle" style="--x:2"></div>
                  <div class="circle" style="--x:3"></div>
                  <div class="circle" style="--x:4"></div>
                  <div class="container">
                      <div class="form">
                          <h2> <h22>登錄失敗</h22><br>
                          </h2>
                          <fon>寶~是不是賬號或密碼記錯惹? <br>
                              <a class="a1" href="login.jsp">返回登入界面</a><br>
                              <p1><a href="amend.jsp">點擊修改密碼</a></p1>
                          </fon>
                          
                      </div>
                  </div>
              </div>
          </section>
           
          </body>
          </html>
          

          點擊運行,我們來看看效果

          那么這里我們是完成了,login總界面的效果,同樣的道理,代碼都差不多,我們直接 cv 大法,這里就給出重點要改的代碼。

          2、registercheck總代碼

          里面要重點改的代碼,一個是 sql 語句插入,另一個是時間格式轉(zhuǎn)換。

          tring sql = "insert into login(name, pass,time)VALUES(?,?,?)";

          SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 時間轉(zhuǎn)換,要不然就會是國際時間格式

          String data = formatter.format(new Date());// 記錄的是當前的時間

          ps.setString(3,data);

          3、logoutcheck總代碼

          3、logoutcheck總代碼

          里面要重點改的代碼,是 sql 語句刪除。

          String sql = "DELETE FROM login WHERE name =? and pass =?";

          4、amendcheck總代碼

          里面要重點改的代碼,是 sql 語句更新。

          String sql = "update login set pass='"+pass+"'";

          然后分別是各個總頁面的 success 與 fail 頁面來實現(xiàn)好,這里有一個小細節(jié),我們在作拋出異常,這里可以 out.println 來打印出信息來測試,可以輸出在網(wǎng)頁上,這樣可以方便知道那里有異常。

          
          catch (ClassNotFoundException e) {
                      e.printStackTrace();
                      out.println("1");
                      // response.sendRedirect("registerfail.jsp");
                  } catch (SQLException e) {
                      e.printStackTrace();
                      out.println("2");
                      // response.sendRedirect("registerfail.jsp");
          
          

          好了,點擊運行,完成總效果。



































          作者:一個名叫追的程序猿

          原文出處:https://blog.csdn.net/aasd23/article/details/124458396?spm=1001.2100.3001.7377&utm_medium=distribute.pc_feed_blog_category.none-task-blog-classify_tag-16-124458396-null-null.nonecase&depth_1-utm_source=distribute.pc_feed_blog_category.none-task-blog-classify_tag-16-124458396-null-null.nonecase

          方微信:動力節(jié)點java學院

          官方微博:動力節(jié)點

          一、什么是Jsp

          jsp是一種基于文本的程序,全名java server page,其特點是html和java程序共存。執(zhí)行時jsp會被運行容器編譯,編譯后的jsp跟servlet一樣,因此jsp是另一種形式的servlet。

          二、jsp頁面組成

          jsp 頁面包括以下內(nèi)容:

          • 靜態(tài)內(nèi)容

          • 指令

          • 表達式

          • 小腳本

          • 聲明

          • 注釋

          1.指令:

          • page指令: 通常位于jsp頁面的頂端,同一個頁面可以有多個page指令。

          • include指令:將一個外部文件嵌入到jsp文件中。

          • taglib指令 :使用標簽定義新的自定義標簽。

          1.1其中page指令語法:

          <%@ page 屬性=“屬性值”>

          屬性默認值
          languagejava
          import“”

          1.2 include 指令

          <%@ include file="url" %>

          1.3 動作

          • include動作

            <jsp:include page="url" flush="true"/>

            include 動作和include指令區(qū)別

            描述 | include指令 | include 動作

            --- | --- | ---

            語法 | < % @ include file=""/> | < jsp:include page="url" flush="true"/>

            發(fā)生時間 | 頁面轉(zhuǎn)換期間 | 請求期間

            包含內(nèi)容 | 文件實際內(nèi)容 | 頁面的輸出

            轉(zhuǎn)化servlet | 一個servlet | 2個servlet

            編譯時間 | 較慢 | 較快

            執(zhí)行時間 | 稍快 |較慢--每次資源必須被編譯

            forward動作

            <jsp:forward page="url"/>

            ==request.getRequestDispatcher("/url").forward(res,resp);

            param動作

            <jsp:param name="參數(shù)名" value="參數(shù)值"/>常常與<jsp:forward>一起使用

            例子:

            <jsp:forward page="user.jsp">

            <jsp:param name="email" value="1233@154.com"/></jsp:forward>

            2.jsp注釋

          • html注釋

            <!-- html注釋 -->//客戶端可見

          • jsp 注釋

            <%-- jsp注釋 --%>//客戶端不可見

          • jsp 腳本注釋 //客戶端不可見

          //單行注射

          /** 多行注釋*/

          3.jsp腳本

          在jsp頁面中執(zhí)行的java代碼,語法:

          <% java 代碼 %>

          4.jsp聲明

          在jsp頁面定義變量或者方法,語法

          <%! java 代碼 %>

          舉例:

          <%!

          String s="adele"; int add(int x,int y){ return x+y;

          }

          %>

          5.jsp表達式

          在jsp頁面執(zhí)行的表達式,語法:

          <% =表達式 %>// 表達式不以分號結(jié)尾

          舉例:

          <%!

          String s="adele";%><h2> hello,<%=s %> </h2>

          三、jsp生命周期

          CC36B22A-503E-4C29-98BB-3B58038C140E.png

          jspService()是用來處理客戶端請求的,對于每一個請求,服務器會創(chuàng)建一個新的線程來處理該請求。以多線程方式執(zhí)行大大降低對系統(tǒng)的資源需求,提高系統(tǒng)的并發(fā)量和縮短了響應時間,servlet是常駐在服務器內(nèi)存中。

          它同servlet 一樣,jsp 實例初始化和銷毀也會調(diào)用sevlet的init() 和destroy();

          另外jsp還有自己的初始化方法_jspInit();_jspDestroy();

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

          <%!

          public void _jspInit(){

          }public void _jspDestroy(){

          }

          %>

          四、javaben的使用

          動作元素:

          動作元素為請求處理階段提供信息。

          Paste_Image.png

          在jsp頁面使用javaben

          • 像普通的java類一樣,創(chuàng)建javabean;

          • 在jsp使用動作標簽來使用 javaben

          相關標簽如下:

          <jsp:useBwan id="" class="" scope="" />

          <jsp:setProperty name="javabean 是例" property="*"/>(跟表單關聯(lián))

          <jsp:setProperty name="javabean 是例" property="javaben 屬性名"/>(跟表單關聯(lián))

          <jsp:setProperty name="javabean 是例" property="javaben 屬性名" value=""/>(手動設置)

          <jsp:setProperty name="javabean 是例" property="javaben 屬性名" param="request對象參數(shù)"/>(跟request參數(shù)關聯(lián))

          <jsp:getProperty name="" property=""/>

          舉個例子:

          首先用戶 在login.jsp提交表單,然后用戶在dologin.jsp 根據(jù)動作標簽獲取參數(shù)。

          login.jsp

          <form name="loginForm" action="dologin.jsp?mypass=999999" method="post">

          <table>

          <tr>

          <td>用戶名:</td>

          <td><input type="text" name="username" value=""/></td>

          </tr>

          <tr>

          <td>密碼:</td>

          <td><input type="password" name="password" value=""/></td>

          </tr>

          <tr>

          <td colspan="2" align="center"><input type="submit" value="登錄"/></td>

          </tr>

          </table>

          </form>

          dologin.jsp

          <body>

          <jsp:useBean id="myUsers" class="com.po.Users" scope="page"/>

          <h1>setProperty動作元素</h1>

          <hr>

          <!--根據(jù)表單自動匹配所有的屬性 -->

          <%--

          <jsp:setProperty name="myUsers" property="*"/>

          --%>

          <!--根據(jù)表單匹配所有部分的屬性 -->

          <%--

          <jsp:setProperty name="myUsers" property="username"/>

          --%>

          <!--根表單無關,通過手工賦值給屬性 -->

          <%--

          <jsp:setProperty name="myUsers" property="username" value="lisi"/>

          <jsp:setProperty name="myUsers" property="password" value="888888"/>

          --%>

          <!--通過URL傳參數(shù)給屬性賦值 -->

          <jsp:setProperty name="myUsers" property="username"/>

          <jsp:setProperty name="myUsers" property="password" param="mypass"/>

          <!-- 使用傳統(tǒng)的表達式方式來獲取用戶名和密碼 -->

          <%--

          用戶名:<%=myUsers.getUsername() %><br>

          密碼:<%=myUsers.getPassword() %><br>

          --%> <!-- 使用getProperty方式來獲取用戶名和密碼 -->

          用戶名:<jsp:getProperty name="myUsers" property="username"/> <br>

          密碼:<jsp:getProperty name="myUsers" property="password"/><br>

          <br>

          <br>

          <a href="testScope.jsp">測試javabean的四個作用域范圍</a>

          <%

          request.getRequestDispatcher("testScope.jsp").forward(request, response); %>

          </body>

          javaben 四大作用域

          • page ,僅當前頁面有效

          • request ,通過httpRequest.getAttribute()獲取jvabean對象

          • session ,通過httpSession.getAttribute() 獲取javabean對象

          • application,通過application.getAttribute方法獲取javabean 對象。

          五、cookie

          1.概述:

          由于http協(xié)議的無狀態(tài),無法保存用戶的狀態(tài),所以需要用session和cookie.

          cookie 是web服務器保存在客戶端的一系列文本信息。它的作用時記錄一些用戶的行為,簡化登陸,但是容易泄露用戶信息。

          2.jsp創(chuàng)建和使用cookie

          • 創(chuàng)建cookie

          Cookie cookie=new Cookie(String ,Object);

          • 寫入cookie

          response.addCookie(cookie);

          • 讀取 cookie

          Cookie[] cookies=request.getCookies();

          3.cookie的常用方法

          • setMaxAge();

          • setValue();

          • getName();

          • getValue();

          • getMaxAge();

          舉個列子: 使用cookie記住用戶登陸的賬號密碼;

          登陸界面:

          <body>

          <h1>用戶登錄</h1>

          <hr>

          <%

          request.setCharacterEncoding("utf-8");

          String username="";

          String password = "";

          Cookie[] cookies = request.getCookies(); if(cookies!=null&&cookies.length>0)

          { for(Cookie c:cookies)

          { if(c.getName().equals("username"))

          {

          username = URLDecoder.decode(c.getValue(),"utf-8");

          } if(c.getName().equals("password"))

          {

          password = URLDecoder.decode(c.getValue(),"utf-8");

          }

          }

          } %>

          <form name="loginForm" action="dologin.jsp" method="post">

          <table>

          <tr>

          <td>用戶名:</td>

          <td><input type="text" name="username" value="<%=username %>"/></td>

          </tr>

          <tr>

          <td>密碼:</td>

          <td><input type="password" name="password" value="<%=password %>" /></td>

          </tr>

          <tr>

          <td colspan="2"><input type="checkbox" name="isUseCookie" checked="checked"/>十天內(nèi)記住我的登錄狀態(tài)</td>

          </tr>

          <tr>

          <td colspan="2" align="center"><input type="submit" value="登錄"/><input type="reset" value="取消"/></td>

          </tr>

          </table>

          </form>

          </body>

          處理登陸邏輯的jsp

          <body>

          <h1>登錄成功</h1>

          <hr>

          <br>

          <br>

          <br>

          <%

          request.setCharacterEncoding("utf-8"); //首先判斷用戶是否選擇了記住登錄狀態(tài)

          String[] isUseCookies = request.getParameterValues("isUseCookie"); if(isUseCookies!=null&&isUseCookies.length>0)

          { //把用戶名和密碼保存在Cookie對象里面

          String username = URLEncoder.encode(request.getParameter("username"),"utf-8"); //使用URLEncoder解決無法在Cookie當中保存中文字符串問題

          String password = URLEncoder.encode(request.getParameter("password"),"utf-8");

          Cookie usernameCookie = new Cookie("username",username);

          Cookie passwordCookie = new Cookie("password",password);

          usernameCookie.setMaxAge(864000);

          passwordCookie.setMaxAge(864000);//設置最大生存期限為10天

          response.addCookie(usernameCookie);

          response.addCookie(passwordCookie);

          } else

          {

          Cookie[] cookies = request.getCookies(); if(cookies!=null&&cookies.length>0)

          { for(Cookie c:cookies)

          { if(c.getName().equals("username")||c.getName().equals("password"))

          {

          c.setMaxAge(0); //設置Cookie失效

          response.addCookie(c); //重新保存。

          }

          }

          }

          } %>

          <a href="users.jsp" target="_blank">查看用戶信息</a>

          </body>

          使用cookie獲取用戶信息:

          <body>

          <h1>用戶信息</h1>

          <hr>

          <%

          request.setCharacterEncoding("utf-8");

          String username="";

          String password = "";

          Cookie[] cookies = request.getCookies(); if(cookies!=null&&cookies.length>0)

          { for(Cookie c:cookies)

          { if(c.getName().equals("username"))

          {

          username = URLDecoder.decode(c.getValue(),"utf-8");

          } if(c.getName().equals("password"))

          {

          password = URLDecoder.decode(c.getValue(),"utf-8");

          }

          }

          } %>

          <BR>

          <BR>

          <BR>

          用戶名:<%=username %><br>

          密碼:<%=password %><br>

          </body>

          3.cookie和 session的區(qū)別

          sessioncookie
          在服務端保存信息在客戶端保存信息
          保存的 object類型保存的是 string 類型
          隨會話結(jié)束,銷毀數(shù)據(jù)可以長期保存在客戶端中
          重要信息不重要信息

          如有問題請下方留言
          來源:互聯(lián)網(wǎng)

          主站蜘蛛池模板: 国产一区二区精品久久凹凸| 中文字幕精品一区影音先锋| 精品成人一区二区三区四区| 人妻aⅴ无码一区二区三区| 国产日韩精品一区二区在线观看| 日韩视频一区二区三区| 色综合一区二区三区| 亚洲欧美日韩一区二区三区| 久久国产精品免费一区| 国产综合精品一区二区| 91午夜精品亚洲一区二区三区 | 亚洲AV日韩精品一区二区三区| 波多野结衣一区二区| 日韩精品无码一区二区三区不卡| 人妻内射一区二区在线视频| 亚洲AV无码一区二区三区久久精品| 国产午夜福利精品一区二区三区| 夜夜爽一区二区三区精品| 国产未成女一区二区三区| 人妻少妇久久中文字幕一区二区| 国产在线精品一区二区三区直播 | 精品国产一区二区三区无码| 水蜜桃av无码一区二区| 国产日韩一区二区三区在线观看| 亚洲AⅤ无码一区二区三区在线| 国产精品盗摄一区二区在线| 国语对白一区二区三区| 亚洲国产AV一区二区三区四区| 99久久精品日本一区二区免费| 精品无码一区二区三区爱欲 | 中文字幕在线视频一区| 国产福利电影一区二区三区,日韩伦理电影在线福 | 无码av免费一区二区三区试看| 国产一区二区电影| 国语精品一区二区三区| 亚洲一区AV无码少妇电影☆| 亚洲一区二区三区在线观看精品中文| 三上悠亚国产精品一区| 国产免费一区二区三区不卡| 性无码免费一区二区三区在线| 久久久精品一区二区三区|