日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
總結(jié)JavaWeb中幾個(gè)函數(shù)的作用

幾個(gè)Java Web函數(shù)的作用

創(chuàng)新互聯(lián)專注于茂南企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站制作。茂南網(wǎng)站建設(shè)公司,為茂南等地區(qū)提供建站服務(wù)。全流程按需開發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)

一,ServletContext():(zz)

Servlet容器在啟動(dòng)時(shí)會(huì)加載Web應(yīng)用,并為每個(gè)Java Web應(yīng)用創(chuàng)建唯一的ServletContext對(duì)象??梢园裇ervletContext看成是一個(gè)Web應(yīng)用的服務(wù)器端組件的共享內(nèi)存。在ServletContext中可以存放共享數(shù)據(jù),它提供了4個(gè)讀取或設(shè)置共享數(shù)據(jù)的方法。

1)setAttribute(Stringname,Objectobject):把一個(gè)對(duì)象和一個(gè)屬性名綁定,將這個(gè)對(duì)象存儲(chǔ)在ServletContext中;

2)getAttribute(Stringname):根據(jù)指定的屬性名返回所綁定的對(duì)象;

3)removeAttribute(Stringname):根據(jù)給定的屬性名從ServletContext中刪除相應(yīng)的操作;

4)getAttributeNames():返回Enumeration對(duì)象,它包含了存儲(chǔ)在ServletContext對(duì)象中的所有屬性名。

 
 
 
  1. packagemypack;  
  2. importjavax.servlet.*;  
  3. importjavax.servlet.http.*;  
  4. importjava.io.*;  
  5. importjava.util.*;  
  6.  
  7. publicclassCounterServletextendsHttpServlet  
  8. {  
  9. publicstaticfinalStringCONTENT_TYPE="text/html;charset=GB2312";  
  10.  
  11. publicvoidinit(ServletConfigconfig)throwsServletException{  
  12. super.init(config);  
  13. }  
  14.  
  15. publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{  
  16. doPost(request,response);  
  17. }  
  18.  
  19. publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{  
  20. //獲得ServletContext的引用  
  21. ServletContextcontext=getServletContext();  
  22. //從ServletContext讀取count屬性  
  23. Integercount=(Integer)context.getAttribute("count");  
  24.  
  25. //如果count屬性還沒有設(shè)置,那么創(chuàng)建count屬性,初始值為0  
  26. //oneandaddittotheServletContext  
  27. if(count==null){  
  28. count=newInteger(0);  
  29. context.setAttribute("count",newInteger(0));  
  30. }  
  31.  
  32. response.setContentType(CONTENT_TYPE);  
  33. PrintWriterout=response.getWriter();  
  34. out.println("");  
  35. out.println("</font></strong>點(diǎn)擊計(jì)數(shù)<strong><font>");  
  36. out.println("");  
  37. //輸出當(dāng)前的count屬性值  
  38. out.println("

    當(dāng)前計(jì)數(shù)是:"+count+"

    ");  
  39. out.println("");  
  40.  
  41. //創(chuàng)建新的count對(duì)象,其值增1  
  42. count=newInteger(count.intValue()+1);  
  43. //將新的count屬性存儲(chǔ)到ServletContent中  
  44. context.setAttribute("count",count);  
  45. }  
  46. publicvoiddestroy(){  
  47. }  

在web.xml中為CounterServlet類加上元素

 
 
 
  1.  
  2. CounterServlet 
  3. mypack.CounterServlet 
  4.  
  5.  
  6. CounterServlet 
  7. /counter 
  8.  
  9.  
  10. 測(cè)試通過。 

1)刷新頁面,count加1;

2)關(guān)閉瀏覽器,再打開新瀏覽器還是在原來基礎(chǔ)上加1(即便由IE換成Opera);

3)如果重啟Tomcat服務(wù)器,再訪問CounterServlet,則count又初始化為0;

4)復(fù)制helloapp應(yīng)用,改為helloapp1,再發(fā)布helloapp1,通過不同的瀏覽器窗口分別訪問helloapp及helloapp1中的CounterServlet,發(fā)現(xiàn)這兩個(gè)Web應(yīng)用擁有各自獨(dú)立的count屬性。

即ServletContext對(duì)象只在Web應(yīng)用被關(guān)閉時(shí)才被銷毀;不同的Web應(yīng)用,ServletContext各自獨(dú)立存在。

