Web(二)

來源:互聯網
上載者:User

標籤:調用   submit   web.xml   character   dom   http   attr   main   解決   

一、資料的傳遞--轉寄和重新導向1、建立一個login.jsp頁面 <body>
     <%-- action:我們需要提交的地址  method:請求的方式 --%>
    <form action="doMain.jsp" method="get">
      <table>
        <tr>
           <td>使用者名稱:</td>
           <td><input  type="text" name="userName"></td>
        </tr>
        <tr>
           <td>密碼:</td>
           <td><input  type="password" name="password"></td>
        </tr>
        <tr>
           <td><input  type="submit" value="登入"></td>
        </tr>
      </table>
    </form>
  </body>2、建立對應的處理頁面doMain.jsp<body>
   <h1>登入成功</h1> 
<%-- login.jsp中的form表單 get方式請求亂碼 
    01.治標不治本    不推薦使用
    String  userName=new String(name.getBytes("iso-8859-1"),"utf-8");
    02.治本
    在伺服器中的conf檔案夾中找到server.xml檔案中的Connector節點中  新增屬性
   URIEncoding="UTF-8"
 --%>
    <%
      //根據login.jsp頁面 name的屬性值  擷取 value  
      //post亂碼 解決   是不是每個頁面都需要設定 請求編碼格式??? 後面 我們會用Filter
      request.setCharacterEncoding("utf-8");  //請求的編碼
      response.setCharacterEncoding("utf-8"); //響應的編碼
      String  name=request.getParameter("userName");  //擷取使用者名稱
      String  pwd=request.getParameter("password");  //擷取密碼
    %>
  
   <%-- 就是想把login頁面的值 傳遞給last.jsp   --%>
    <%
    out.print("使用者名稱:"+name);
    out.print("密碼:"+pwd);
    //把從login頁面拿到的值 儲存到了 request範圍中了
    request.setAttribute("userName", name);
    request.setAttribute("password", pwd);
     //轉寄到了last.jsp  攜帶了資料     last頁面能取得資料
    //request.getRequestDispatcher("last.jsp").forward(request, response);
     //重新導向last.jsp   資料都會丟失!   last頁面不能取得資料
     response.sendRedirect("last.jsp");
    %>
   
     <%-- get請求  --%>
    <a  href="last.jsp?userName=小黑黑2&password=123456">跳轉到最後一個介面</a>
  </body>
3、建立last.jsp看能不能擷取login.jsp的值 <body>
  <h1>last.jsp</h1>
  <%
  //在doMain中使用了轉寄後   能直接擷取  login頁面的值
  String  name=request.getParameter("userName");  //擷取使用者名稱
  String  pwd=request.getParameter("password");  //擷取密碼
  out.print("使用者名稱:"+name);
  out.print("密碼:"+pwd);
  %>
 
   <%
   //從request.getAttribute()取值
    request.setCharacterEncoding("utf-8");  //請求的編碼
    String  name1=(String)request.getAttribute("userName");//擷取使用者名稱
    String  pwd2=(String)request.getAttribute("password");  //擷取密碼
    out.print("使用者名稱:"+name1);
    out.print("密碼:"+pwd2);
    %>
  </body>
二、通過request擷取介面的多個值5、建立request1.jsp頁面<form action="request2.jsp" method="post">
      <input type="checkbox"  name="box" value="別玩手機">別玩手機"
      <input type="checkbox"  name="box" value="就玩手機">就玩手機"
      <input type="checkbox"  name="box" value="還玩手機">還玩手機"
      <input type="checkbox"  name="box" value="真玩手機">真玩手機"
      <input type="checkbox"  name="box" value="玩手機">玩手機">
      <button type="submit">提交</button>
  </form>6、建立request2.jsp頁面擷取request1.jsp的值<body>
  <%
  request.setCharacterEncoding("utf-8");
  //擷取選中複選框的值
 String [] boxs=request.getParameterValues("box");
  //首先進行判斷  必須先判斷非空
  if(boxs!=null&&boxs.length!=0){
      for(String box:boxs){
          out.print(box+"<br/>");
      }
  }else{
      //重新導向到request1介面
      response.sendRedirect("request1.jsp");
  }
  %>
 
      <h1>request對象常用的方法</h1>
     擷取http請求中使用的方法名稱 <%=request.getMethod() %><br/>
     擷取http請求中調用servlet的url部分 <%=request.getServletPath() %><br/>
     擷取http請求中MIME類型 <%=request.getContentType() %><br/>
     擷取請求中伺服器主機名稱 <%=request.getServerName() %><br/>
     擷取請求中伺服器的連接埠號碼名稱 <%=request.getServerPort() %><br/>
     擷取請求中伺服器的ip地址 <%=request.getRemoteAddr()%><br/>
     擷取請求中伺服器的ip地址 <%=request.getRemoteHost()%><br/>
     擷取請求中使用的協議 <%=request.getScheme() %><br/>
  </body>三、session  1、建立session1jsp<body>
    <form action="session2.jsp" method="post">
      <table><tr> <td>使用者名稱:</td>
           <td><input  type="text" name="userName"></td></tr>
        <tr> <td>密碼:</td>
           <td><input  type="password" name="password"></td> </tr>
        <tr><td><input  type="submit" value="登入"></td></tr> </table>
    </form> </body>2、建立session2.jsp<body>
  <%
    request.setCharacterEncoding("utf-8");
  //擷取session1.jsp的資訊
    String name= request.getParameter("userName");
    String pwd= request.getParameter("password");
    //把資訊儲存到session
    session.setAttribute("sessionName", name);
    session.setAttribute("sessionPwd", pwd);
    //把資訊儲存到request
    request.setAttribute("requestName", name);
    request.setAttribute("requestPwd", pwd);
    //重新導向  request範圍的值消失  session呢???
    response.sendRedirect("session3.jsp");
