JSP ÈÝÆ÷µÄ³õʼ»¯ºÍÏú»Ù ServletContext£¨×ª£©
ServletContext ±» Servlet ³ÌÐòÓÃÀ´Óë Web ÈÝÆ÷ͨÐÅ¡£ÀýÈçдÈÕÖ¾£¬×ª·¢ÇëÇó¡£Ã¿Ò»¸ö Web
Ó¦ÓóÌÐòº¬ÓÐÒ»¸öContext£¬±»WebÓ¦ÓÃÄڵĸ÷¸ö³ÌÐò¹²Ïí¡£ÒòΪContext¿ÉÒÔÓÃÀ´±£´æ×ÊÔ´²¢ÇÒ¹²Ïí£¬ËùÒÔÎÒËùÖªµÀµÄ
ServletContext µÄ×î´óÓ¦ÓÃÊÇWeb»º´æ----°Ñ²»¾³£¸ü¸ÄµÄÄÚÈݶÁÈëÄڴ棬ËùÒÔ·þÎñÆ÷ÏìÓ¦ÇëÇóµÄʱºò¾Í²»ÐèÒª½øÐÐÂýËٵĴÅÅÌI/OÁË¡£
ServletContextListener ÊÇ ServletContext µÄ¼àÌýÕߣ¬Èç¹û ServletContext
·¢Éú±ä»¯£¬Èç·þÎñÆ÷Æô¶¯Ê± ServletContext ±»´´½¨£¬·þÎñÆ÷¹Ø±Õʱ ServletContext ½«Òª±»Ïú»Ù¡£
ÔÚJSP
ÎļþÖУ¬application ÊÇ ServletContext µÄʵÀý£¬ÓÉJSPÈÝÆ÷ĬÈÏ´´½¨¡£Servlet Öе÷ÓÃ
getServletContext()·½·¨µÃµ½ ServletContext µÄʵÀý¡£
ÎÒÃÇʹÓûº´æµÄ˼·´ó¸ÅÊÇ£º
·þ
ÎñÆ÷Æô¶¯Ê±£¬ServletContextListener µÄ
contextInitialized()·½·¨±»µ÷Óã¬ËùÒÔÔÚÀïÃæ´´½¨ºÃ»º´æ¡£¿ÉÒÔ´ÓÎļþÖлòÕß´ÓÊý¾Ý¿âÖжÁÈ¡È¡»º´æÄÚÈÝÉú³ÉÀ࣬ÓÃ
ervletContext.setAttribute()·½·¨½«»º´æÀà±£´æÔÚ ServletContext µÄʵÀýÖС£
³Ì
ÐòʹÓà ServletContext.getAttribute()¶ÁÈ¡»º´æ¡£Èç¹ûÊÇ JSP£¬Ê¹ÓÃa
pplication.getAttribute()¡£Èç¹ûÊÇ Servlet£¬Ê¹ÓÃ
getServletContext().getAttribute()¡£Èç¹û»º´æ·¢Éú±ä»¯(Èç·ÃÎʼÆÊý)£¬Äã¿ÉÒÔͬʱ¸ü¸Ä»º´æºÍÎļþ/Êý¾Ý¿â¡£»òÕßÄãµÈ
±ä»¯»ýÀÛµ½Ò»¶¨³ÌÐòÔÙ±£´æ£¬Ò²¿ÉÒÔÔÚÏÂÒ»²½±£´æ¡£
·þÎñÆ÷½«Òª¹Ø±Õ
ʱ£¬ServletContextListener µÄ
contextDestroyed()·½·¨±»µ÷Óã¬ËùÒÔÔÚÀïÃæ±£´æ»º´æµÄ¸ü¸Ä¡£½«¸ü¸ÄºóµÄ»º´æ±£´æ»ØÎļþ»òÕßÊý¾Ý¿â£¬¸üÐÂÔÀ´µÄÄÚÈÝ¡£
1
import
User;
//
my own class
2
import
DatabaseManager;
//
my own class
3
import
javax.servlet.ServletContext;
4
import
javax.servlet.ServletContextListener;
5
6
public
class
MyContextListener
7
8
implements
ServletContextListener {
9
private
ServletContext context
=
null
;
10
11
public
void
contextIniti
Ïà¹ØÎĵµ£º
µÚ1¸öÉÏ´«×é¼þcommons-fileupload
=============commons-fileupload ================
common-fileupload×é¼þÊÇapacheµÄÒ»¸ö¿ªÔ´ÏîÄ¿Ö®Ò»£¬¿ÉÒÔ´Óhttp://jakarta.apache.org/commons/fileupload/ÏÂÔØ¡£¸Ã×é¼þ¼òµ¥Ò×Ó㬿ÉʵÏÖÒ»´ÎÉÏ´«Ò»¸ö»ò¶à¸öÎļþ£¬²¢¿ÉÏÞÖÆÎļþ´óС¡£
-ÏÂÔغó½âѹzip°ü£¬½«commons-fileupload-1.1. ......
ÔÚÍøÉÏ¿´µ½ºÜ¶àÎÊÌâÊǹØÓÚÈçºÎѧϰjspµÄ£¬ÕýºÃÍøÉÏ¿´µ½Ò»Æª¹ØÓÚѧϰjspµÄÎÄÕ£¬¾ÍÕªÁËÒ»²¿·Ö·Òë¹ýÀ´£¬Ï£ÍûÄܶԴó¼ÒѧϰjspÓеãÖ¸µ¼¡£
¡¡¡¡Ò»¸öÆÕͨµÄ´íÎóÊÇ°ÑJSPµ±×÷¼ò»¯µÄ Java¡£Ëü²»ÊÇ£¬£¨ÊÂʵÉÏ, JSP ÊǼò»¯µÄ servlets ¡££©³ÌÐòԱͨ³£ÊÔ×ÅûÓÐѧϰҪÇóµÄÖ§³Ö¼¼ÇɶøÖ±½Óѧϰ JSP ¡£JSP ÊÇÒ»¸öÏνӼ¼Êõ£¬²¢Çҳɹ¦µØÁ¬½ ......
ÔÚJSPÖеÄÈç¹ûʹÓà "Ïà¶Ô·¾¶" ÔòÓпÉÄÜ»á³öÏÖÎÊÌâ.
ÒòΪ ÍøÒ³ÖÐµÄ "Ïà¶Ô·¾¶" , ËûÊÇÏà¶ÔÓÚ "URLÇëÇóµÄµØÖ·" ȥѰÕÒ×ÊÔ´.
ÉÏÃæÕâ¾ä»°ÊÇʲôÒâ˼ÄØ ?
¾Ù¸öÀý×Ó:
¼ÙÈçÎÒÃÇÓÐÒ»¸öÏîÄ¿: MyApp
ÔÚ¸ÃÏîÄ¿ÏÂ, ÓÐÒ»¸öjspÎļþ¼Ð
¸ÃÎļþ¼ÐÏ°üÀ¨:
login.jsp // ......
¾Å´ó¶ÔÏó£º
ÄÚÖöÔÏó£¨ÓÖ½ÐÒþº¬¶ÔÏó£¬ÓÐ9¸öÄÚÖöÔÏó£©:²»ÐèÒªÔ¤ÏÈÉùÃ÷¾Í¿ÉÒÔÔڽű¾´úÂëºÍ±í´ïʽÖÐËæÒâʹÓÃ
1-out:
javax.servlet.jsp.JspWriterÀàÐÍ£¬´ú±íÊä³öÁ÷µÄ¶ÔÏó¡£×÷ÓÃÓòΪpage£¨Ò³ÃæÖ´ÐÐÆÚ£©
request£ºjavax.servlet.ServletRequestµÄ×ÓÀàÐÍ£¬´Ë¶ÔÏó·â×°ÁËÓÉWEBä¯ÀÀÆ÷»òÆäËü¿Í»§¶ËÉú³ÉµØHTTP
ÇëÇóµÄϸ½Ú£¨²ÎÊý ......