二,以前不知道在struts-config.xml中還可以寫數(shù)據(jù)庫的配置,現(xiàn)在知道了,是可以在struts-config.xml中下標(biāo)簽來進(jìn)行數(shù)據(jù)庫的配置的。

 
 
 
  1.  
  2.  
  3. <data-sourcekeydata-sourcekey="DATASOURCE"type="oracle.jdbc.pool.OracleDataSource"> 
  4.  
  5. <set-propertypropertyset-propertyproperty="description"value="ExampleDataSourceConfiguration"/> 
  6.  
  7.  
  8. <set-propertypropertyset-propertyproperty="driverClassName"value="oracle.jdbc.driver.OracleDriver"/> 
  9.  
  10.  
  11. <set-propertypropertyset-propertyproperty="URL"value="jdbc:oracle:thin:@10.171.33.12:1521;DatabaseName=ITDBT"/> 
  12.  
  13.  
  14. <set-propertypropertyset-propertyproperty="maxActive"value="10"/> 
  15.  
  16.  
  17. <set-propertypropertyset-propertyproperty="minCount"value="1"/> 
  18.  
  19.  
  20. <set-propertypropertyset-propertyproperty="user"value="vendorda1"/> 
  21.  
  22.  
  23. <set-propertypropertyset-propertyproperty="password"value="vendorda1"/> 
  24.  
  25.  
  26. <set-propertypropertyset-propertyproperty="autoCommit"value="true"/> 
  27.  
  28.  
  29.  

在這個(gè)里面配置好之后,如果想要建立一個(gè)數(shù)據(jù)庫的鏈接就可以這樣寫。

 
 
 
  1. DataSourcedataSource=(DataSource)servlet.getServletContext().getAttribute(Action.DATA_SOURCE_KEY);  
  2. Connectionconnection=dataSource.getConnection(); 

三,ActionMessage的作用(zz)

ActionMessage、ActionMessages、saveMessages()、saveErrors()、之間的關(guān)系及用法

ActionMessage功能是:用來產(chǎn)生STRUTS消息的包括正常消息和錯(cuò)誤消息。注(原有ActionError方法來產(chǎn)生錯(cuò)誤消息,但已廢棄,但仍可使用。)例:ActionMessage(key,object);其中key為在appication.properties中指的key值。object為消息中要傳入的參數(shù),這里實(shí)現(xiàn)了動(dòng)態(tài)傳入。如果要傳多個(gè)參數(shù)時(shí),object處可以改為數(shù)組。

ActionMessages功能:是作為容器(map)來存ActionMessage的。例::ActionMessagesams=newActionMessages().ams.add("id",ActionMessage).

saveErrors功能:是用來存儲(chǔ)ActionMessages,例:saveErrors(ActionMessages,scope)這里有個(gè)隱性的error_key作為KEY與ActionMessages對(duì)應(yīng)。scope值為:application,session,request,page.默認(rèn)為page范圍。

saveMessages功能:是用來存儲(chǔ)ActionMessages,例:saveMessages(ActionMessages,scope)這里有個(gè)隱性的message_key作為KEY與ActionMessages對(duì)應(yīng)。scope值為:application,session,request,page.默認(rèn)為page范圍。

取出ActionMessage消息STRUTS提供了兩種標(biāo)簽:

1.意思是從page范圍(范圍由小到大從page到application)開始以error_key為key尋找ActionMessages,要取具體的ActionMessage可以用

2.意思是迭代找出所有ActionMessage賦值給var,然后通過bean標(biāo)簽輸出。要取具體的ActionMessage可以用,這里html:message標(biāo)簽有個(gè)默認(rèn)message屬性,它的默認(rèn)值為false,代表以error_key為key取出ActionMessages.設(shè)為true時(shí)代表以message_key為key取出ActionMessages.它與的主要區(qū)別是,它可以取正常消息或錯(cuò)誤消息(即以error_key或message_key為KEY),通過設(shè)置message屬性true或false來實(shí)現(xiàn)。

以上這些過程的實(shí)現(xiàn)都是通過操作(page到application)四個(gè)范圍當(dāng)中的attribte屬性為實(shí)現(xiàn)的。

【編輯推薦】

  1. 淺談Java SE、Java EE、Java ME三者的區(qū)別
  2. Java虛擬機(jī)內(nèi)部構(gòu)成淺析
  3. 淺談Java編程語言中創(chuàng)建和使用日期
  4. 詳解Java的特點(diǎn)與優(yōu)勢(shì)
  5. 淺談為什么Java接口中不允許定義變量

分享標(biāo)題:總結(jié)JavaWeb中幾個(gè)函數(shù)的作用
瀏覽路徑:http://m.5511xx.com/article/ccscjho.html