%>
<form action="session3.jsp" method="post">
      <table>
        <tr> <td>銀行卡密碼:</td>
           <td><input  type="text" name="card"></td></tr>
        <tr><td><input  type="submit" value="登入"></td></tr>
      </table></form></body>3、建立session3.jsp<body>
  <%
    request.setCharacterEncoding("utf-8");
     //擷取銀行卡密碼
    String card= request.getParameter("card");
    //清空session中指定的屬性
    session.removeAttribute("sessionPwd");  //刪除了密碼
     //從session中擷取資料
    String  name=(String)session.getAttribute("sessionName");
    String  pwd=(String)session.getAttribute("sessionPwd");
     //從request中擷取資料
    String  rName=(String)session.getAttribute("requestName");
    String  rPwd=(String)session.getAttribute("requestPwd");
    //讓session失效  以秒為單位
    session.setMaxInactiveInterval(3);  
  %>
<%-- 設定session的失效
  01.在tomcat中的conf檔案夾下面的web.xml   分鐘 計算的 預設30分鐘
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
   02.項目中的web.xml中也可以設定    自訂    項目中的為準  分鐘
   <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
   03.session.setMaxInactiveInterval(s) 當前的會話時間  不是整個web
 --%> 
  session取得的資料:<br/>
  您的使用者名稱是:<%=name %><br/>
  您的密碼是:<%=pwd %><br/>
  您的銀行卡密碼是:<%=card %><br/>
  <hr/>
    request取得的資料:<br/>
  您的使用者名稱是:<%=rName %><br/>
  您的密碼是:<%=rPwd %><br/></body>4、建立一個User類public class User {   
    private  String  name;
    private  String  pwd;   
    @Override
    public String toString() {
        return "User [name=" + name + ", pwd=" + pwd + "]"; }
    public User() {
        super(); }
    public User(String name, String pwd) {
        super();
        this.name = name;
        this.pwd = pwd; }
    public String getName() {
        return name; }
    public void setName(String name) {
        this.name = name; }
    public String getPwd() {
        return pwd;}
    public void setPwd(String pwd) {
        this.pwd = pwd;}
  
}
5、建立1.jsp<%@page import="cn.bdqn.bean.User"%>
  <body>
  <%
   //建立一個User對象
     User  user=new User("admin","123456");
  //把對象放進session範圍
     session.setAttribute("user", user);
  %>
    <a  href="session/2.jsp">跳轉第二個介面</a>
  </body>
6、建立2.jsp<body>
   <%
    //重新導向
     response.sendRedirect("3.jsp");
   %>
  </body>7、建立3.jsp<%@page import="cn.bdqn.bean.User"%>
  <body>
    <%
    User user=(User)session.getAttribute("user");
    out.print(user.getName());
    %>
     <%=session.getAttribute("user")%>
  </body>四、cookie1、1.jsp<body>
    <form action="cookie/2.jsp"  method="post">
      姓名:<input type="text" name="userName"/>
       <button type="submit">登入</button>
    </form> 
  <%
    //擷取第二個介面的資訊
     String msg=(String)request.getAttribute("msg");
   //有可能Null
   if(msg!=null){
       out.print(msg);
   }
  %>
  </body>2、2.jsp<%@page import="java.net.URLEncoder"%>
  <body>
     <%
     request.setCharacterEncoding("utf-8");  //解決post請求亂碼問題
       //擷取登陸的使用者名稱
     String name =request.getParameter("userName");
     //判斷使用者名稱是不是admin
     if(name.equalsIgnoreCase("admin")){
         //在返回到第一個介面時 給一個提示
         request.setAttribute("msg", "不允許使用admin登入!");
         //轉寄
         request.getRequestDispatcher("1.jsp").forward(request, response);
     }else{
         //成功! 跳轉到下個介面,並把使用者名稱傳遞下去
         //01.建立cookie     如果有中文  必須需要設定編碼格式
         Cookie cookie=new Cookie("user",URLEncoder.encode(name,"utf-8"));
         //02.如果想給cookie設定有效期間  必須在增加之前
         cookie.setMaxAge(20);
         //03.給響應新增cookie
         response.addCookie(cookie);
         //重新導向
         response.sendRedirect("3.jsp");   
     }
     %>
  </body>3、3.jsp<%@page import="java.net.URLDecoder"%>
  <body>
  <%
     //擷取第二個介面的cookie
    Cookie [] cookies= request.getCookies();
    String  name="";
    //遍曆數組  擷取我們想要的  user
    for(Cookie cookie:cookies){
        if(cookie.getName().equals("user")){  //找到了
            //進行中文的解碼
            name=URLDecoder.decode(cookie.getValue(), "utf-8");
            out.print("使用者名稱====》"+name);
        }
    }
  %>
  </body>

Web(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.