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

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
JSP學習經(jīng)驗全面總結

JSP學習經(jīng)驗前言
  熟悉JAVA語法很久后,遲遲才開始學習JSP。而學習JSP時,卻只學了基本的用法就去學Struts和Hibernate,以致對JSP掌握得很不夠。后來發(fā)現(xiàn)所學習的Struts框架實際上是“包裝”了的JSP。所以,我在學習框架的時候也回頭看看JSP。   以后應該不會再去專門學習JSP了?,F(xiàn)在把一些JSP學習經(jīng)驗總結下,記錄下來,以防來日忘了。   說明:以下所描述的環(huán)境是jdk1.5、tomcat5.5、 jsp2.0、 servlet2.4、JSTL1.1.2  
一、基本配置  基本的重要的配置在web.xml 文件中。  
1、Jsp屬性組

成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供海曙企業(yè)網(wǎng)站建設,專注與成都網(wǎng)站設計、網(wǎng)站制作、HTML5建站、小程序制作等業(yè)務。10年已為海曙眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設公司優(yōu)惠進行中。

   
  
  
  1. /pages/*url-pattern>
  2. trueel-ignore>
  3. UTF-8page-encoding>
  4. /include/header.jspfinclude-prelude>
  5. /include/copyright.jspfinclude-coda>
  6. jsp-property-group>

這個設置可以指定頁面編碼,頁頭頁腳等等。

設置 UTF-8 的好處是不用在每個頁面像這樣指定編碼

而設置 /include/header.jspf 使得每個頁面都在頭部包含header.jspf文件(通常把對標簽的包含放在這里)。

2、數(shù)據(jù)庫資源的引用

 
 
 
  1. CourseDesignJDNIdatasourcedescription>
  2. jdbc/testres-ref-name>
  3. javax.sql.DataSourceres-type>
  4. Containerres-auth>
  5. resource-ref>

前提是要在TOMCAT的中配置

 
 
 
  1. <ContextpathContextpath="/Course"docBase="Course"debug=
    "0"crosscontext="true"reloadable="true">
  2. <ResourcenameResourcename="jdbc/test"auth=
    "Container"type="javax.sql.DataSource"
  3. maxActive="100"maxIdle="30"maxWait="10000"
  4. username="root"password="123456"
  5. driverClassName="com.mysql.jdbc.Driver"
  6. url="jdbc:mysql://localhost:3306/databaseName?
    useUnicode=true&characterEncoding=UTF-8"/>
  7. Context>

在程序中可以這樣獲取連接

 
 
 
  1. publicstaticConnectiongetConnection()
  2. ...{Connectionconn=null;
  3. try
  4. ...{
  5. ContextinitContext=newInitialContext();
  6. ContextenvContext=(Context)initContext.lookup"java:/comp/env");
  7. DataSourceds=(DataSource)envContext.lookup"jdbc/test");
  8. conn=ds.getConnection();
  9. }
  10. catch(Exceptione)...{
  11. }
  12. returnconn;
  13. }

3、過濾器

一般來說,字符編碼的處理,我們會寫一個過濾器。這個過濾器的JAVA類在TOMCAT的例子中有提供,可以按需來更改再拿來用。只要在配置文件中設置:

 
 
 
  1. setCharacterEncodingfilter-name>
  2. powerwind.filter.SetCharacterEncodingFilterfilter-class>
  3. encodingparam-name>
  4. UTF-8param-value>
  5. init-param>
  6. filter>
  7. setCharacterEncodingfilter-name>
  8. /pages/*url-pattern>
  9. filter-mapping>

4、標簽的URI

JSTL是個東西,里面提供了很好用的標簽(Tag),但也不一定滿足我們的要求,就自己寫標簽了。把 *.tld 文件直接放到WEB-INF下,在自己定義的tld文件中加上元素,如:http://powerwind/course 。

5、日志

只用過log4j這個日志包。首先是配置文件 log4j.properties (比較完整的配置,應根據(jù)情況選擇):

 
 
 
  1. log4j.rootLogger=DEBUG,INFO,A1,A2,A3
  2. log4j.appender.A1=org.apache.log4j.ConsoleAppender
  3. log4j.appender.A1.layout=org.apache.log4j.PatternLayout
  4. log4j.appender.A1.layout.ConversionPattern=%4p[%t](%F:%L)-%m%n
  5. log4j.appender.A2=org.apache.log4j.RollingFileAppender
  6. log4j.appender.A2.File=../../log/test.log
  7. log4j.appender.A2.MaxFileSize=1KB
  8. log4j.appender.A2.MaxBackupIndex=3
  9. log4j.appender.A2.layout=org.apache.log4j.PatternLayout
  10. log4j.appender.A2.layout.ConversionPattern=%d{yyyy-MM-ddhh:mm:ss}:%p%t%c-%m%n
  11. log4j.appender.A3=org.apache.log4j.jdbc.JDBCAppender
  12. log4j.appender.A3.URL=jdbc:mysql://localhost:3306/log4jTest
  13. log4j.appender.A3.driver=com.mysql.jdbc.Driver
  14. log4j.appender.A3.user=root
  15. log4j.appender.A3.password=123456
  16. log4j.appender.A3.layout=org.apache.log4j.PatternLayout
  17. log4j.appender.A3.layout.ConversionPattern=INSERTINTO
  18. log4j(createDate,thread,level,class,message)values('%d','%t','%-5p','%c','%m')

接著寫個Servlet來加載log4j:

 
 
 
  1. packagepowerwind.servlet;
  2. importorg.apache.log4j.Logger;
  3. importorg.apache.log4j.PropertyConfigurator;
  4. importjavax.servlet.*;
  5. importjavax.servlet.http.*;
  6. publicclassLog4jInitextendsHttpServlet{
  7. publicvoidinit(ServletConfigconfig)throwsServletException{
  8. super.init(config);
  9. Stringprefix=getServletContext().getRealPath("/");
  10. Stringfile=getInitParameter("log4j");
  11. System.out.println("initlog4j...");
  12. if(file!=null){
  13. PropertyConfigurator.configure(prefix+file);
  14. }else
  15. {
  16. PropertyConfigurator.configure(prefix+"log4j.properties");}
  17. }
  18. }

然后同時要在web.xml下配置:

 
 
 
  1. log4jInitservlet-name>
  2. powerwind.servlet.Log4jInitservlet-class>
  3. log4jparam-name>
  4. WEB-INF/classes/log4j.propertiesparam-value>
  5. init-param>
  6. 1load-on-startup>
  7. servlet>

小型的應用中,我們并不常需要國際化。但是,如果網(wǎng)站要中文版和英文版的話,這個就不錯啦。使用時很簡單,把資源test_zh_CN.properties文件放到classes目錄下,然后用JSTL的fmt標簽調用。

其中var和scope屬性不是必需的。三者結合,就可以實現(xiàn)國際化了。

 
 
 
  1. <fmt:setLocalevaluefmt:setLocalevalue="zh_CN"scope=”session”/>
  2. <fmt:setBundlebasenamefmt:setBundlebasename="test"scope=”session”var=”hehe”/>
  3. <fmt:messagekeyfmt:messagekey="login.title"bundle=”${hehe}”scope=”session”/>

二、極限與安全

資源放在WEB-INF下是安全的,因為這個目錄對于客戶端是不存在的。權限控制并不是僅僅這樣就可以了。如果只是簡單地判斷用戶是否登錄,可用一個過濾器檢查Session對象即可。若需要級別控制的話,就在Session中保存級別信息,然后加以判斷。

一般把權限的控制做成一個標簽(tag)。如:

 
 
 
  1. publicintdoEndTag()throwsJspException{
  2. HttpSessionsession=pageContext.getSession();
  3. if((session!=null)&&(session.getAttribute("user")!=null)){
  4. Stringt=((UserBean)session.getAttribute("user")).getType();
  5. if(t==null||role==null){
  6. invalid();
  7. return(SKIP_PAGE);
  8. }
  9. String[]roleroles=role.split(delimiter);
  10. for(inti=0;i;i++){
  11. if(roles[i].equalsIgnoreCase(role))
  12. return(EVAL_PAGE);
  13. }
  14. }else{
  15. invalid();
  16. return(SKIP_PAGE);
  17. }
  18. return(EVAL_PAGE);
  19. }

三、上傳與下載

上傳的話,一般使用已有的組件,如commons-fileupload 或者歐萊禮的cos (可能會遇到中文編碼的問題)。而下載,比較簡單,就自己寫了個Servlet。

 
 
 
  1. publicvoidhandleRequest(HttpServletRequestrequest,
  2. HttpServletResponseresponse)throwsIOException,ServletException{
  3. Stringname=request.getParameter("name");
  4. Stringtype=request.getParameter("type");
  5. Stringdir=request.getParameter("dir");
  6. if(name==null||name.length()<2||dir==null||dir.
    length()<1||type==null||type.length()<1){
  7. thrownewServletException("Sorry,erroroccured");
  8. }
  9. charch=dir.charAt(dir.length()-1);
  10. if(ch!='/'||ch!='\')
  11. dirdir=dir+"/";
  12. ServletOutputStreamos=null;
  13. BufferedInputStreambis=null;
  14. try{
  15. Filefile=newFile(dir+name);
  16. if(!file.exists()||file.length()>=Integer.MAX_VALUE){
  17. logger.error("Invalidfileorfiletolarge,file:"+name);
  18. thrownewServletException(
  19. "Invalidfileorfiletolarge,file:"+name);
  20. }
  21. response.setContentType("application/"+type);
  22. response.addHeader("Content-Disposition","attachment;filename="+name);
  23. response.setContentLength((int)file.length());
  24. os=response.getOutputStream();
  25. bis=newBufferedInputStream(newFileInputStream(file));
  26. intsize=-1;
  27. while((size=bis.read())!=-1)
  28. os.write(size);
  29. }catch(IOExceptionioe){
  30. thrownewServletException(ioe.getMessage());
  31. }finally{
  32. if(os!=null)
  33. os.close();
  34. if(bis!=null)
  35. bis.close();
  36. }
  37. }

以上只是個示例程序紀錄在JSP學習經(jīng)驗中,靈活與方便的做法應該是在Servlet初始化參數(shù)()設置下載文件所在目錄,當然也可以在頁面中設置參數(shù)。甚至可以做成一個下載標簽,方便使用。


分享題目:JSP學習經(jīng)驗全面總結
鏈接分享:http://www.5511xx.com/article/dphdhoi.html