JSP ±Ê¼Ç
JSP£¨Java Server Page£©
JSPÊÇ·þÎñÆ÷¶ËÔËÐеÄÒ³Ã棬JSP±¾¾ÍÊÇÒ»¸öÎĵµ£¬Ëû²»½ö¿ÉÒÔ°üº¬¾²Ì¬µÄHTML´úÂ룬Ҳ¿ÉÒÔ°üº¬¶¯Ì¬µÄJAVA´úÂ룬·þÎñÆ÷ÈÝÆ÷¿ÉÒÔ½«JSPת»»³ÉServlet·¢²¼£¬²¢½ÓÊÜÇëÇó¡£
JspÎļþÖ»Ðè·ÅÔÚÓ¦ÓÃÎļþϾͿÉÒÔÁË¡£
JSPÀý×Ó£º
<html>
<head><title>time</title></head>
<body>
<h1><%=new
java.util.Date()%></h1>
</body>
</html>
JSPÔËÐйý³Ì
1£¬JSPÎļþÏÈÒª·Òë³ÉJavaÎļþ£¨Servlet£©£¬ÔÚtomcatÖзÒëºóµÄjavaÎļþÔÚtomcatϵÄwork\Catalina\localhostÖÐÏàÓ¦Ãû×ÖµÄÓ¦ÓÃĿ¼Àï¡£
2£¬±àÒëJavaÎļþ
3£¬ÔËÐÐ.classÎļþ
TomcatµÄÈ«¾ÖÅäÖÃÎļþ
ÔÚTomcatϵÄconfĿ¼ÖеÄweb.xmlÊÇ·þÎñÆ÷È«¾ÖµÄÅäÖÃÎļþ£¬ÆäÖÐÒªÏë¹Ø±ÕÁбíÏÔʾ½ÐÒª°Ñ
<init-param>
<param-name>listings</param-name>
<param-value>false</param-value>
</init-param>
TomcatµÄ¹ÜÀíÔ±ÉèÖÃ
ÔÚtomcatϵÄconfĿ¼ÖеÄtomcat-use.xmlÎļþÖпÉÒÔÅäÖÃtomcatµÄÓû§½ÇÉ«
<tomcat-users>
<role rolename="manager" description=""/>
<role rolename="admin"/><!--É趨½ÇÉ«µÄÃû×Ö£¬adminÊǹÜÀíÔ±£¬managerÊÇÏîÄ¿¹ÜÀí-->
<user username="manager" password="123"
roles="manager"/><!--É趨Óû§ÃûÃÜÂ룬ºÍ½ÇÉ«-->
<user username="admin" password="123"
roles="admin"/>
</tomcat-users>
JSPµÄÌصã
1£¬JSP²»ÐèÒª±àÒë
2£¬JSP²»ÐèÒª½øÐÐÅäÖÃ
3£¬JSP±¾ÖÊÉϾÍÊÇÒ»¸öServlet
4£¬JSPÊÇÒÔ¾²Ì¬Ò³ÃæΪÖ÷µÄ¡£
JSPµÄÓï·¨¸ñʽ
<%=...%>±í´ïʽ£¬Ð´±í´ïʽʱ²»Òª¼ÓÉϷֺţ¬±í´ïʽ£¬·Òë³ÉJava´úÂëÒ²¾ÍÊÇÖ±½ÓʹÓÃÁ÷Êä³ö£¬Ò²¾Í²»ÄܼӷֺÅÁË¡£
<%! int a=0; %>ÉùÃ÷£¬Ê¹ÓÃÉùÃ÷µÄ±äÁ¿£¬Ëù´´½¨µÄ±äÁ¿ÊÇÔÚservice·½·¨ÌåÍ⣬Ҳ¾ÍÊÇ·Òë³ÉʵÀý±äÁ¿£¬ÉùÃ÷Ò²¿ÉÒÔÉùÃ÷ÄÚ²¿Àà»òÕßÊÇ·½·¨µÄÉùÃ÷¡£
<% if(a==0){ a=3;}%>´úÂëƬ¶Ï£¬´úÂë¶Î»áÔÑù·Òë¡£ÔÚ´úÂë¶ÎÖÐÉùÃ÷µÄ±äÁ¿£¬¾Í»á±»·ÒëµÄserviceµÄ·½·¨ÌåÖУ¬Ò²¾ÍÊDZ»·Òë³É¾Ö²¿±äÁ¿¡£
Ç°ÈýÖÖJSPµÄÓï·¨Ò²½Ð×öJSP´úÂ룬ÒÔÉϵıíʾ·½·¨£¬¶¼ÓÐÓëÖ®Ïà¶ÔÓ¦µÄxml±í´ïÐÎʽ£¬ÒòΪ¿É¶ÁÐԽϲËùÒÔ½ÏÉÙʹÓá£
<
Ïà¹ØÎĵµ£º
JSPÖеÄÌøת£º
(1). forward()·½·¨
ʹÓõ½javax.servlet.RequestDispatcherÀà
RequestDispatcher rd = getServletContext().getRequestDispatcher("url");
rd.forward(requestVar,requestVar); //´«µÝservletµÄÈë¿Ú²ÎÊý
/*forwardº ......
JSPÄÚÖöÔÏó(9¸ö³£ÓõÄÄÚÖöÔÏó)
1.request¶ÔÏó
¿Í»§¶ËµÄÇëÇóÐÅÏ¢±»·â×°ÔÚrequest¶ÔÏóÖУ¬Í¨¹ýËü²ÅÄÜÁ˽⵽¿Í»§µÄÐèÇó£¬È»ºó×ö³öÏìÓ¦¡£ËüÊÇHttpServletRequestÀàµÄʵÀý¡£
ÐòºÅ ·½ ·¨ ˵ Ã÷
1 object getAttribute(String name) ·µ»ØÖ¸¶¨ÊôÐÔµÄÊôÐÔÖµ
2 Enumeration getAttributeNames() ·µ»ØËùÓпÉÓÃÊôÐÔÃû ......
ServletÈý¸öÒªËØ:
1.±ØÐë¼Ì³Ð×ÔHttpServlet
2.±ØÐëʵÏÖdoGet()»òÕßdoPost()
3.±ØÐëÔÚweb.xmlÖÐÅäÖÃServlet
<servlet>
<servlet-name> </servlet-name>
<servlet-class> </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> </servlet-nam ......
×î»ù±¾µÄÂÒÂëÎÊÌâ
Õâ¸öÂÒÂëÎÊÌâÊÇ×î¼òµ¥µÄÂÒÂëÎÊÌâ¡£Ò»°ãлá³öÏÖ¡£¾ÍÊÇÒ³Ãæ±àÂë²»Ò»Öµ¼ÖµÄÂÒÂë¡£
Html´úÂ룺
£¼%@ page language="java" pageEncoding="UTF-8"%£¾
£¼%@ page contentType="text/html;charset=iso8859-1"%£¾
£¼html£¾
£¼head£¾
£¼title ......
1.request¶ÔÏó
¸Ã¶ÔÏó·â×°ÁËÓû§Ìá½»µÄÐÅÏ¢£¬Í¨¹ýµ÷ÓøöÔÏóµÄÏìÓ¦µÄ·½·¨¿ÉÒÔ»ñÈ¡Óû§Ìá½»µÄÐÅÏ¢¡£
µ±request¶ÔÏó»ñÈ¡Óû§Ìá½»µÄºº×Ö×Ö·ûʱ£¬»á²úÉúÂÒÂ룬ÓÉÏÂÃæµÄ·½·¨¿ÉÒÔ½â¾ö£º
Sting s2 = new String(s1.getBytes("iso8859-1"),"GB2312")½øÐÐת»»¡£
request³£Óõķ½·¨ ......