新聞中心
幾個(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ì)象中的所有屬性名。
- packagemypack;
- importjavax.servlet.*;
- importjavax.servlet.http.*;
- importjava.io.*;
- importjava.util.*;
- publicclassCounterServletextendsHttpServlet
- {
- publicstaticfinalStringCONTENT_TYPE="text/html;charset=GB2312";
- publicvoidinit(ServletConfigconfig)throwsServletException{
- super.init(config);
- }
- publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
- doPost(request,response);
- }
- publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
- //獲得ServletContext的引用
- ServletContextcontext=getServletContext();
- //從ServletContext讀取count屬性
- Integercount=(Integer)context.getAttribute("count");
- //如果count屬性還沒有設(shè)置,那么創(chuàng)建count屬性,初始值為0
- //oneandaddittotheServletContext
- if(count==null){
- count=newInteger(0);
- context.setAttribute("count",newInteger(0));
- }
- response.setContentType(CONTENT_TYPE);
- PrintWriterout=response.getWriter();
- out.println("");
- out.println("
點(diǎn)擊計(jì)數(shù)"); - out.println("");
- //輸出當(dāng)前的count屬性值
- out.println("當(dāng)前計(jì)數(shù)是:"+count+"");
- out.println("");
- //創(chuàng)建新的count對(duì)象,其值增1
- count=newInteger(count.intValue()+1);
- //將新的count屬性存儲(chǔ)到ServletContent中
- context.setAttribute("count",count);
- }
- publicvoiddestroy(){
- }
- }
在web.xml中為CounterServlet類加上
CounterServlet mypack.CounterServlet CounterServlet /counter - 測(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中下
- <data-sourcekeydata-sourcekey="DATASOURCE"type="oracle.jdbc.pool.OracleDataSource">
- <set-propertypropertyset-propertyproperty="description"value="ExampleDataSourceConfiguration"/>
- <set-propertypropertyset-propertyproperty="driverClassName"value="oracle.jdbc.driver.OracleDriver"/>
- <set-propertypropertyset-propertyproperty="URL"value="jdbc:oracle:thin:@10.171.33.12:1521;DatabaseName=ITDBT"/>
- <set-propertypropertyset-propertyproperty="maxActive"value="10"/>
- <set-propertypropertyset-propertyproperty="minCount"value="1"/>
- <set-propertypropertyset-propertyproperty="user"value="vendorda1"/>
- <set-propertypropertyset-propertyproperty="password"value="vendorda1"/>
- <set-propertypropertyset-propertyproperty="autoCommit"value="true"/>
在這個(gè)里面配置好之后,如果想要建立一個(gè)數(shù)據(jù)庫的鏈接就可以這樣寫。
- DataSourcedataSource=(DataSource)servlet.getServletContext().getAttribute(Action.DATA_SOURCE_KEY);
- 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.
2.
以上這些過程的實(shí)現(xiàn)都是通過操作(page到application)四個(gè)范圍當(dāng)中的attribte屬性為實(shí)現(xiàn)的。
【編輯推薦】
- 淺談Java SE、Java EE、Java ME三者的區(qū)別
- Java虛擬機(jī)內(nèi)部構(gòu)成淺析
- 淺談Java編程語言中創(chuàng)建和使用日期
- 詳解Java的特點(diǎn)與優(yōu)勢(shì)
- 淺談為什么Java接口中不允許定義變量
分享標(biāo)題:總結(jié)JavaWeb中幾個(gè)函數(shù)的作用
瀏覽路徑:http://m.5511xx.com/article/ccscjho.html


咨詢
建站咨詢
