国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)

2019-11-18 16:17:20
字體:
供稿:網(wǎng)友
前言
熟悉java語法很久后,遲遲才開始學(xué)習(xí)jsp。而學(xué)習(xí)JSP時(shí),卻只學(xué)了基本的用法就去學(xué)Struts和Hibernate,以致對(duì)JSP掌握得很不夠。后來發(fā)現(xiàn)所學(xué)習(xí)的Struts框架實(shí)際上是“包裝”了的JSP。所以,我在學(xué)習(xí)框架的時(shí)候也回頭看看JSP。
以后應(yīng)該不會(huì)再去專門學(xué)習(xí)JSP了?,F(xiàn)在把一些JSP的相關(guān)知識(shí)總結(jié)下,記錄下來,以防來日忘了。
 
說明:以下所描述的環(huán)境是jdk1.5、tomcat5.5、 jsp2.0、 servlet2.4、JSTL1.1.2
一、基本配置
基本的重要的配置在web.xml 文件中。
 
1、Jsp屬性組
 
Word-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: windowtext 0.5pt solid">
<jsp-PRoperty-group>
       <url-pattern>/pages/*</url-pattern>
    <el-ignore>true</el-ignore>
    <page-encoding>UTF-8</page-encoding>
    <include-prelude>/include/header.jspf</include-prelude>
    <include-coda>/include/copyright.jspf</include-coda>
</jsp-property-group>
這個(gè)設(shè)置可以指定頁面編碼,頁頭頁腳等等。
設(shè)置 <page-encoding>UTF-8</page-encoding> 的好處是不用在每個(gè)頁面像這樣指定編碼:<%@page contentType="Html/text;charset=UTF-8" %>
而設(shè)置 <include-prelude>/include/header.jspf</include-prelude> 使得每個(gè)頁面都在頭部包含header.jspf文件(通常把對(duì)標(biāo)簽的包含放在這里)。
 
2、數(shù)據(jù)庫資源的引用
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)<resource-ref>
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)       <description>CourseDesign JDNI datasource</description>
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)       <res-ref-name>jdbc/test</res-ref-name>
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)       <res-type>javax.sql.DataSource</res-type>
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)       <res-auth>Container</res-auth>
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)</resource-ref>
前提是要在TOMCAT的中配置
<Context path="/Course" docBase="Course" debug="0" crosscontext="true" reloadable="true">
       <Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="root" passWord="123456" 
driverClassName="com.MySQL.jdbc.Driver" 
url="jdbc:mysql://localhost:3306/databaseName?useUnicode=true&amp;characterEncoding=UTF-8" />
</Context>
在程序中可以這樣獲取連接
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)public static Connection getConnection()
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖二)J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖三)       ...{
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖四)Connection conn=null;
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖四)              try   
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖五)J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖六)              ...{            
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖四)                     Context initContext = new InitialContext();
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖四)                     Context envContext = (Context)initContext.lookup("java:/comp/env");
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖四)                     DataSource ds = (DataSource)envContext.lookup("jdbc/test");
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖四)                     conn = ds.getConnection();
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖五)J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖六)              }catch(Exception e)...{            }
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖四)              return conn;
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖七)       }
3、過濾器
一般來說,字符編碼的處理,我們會(huì)寫一個(gè)過濾器。這個(gè)過濾器的JAVA類在TOMCAT的例子中有提供,可以按需來更改再拿來用。只要在配置文件中設(shè)置:
 
<filter-name>setCharacterEncoding</filter-name>
        <filter-class>powerwind.filter.SetCharacterEncodingFilter</filter-class>
        <init-param>
                <param-name>encoding</param-name>
                <param-value>UTF-8</param-value>
        </init-param>
</filter>
 
<filter-mapping>
        <filter-name>setCharacterEncoding</filter-name>
        <url-pattern>/pages/*</url-pattern>
</filter-mapping>
 
4、標(biāo)簽的URI
JSTL是個(gè)東西,里面提供了很好用的標(biāo)簽(Tag),但也不一定滿足我們的要求,就自己寫標(biāo)簽了。把 *.tld 文件直接放到WEB-INF下,在自己定義的tld文件中加上<uri>元素,如:<uri>http://powerwind/course</uri> 。
 
5、日志
只用過log4j這個(gè)日志包。首先是配置文件 log4j.properties (比較完整的配置,應(yīng)根據(jù)情況選擇):
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)log4j.rootLogger = DEBUG,INFO, A1, A2,A3
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)log4j.appender.A1 = org.apache.log4j.ConsoleAppender
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)log4j.appender.A1.layout = org.apache.log4j.PatternLayout
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)log4j.appender.A1.layout.ConversionPattern = %4p [%t] (%F:%L) - %m%n
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一) 
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)log4j.appender.A2 = org.apache.log4j.RollingFileAppender
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)log4j.appender.A2.File =../../log/test.log
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)log4j.appender.A2.MaxFileSize = 1KB
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)log4j.appender.A2.MaxBackupIndex = 3
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)log4j.appender.A2.layout = org.apache.log4j.PatternLayout
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)log4j.appender.A2.layout.ConversionPattern = %d{yyyy-MM-dd hh:mm:ss}:%p %t %c - %m%n
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一) 
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)log4j.appender.A3=org.apache.log4j.jdbc.JDBCAppender 
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)log4j.appender.A3.URL= jdbc:mysql://localhost:3306/log4jTest
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)log4j.appender.A3.driver= com.mysql.jdbc.Driver
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)log4j.appender.A3.user= root
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)log4j.appender.A3.password= 123456
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)log4j.appender.A3.layout = org.apache.log4j.PatternLayout
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)log4j.appender.A3.layout.ConversionPattern = INSERT INTO log4j (createDate, thread, level, class, message) values('%d', '%t', '%-5p', '%c', '%m')
QQread.com 推出Windows2003教程 win2003安裝介紹 win2003網(wǎng)絡(luò)優(yōu)化 win2003使用技巧 win2003系統(tǒng)故障 服務(wù)器配置 專家答疑


更多的請(qǐng)看:http://www.qqread.com/windows/2003/index.html

接著寫個(gè)Servlet來加載log4j:

package powerwind.servlet;
 
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
 
import javax.servlet.*;
import javax.servlet.http.*;
 
public class Log4jInit extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
        super.init(config);
        String prefix = getServletContext().getRealPath("/");
        String file = getInitParameter("log4j"); 
        System.out.println("init log4j...");
        if (file != null){
          PropertyConfigurator.configure(prefix+file);
        }else
        {
                 PropertyConfigurator.configure(prefix+"log4j.properties");
        }
}
}

然后同時(shí)要在web.xml下配置:

<servlet>
         <servlet-name>log4jInit</servlet-name>
         <servlet-class>powerwind.servlet.Log4jInit</servlet-class>
              <init-param>
                <param-name>log4j</param-name>
                <param-value>WEB-INF/classes/log4j.properties</param-value>
              </init-param>
         <load-on-startup>1</load-on-startup>
</servlet>


6、國際化

J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)#test_zh_CN.properties
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)#login page
J2EE學(xué)習(xí)經(jīng)驗(yàn):JSP學(xué)習(xí)總結(jié)(圖一)login.title=登錄頁面
小型的應(yīng)用中,我們并不常需要國際化。但是,如果網(wǎng)站要中文版和英文版的話,這個(gè)就不錯(cuò)啦。使用時(shí)很簡單,把資源test_zh_CN.properties文件放到classes目錄下,然后用JSTL的fmt標(biāo)簽調(diào)用。
<fmt:setLocale value="zh_CN"  scope=”session” />
<fmt:setBundle basename="test" scope=”session” var=”hehe” />
<fmt:message key="login.title" bundle=”${hehe}” scope=”session” />

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

二、極限與安全
資源放在WEB-INF下是安全的,因?yàn)檫@個(gè)目錄對(duì)于客戶端是不存在的。權(quán)限控制并不是僅僅這樣就可以了。如果只是簡單地判斷用戶是否登錄,可用一個(gè)過濾器檢查Session對(duì)象即可。若需要級(jí)別控制的話,就在Session中保存級(jí)別信息,然后加以判斷。
一般把權(quán)限的控制做成一個(gè)標(biāo)簽(tag)。如:
public int doEndTag() throws JspException {
              HttpSession session = pageContext.getSession();
              if ((session != null) && (session.getAttribute("user") != null)) {
                     String t = ((UserBean) session.getAttribute("user")).getType();
                     if (t == null  role == null) {
                            invalid();
                            return (SKIP_PAGE);
                     }
                     String[] roles = role.split(delimiter);
                     for (int i = 0; i < roles.length; i++) {
                            if (roles[i].equalsIgnoreCase(role))
                                   return (EVAL_PAGE);
                     }
              } else {
                     invalid();
                     return (SKIP_PAGE);
              }
              return (EVAL_PAGE);
       }


三、上傳與下載

上傳的話,一般使用已有的組件,如commons-fileupload 或者歐萊禮的cos (可能會(huì)遇到中文編碼的問題)。而下載,比較簡單,就自己寫了個(gè)Servlet。
public void handleRequest(HttpServletRequest request,
              HttpServletResponse response) throws IOException, ServletException {
              String name = request.getParameter("name");
              String type = request.getParameter("type");
              String dir = request.getParameter("dir");
              if (name == null  name.length() < 2  dir == null  dir.length() < 1  type == null  type.length() < 1) {
                     throw new ServletException("Sorry,error occured");
              }
              char ch = dir.charAt(dir.length() - 1);
              if (ch != '/'  ch != '/')
                     dir = dir + "/";
              ServletOutputStream os = null;
              BufferedInputStream bis = null;
              try {
                     File file = new File(dir + name);
                     if (!file.exists()  file.length() >= Integer.MAX_VALUE) {
                            logger.error("Invalid file or file to large,file: " + name);
                            throw new ServletException(
                                          "Invalid file or file to large,file: " + name);
                     }
                     response.setContentType("application/" + type);
                     response.addHeader("Content-Disposition", "attachment; filename="+ name);
                     response.setContentLength((int) file.length());
                     os = response.getOutputStream();
                     bis = new BufferedInputStream(new FileInputStream(file));
                     int size = -1;
                     while ((size = bis.read()) != -1)
                            os.write(size);
              } catch (IOException ioe) {
                     throw new ServletException(ioe.getMessage());
              } finally {
                     if (os != null)
                            os.close();
                     if (bis != null)
                            bis.close();
              }
       }


        以上只是個(gè)示例程序,靈活與方便的做法應(yīng)該是在Servlet初始化參數(shù)(<init-param>)設(shè)置下載文件所在目錄,當(dāng)然也可以在頁面中設(shè)置參數(shù)。甚至可以做成一個(gè)下載標(biāo)簽,方便使用。

(出處:http://m.survivalescaperooms.com)



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 清水县| 康乐县| 汉沽区| 新营市| 嘉定区| 阳高县| 镇远县| 公安县| 台州市| 桃源县| 永济市| 丹棱县| 格尔木市| 铜梁县| 吉木萨尔县| 华宁县| 柳林县| 新郑市| 衡阳县| 绥德县| 雷山县| 招远市| 天柱县| 周口市| 涡阳县| 大连市| 买车| 那坡县| 嵊州市| 哈尔滨市| 含山县| 台东市| 原平市| 霍山县| 诏安县| 广昌县| 晋江市| 高雄市| 犍为县| 洪湖市| 仪陇县|