Java中九大內置對象


jsp九大內置對象包括request response session application out page config exception pageContent

其中 request  response  out page config exception pageContent對象的有效范圍是當前頁面的應用  session 有效范圍是當前會話(當前客戶端的所有頁面) application 有效范圍是整個應用程序,只要服務器不關閉對象就有效

====================================================================
request
====================================================================
request.getParameter();獲得用戶提交的表單信息
request.setCharacterEncoding("UTF-8");設置請求編碼,防止亂碼
request.setAttribute("Unmae", new Object());將數據保存到request范圍內的變量中
request.forward(String Url);轉發
request.getRequestURL();獲得當前頁的IE地址
request.getHeader("resref");獲得請求也的IE地址
request.getRemoteAddr();獲得用戶的IP地址
====================================================================
cookie
====================================================================
cookie.getCookies()獲得所有cookie對象的集合
cookie.getName()獲得指定名稱的cookie
cookie.getValue()獲得cookie對象的值
URLEncoder.encode();將需要保存到cookie中的數據進行編碼
URLEncoder.decode();讀取cookie信息時將信息解碼
====================================================================
response
====================================================================
response.addCookie()將一個cookie對象發送到客戶端
response.sendRedirect(String path); 重定向
====================================================================
application
====================================================================
application.setAttribute(key,value);給application添加屬性值
application.getAttribute(key,value);獲取指定的值
====================================================================
session
====================================================================
session.setMaxInactiveInterval(int num);設置session對象的有效活動時間
session.isNew();判斷是否為新用戶  返回Boolean
session.setAttribute();
session.getAttribute();
session.invalidate();銷毀當前session
====================================================================
                             案例
====================================================================
1:防止表單在網站外部提交  使用request

String address=request.getRequestURL().toString();//獲得當前的IE地址
String addresstwo=request.getHeader("referer");//獲得請求地址
  String pathadd=null;//當前服務器主機
  String pathaddtwo=null;//訪問服務器主機
  //獲得訪問主機名稱
  try {
   if(address!=null&&address!=""){
    URL url=new URL(address);
    pathadd=url.getHost();
   }
   if(addresstwo!=null&&addresstwo!=""){
    URL url1=new URL(addresstwo);
    pathaddtwo=url1.getHost();
   }
  if(pathadd.equals(pathaddtwo)){
   System.err.println("可以訪問");
  }else{
   System.err.println("不在同一站內訪問");
  }
   
   
  } catch (MalformedURLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

2:網站計數器  application

synchronized  java關鍵字  使用中一個線程未完成鎖定下一個線程
 
                int i=0;
  synchronized (application) {
  
   if(application.getAttribute("times")==null){//服務器啟動后的第一位訪問者
    i=1;
   }else{
    i=Integer.parseInt(application.getAttribute("times"));
    i++;//訪問次數加一
   }
   application.setAttribute("times",Integer.toString(i)); //將訪問次數存入到application中
  }
====================================================================

 


免責聲明!

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



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