自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

JSP學習經(jīng)驗全面總結(jié)

開發(fā) 后端
本文全面總結(jié)JSP學習經(jīng)驗,熟悉JAVA語法很久后,遲遲才開始學習JSP,學習JSP時,卻只學了基本的用法就去學Struts和Hibernate,以致對JSP掌握得很不夠。
JSP學習經(jīng)驗前言
 
熟悉JAVA語法很久后,遲遲才開始學習JSP。而學習JSP時,卻只學了基本的用法就去學Struts和Hibernate,以致對JSP掌握得很不夠。后來發(fā)現(xiàn)所學習的Struts框架實際上是“包裝”了的JSP。所以,我在學習框架的時候也回頭看看JSP。
 
以后應該不會再去專門學習JSP了?,F(xiàn)在把一些JSP學習經(jīng)驗總結(jié)下,記錄下來,以防來日忘了。
 
說明:以下所描述的環(huán)境是jdk1.5、tomcat5.5、 jsp2.0、 servlet2.4、JSTL1.1.2
 
一、基本配置
 
基本的重要的配置在web.xml 文件中。
 
1、Jsp屬性組
  1. <jsp-property-group> 
  2. <url-pattern>/pages/*url-pattern> 
  3. <el-ignore>trueel-ignore> 
  4. <page-encoding>UTF-8page-encoding> 
  5. <include-prelude>/include/header.jspfinclude-prelude> 
  6. <include-coda>/include/copyright.jspfinclude-coda> 
  7. jsp-property-group> 

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

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

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

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

  1. <resource-ref> 
  2. <description>CourseDesignJDNIdatasourcedescription> 
  3. <res-ref-name>jdbc/testres-ref-name> 
  4. <res-type>javax.sql.DataSourceres-type> 
  5. <res-auth>Containerres-auth> 
  6. 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;  

3、過濾器

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

  1. <filter-name>setCharacterEncodingfilter-name> 
  2. <filter-class>powerwind.filter.SetCharacterEncodingFilterfilter-class> 
  3. <init-param> 
  4. <param-name>encodingparam-name> 
  5. <param-value>UTF-8param-value> 
  6. init-param> 
  7. filter> 
  8. <filter-mapping> 
  9. <filter-name>setCharacterEncodingfilter-name> 
  10. <url-pattern>/pages/*url-pattern> 
  11. 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.  
  6. log4j.appender.A2=org.apache.log4j.RollingFileAppender  
  7. log4j.appender.A2.File=../../log/test.log  
  8. log4j.appender.A2.MaxFileSize=1KB 
  9. log4j.appender.A2.MaxBackupIndex=3 
  10. log4j.appender.A2.layout=org.apache.log4j.PatternLayout  
  11. log4j.appender.A2.layout.ConversionPattern=%d{yyyy-MM-ddhh:mm:ss}:%p%t%c-%m%n  
  12.  
  13. log4j.appender.A3=org.apache.log4j.jdbc.JDBCAppender  
  14. log4j.appender.A3.URL=jdbc:mysql://localhost:3306/log4jTest  
  15. log4j.appender.A3.driver=com.mysql.jdbc.Driver  
  16. log4j.appender.A3.user=root 
  17. log4j.appender.A3.password=123456 
  18. log4j.appender.A3.layout=org.apache.log4j.PatternLayout  
  19. log4j.appender.A3.layout.ConversionPattern=INSERTINTO 
  20. 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.  
  5. importjavax.servlet.*;  
  6. importjavax.servlet.http.*;  
  7.  
  8. publicclassLog4jInitextendsHttpServlet{  
  9. publicvoidinit(ServletConfigconfig)throwsServletException{  
  10. super.init(config);  
  11. Stringprefix=getServletContext().getRealPath("/");  
  12. Stringfile=getInitParameter("log4j");  
  13. System.out.println("initlog4j...");  
  14. if(file!=null){  
  15. PropertyConfigurator.configure(prefix+file);  
  16. }else  
  17. {  
  18. PropertyConfigurator.configure(prefix+"log4j.properties");}  
  19. }  

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

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

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

其中var和scope屬性不是必需的。三者結(jié)合,就可以實現(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下是安全的,因為這個目錄對于客戶端是不存在的。權(quán)限控制并不是僅僅這樣就可以了。如果只是簡單地判斷用戶是否登錄,可用一個過濾器檢查Session對象即可。若需要級別控制的話,就在Session中保存級別信息,然后加以判斷。

一般把權(quán)限的控制做成一個標簽(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<roles.length;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);  

三、上傳與下載

上傳的話,一般使用已有的組件,如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. }  

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

【編輯推薦】

  1. 全面介紹JSP標準標記庫JSTL
  2. JSP開發(fā)技術(shù)應用詳解
  3. 在實戰(zhàn)中成長:JSP開發(fā)之路
  4. Servlet和JSP技術(shù)特性
  5. JSP標簽庫概念及特點介紹
責任編輯:佚名 來源: Csdn
相關推薦

2011-07-08 13:15:52

JSP

2009-08-20 17:35:47

Servlet和JSP

2009-07-01 11:44:32

JSP學習教程

2009-09-16 17:13:54

學習Linq

2009-08-10 16:25:30

JSP SQL Ser

2011-07-21 13:40:17

java

2009-08-13 18:13:27

C#學習經(jīng)驗

2010-06-13 13:44:07

UML學習筆記

2013-12-18 15:54:21

2009-07-02 11:49:44

JSP學習步驟

2009-08-11 14:20:41

C# .NET學習經(jīng)驗

2010-06-02 09:06:26

SVN學習

2009-09-01 13:10:39

C#讀取Word

2010-01-05 16:46:14

學習.NET Fram

2009-03-21 19:21:22

2010-09-28 16:05:36

J2ME技術(shù)J2MEWTK

2015-06-23 15:07:53

2022-03-14 10:20:15

人工智能學習高效

2009-09-04 16:33:28

CCNA學習方法

2011-01-12 17:27:53

點贊
收藏

51CTO技術(shù)棧公眾號