JSP ã®æŠèŠ#
JSPïŒJava Server PagesïŒã¯ãæ ¹æ¬çã«ã¯ç°¡ç¥åããã Servlet èšèšã§ãããJava 㧠HTML ã¿ã°ã䜿çšããããšãå®çŸããŸããJSP ã¯åçãŠã§ãããŒãžæè¡ã®æšæºã§ãããJavaEE ã®æšæºã§ããããŸããJSP 㯠Servlet ãšåæ§ã«ãµãŒããŒãµã€ãã§å®è¡ãããŸããJSP 㯠Servlet æè¡ã®çºå±ã®åŸãéçºè ã HTML ã¿ã°ãæžãã®ããã䟿å©ã«ããããã«çºå±ããæè¡ã§ãããå®éã«ã¯ Servlet ãã®ãã®ã§ãã
ãããã人ã ã¯éåžž Servlet ã Web ã¢ããªã±ãŒã·ã§ã³ã®å¶åŸ¡ã³ã³ããŒãã³ããšããŠäœ¿çšãããªã¯ãšã¹ãã«å¿ããŠããŒã¿ãçæãããã®ããŒã¿ã転éæè¡ãéã㊠JSP ã«æž¡ãããšã«å°å¿µããJSP æè¡ãããŒã¿è¡šç€ºãã³ãã¬ãŒããšããŠäœ¿çšããŸããããã«ãããããã°ã©ã ã®æ§é ãããæ確ã«ãªããå¯èªæ§ãšä¿å®æ§ãåäžããŸãã
ãã䜿ãããåçãŠã§ããµã€ãéçºæè¡#
JSPïŒJava ãã©ãããã©ãŒã ã§ãããå®å šæ§ãé«ãã倧èŠæš¡ã§äŒæ¥ã¬ãã«ã®åæ£å Web ã¢ããªã±ãŒã·ã§ã³ã®éçºã«é©ããŠããŸããäŸïŒHadoopãé»åéè¡ã12306 ãªã©ã
ASP.NETïŒ.Net ãã©ãããã©ãŒã ã§ãããç°¡åã«åŠã¹ãŸããããããå®å šæ§ãã¯ãã¹ãã©ãããã©ãŒã æ§ãå£ããŸãã
PHPïŒã·ã³ãã«ã§å¹ççãã³ã¹ããäœããéçºãµã€ã¯ã«ãçããç¹ã«äžå°äŒæ¥ã® Web ã¢ããªã±ãŒã·ã§ã³éçºã«é©ããŠããŸããïŒLAMPïŒLinux+Apache+MySQL+PHPïŒ
JSP ããŒãžèŠçŽ ã®æ§æ#
JSP ãã£ã¬ã¯ãã£ã#
JSP ãã£ã¬ã¯ãã£ãïŒdirectiveïŒã¯ JSP ãšã³ãžã³ã®ããã«èšèšãããŠãããå¯èŠåºåãçŽæ¥çæãããã®ã§ã¯ãªããJSP ããŒãžå
ã®æ®ãã®éšåãã©ã®ããã«åŠçãããããšã³ãžã³ã«æ瀺ããŸããJSP2.0 ä»æ§ã§ã¯ã3 ã€ã®ãã£ã¬ã¯ãã£ããå®çŸ©ãããŠãããåºæ¬çãªæ§æ圢åŒã¯<%@ directive attributeName="value" %>
ã§ãããã£ã¬ã¯ãã£ãã«è€æ°ã®å±æ§ãããå Žåããããã®å±æ§ã¯ 1 ã€ã®ãã£ã¬ã¯ãã£ãå
ã«æžãããšããåããŠæžãããšãã§ããŸãã
-
page ãã£ã¬ã¯ãã£ãïŒpage ãã£ã¬ã¯ãã£ã㯠JSP ããŒãžã®ããŸããŸãªå±æ§ãå®çŸ©ããããã«äœ¿çšããããã£ã¬ã¯ãã£ããããŒãžå ã®ã©ãã«åºçŸããŠãããã®å¹æã¯å šäœã® JSP ããŒãžã«åã³ãŸããããã°ã©ã ã®å¯èªæ§ãä¿ã¡ãè¯ãããã°ã©ãã³ã°ç¿æ £ã«åŸãããã«ãpage ãã£ã¬ã¯ãã£ãã¯éåžžãå šäœã® JSP ããŒãžã®éå§äœçœ®ã«çœ®ãããŸãã1 ã€ã®ããŒãžã«ã¯è€æ°ã® page ãã£ã¬ã¯ãã£ããååšããããšãã§ããŸãã
<%@ page language="java" contentType="text/html,ISO-8859-1" import="java.util.*,java.sql.*,java.io.*" session="true|false" buffer="none|8kb|sizekb" autoFlush="true|false" info="æååã®å 容" errorPage="relative_url" isErrorpage="true|false"%>
- languageïŒJSP ããŒãžã§äœ¿çšããã¹ã¯ãªããèšèªãæå®ããããã©ã«ãå€ã¯ java ã§ãã
- contentTypeïŒJSP ããŒãžã®ãã¡ã€ã«ã¿ã€ããšäœ¿çšãããšã³ã³ãŒãã£ã³ã°æ¹åŒãæå®ããããã©ã«ãå€ã¯ âtext/html,ISO-8859-1â ã§ãã
- importïŒãã®å±æ§ã䜿çšããŠã¹ã¯ãªããèšèªã§äœ¿çšãããã¯ã©ã¹ãã¡ã€ã«ãåç §ããè€æ°ã®ã¯ã©ã¹ãããã±ãŒãžãã«ã³ãã§åºåã£ãŠã€ã³ããŒãããŸããJSP ãšã³ãžã³ã¯èªåçã« java.lang.;java.servlet.;javax.servlet.jsp._;javax.servlet.http._ãã€ã³ããŒãããŸãã
- pageEncodingïŒJSP ããŒãžã®ãšã³ã³ãŒãã£ã³ã°æ¹åŒãæå®ããããã©ã«ãå€ã¯ âISO-8859-1â ã§ãã
- sessionïŒã»ãã·ã§ã³ãªããžã§ã¯ããäœæãããã©ããã瀺ããããã©ã«ã㯠true ã§ãã
- bufferïŒout ãªããžã§ã¯ãããããã¡ãäœæãããã©ãããæå®ãããããã¡ãµã€ãºãæå®ããŸããããã©ã«ã㯠8kb ã§ãnone ã¯ãããã¡ãäœæããªãããšã瀺ããŸãã
- autoFlushïŒãããã¡ãèªåçã«ãã©ãã·ã¥ããããã©ããã瀺ããããã©ã«ã㯠true ã§ããfalse ã®å Žåããããã¡ãæºæ¯ã«ãªã£ãåŸã«æåã§ãã©ãã·ã¥ããªããšäŸå€ãçºçããŸãã
- infoïŒæååå®æ°ãå®çŸ©ããgetServletInfo ã¡ãœããã䜿çšããŠå°å·ã§ããŸãã
- errorPageïŒäŸå€åŠçããŒãžãæå®ããŸããweb.xml å ã§èŠçŽ ã䜿çšããŠå šäœã® WEB ã¢ããªã±ãŒã·ã§ã³ã«åŠçããŒãžãèšå®ããããšãã§ãããã®åèŠçŽ ã¯äŸå€ã¯ã©ã¹ã®å®å šä¿®é£Ÿåãæå®ããèŠçŽ 㯠â/â ã§å§ãŸããšã©ãŒãã³ããªã³ã°ããŒãžã®ãã¹ãæå®ããŸããç¹å®ã® JSP ããŒãžã® errorPage å±æ§ãèšå®ãããŠããå Žåãweb.xml ãã¡ã€ã«å ã®ãã®ãšã©ãŒåŠçã¯ãã®ããŒãžã«ã¯é©çšãããŸããã
-
include ãã£ã¬ã¯ãã£ãïŒä»ã® JSP ããŒãžãã€ã³ã¯ã«ãŒãããããã«äœ¿çšãããŸããinclude ãã£ã¬ã¯ãã£ãã䜿çšããŠä»ã® JSP ããŒãžãã€ã³ã¯ã«ãŒããããšãJSP ãšã³ãžã³ã¯ããã 2 ã€ã® JSP ã 1 ã€ã® Servlet ã«ç¿»èš³ããŸãããããã£ãŠãinclude ãã£ã¬ã¯ãã£ãã«ããã€ã³ã¯ã«ãŒãã¯éåžžéçã€ã³ã¯ã«ãŒããšãåŒã°ããŸãã
<%@ include file="å«ãŸããã³ã³ããŒãã³ãã®çµ¶å¯ŸURLãŸãã¯çžå¯ŸURL" %>
ã€ã³ã¯ã«ãŒãããããã¡ã€ã«ã¯ JSP æ§æã«åŸãå¿ èŠããããŸããã€ã³ã¯ã«ãŒãããããã¡ã€ã«ã¯ä»»æã®æ¡åŒµåãæã€ããšãã§ããããšããã®æ¡åŒµåã html ã§ãã£ãŠããJSP ãšã³ãžã³ã¯ããã JSP ããŒãžã®å 容ãåŠçããæ¹æ³ã§åŠçããŸããæå³ãæ確ã«ããããã«ãJSP ä»æ§ã§ã¯éçã€ã³ã¯ã«ãŒããã¡ã€ã«ã®æ¡åŒµåãšããŠ.jspfïŒJSP ãã©ã°ã¡ã³ãïŒã䜿çšããããšãæšå¥šããŠããŸããinclude ã䜿çšãããšã2 ã€ã® JSP ããŒãžãé¢äžããJSO ã 1 ã€ã® Servlet ã«ç¿»èš³ããããããããã 2 ã€ã® JSP ããŒãžã®ãã£ã¬ã¯ãã£ãã¯è¡çªããŠã¯ãªããŸããïŒpageEncoding ãšã€ã³ããŒããé€ãïŒã
- taglib ãã£ã¬ã¯ãã£ãïŒã«ã¹ã¿ã ã¿ã°ãå®çŸ©ããããã«ã¿ã°ã©ã€ãã©ãªã䜿çšããJSP ããŒãžå ã§ã«ã¹ã¿ãã€ãºãããåäœãæå¹ã«ããŸãã
è¡šçŸåŒ#
JSP ããŒãžå
ã§å®è¡ãããè¡šçŸåŒ <%=è¡šçŸåŒ%>
ã泚æïŒè¡šçŸåŒã¯ã»ãã³ãã³ã§çµãããªãããšã«æ³šæããŠãã ãããäŸãã°ãçŸåšã®æéïŒ<%= new java.util.Date() %>
ã¹ã¯ãªããã¹ãããã#
JSP ããŒãžå
ã«è€æ°è¡ã® Java ã³ãŒããæ¿å
¥ãã <% Javaã³ãŒã %>
ãJSP ãšã³ãžã³ã¯ JSP ããŒãžã翻蚳ããéã«ãJSP ã¹ã¯ãªããã¹ããããå
ã® Java ã³ãŒãããã®ãŸãŸ Servlet ã®_jspServlet ã¡ãœããã«é
眮ããŸãããããã£ãŠã<% %>
å
ã® Java ã³ãŒã㯠Java æ§æã«å³å¯ã«åŸãå¿
èŠããããŸãã
宣èš#
JSP ããŒãžå
ã§å€æ°ãŸãã¯ã¡ãœãããå®çŸ©ãã <%! Javaã³ãŒã %>
ã宣èšå
ã® Java ã³ãŒãã¯_jspService ã¡ãœããã®å€åŽã«ç¿»èš³ãããã¯ã©ã¹ã«å±ããŸãããããã£ãŠãã¡ã³ããŒå€æ°ã宣èšããŠåæåããããšããã¡ãœããã宣èšãŸãã¯å®çŸ©ããããšãã§ããŸãããŸããéçã³ãŒããããã¯ã宣èšããããšãã§ããŸããJSP ã®æé»ã®ãªããžã§ã¯ãã®äœçšç¯å²ã¯ Servlet ã®_jspService ã¡ãœããã«éããããããJSP 宣èšå
ã§ãããã®æé»ã®ãªããžã§ã¯ãã䜿çšããããšã¯ã§ããŸããã
<%!
static {
System.out.println("Servletãèªã¿èŸŒãã§ããŸãïŒ");
}
private int globalVar = 0;
public void jspInit() {
System.out.println("jspãåæåããŠããŸãïŒ");
}
%>
<%!
public void jspDestroy() {
System.out.println("jspãç Žæ£ããŠããŸãïŒ");
}
%>
ã³ã¡ã³ãïŒ3 ã€ã®æ¹æ³ïŒ#
- HTML ã³ã¡ã³ã
- JSP ã³ã¡ã³ãïŒ
<%--JSPã³ã¡ã³ããã¯ã©ã€ã¢ã³ãã«ã¯èŠããªã--%>
- JSP ã¹ã¯ãªããã³ã¡ã³ãïŒããªãã¡ Java ã³ã¡ã³ã
//åäžè¡
ã/*è€æ°è¡ */
éçã³ã³ãã³ã#
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<html>
<head>
<title>index.jspããŒãž</title>
</head>
<body>
<h1>çããããã«ã¡ã¯</h1><hr>
<!-- ç§ã¯HTMLã³ã¡ã³ãã§ãã¯ã©ã€ã¢ã³ãã«èŠããŸã -->
<%-- ç§ã¯JSPã³ã¡ã³ãã§ãã¯ã©ã€ã¢ã³ãã«ã¯èŠããŸãã --%>
<%!
String s = "匵äž";
//æååå€æ°ã宣èšããŸãã
int add(int x,int y) {
//æŽæ°ã®åèšãèšç®ããæ»ãå€ã®æŽæ°åé¢æ°ã宣èšããŸããã
return x+y;
}
%>
<%
//åäžè¡ã³ã¡ã³ã
/*è€æ°è¡ã³ã¡ã³ã*/
out.println("çããããã«ã¡ã¯");
%>
<br>
ããã«ã¡ã¯ã<%=s %><br>
x+y=<%=add(10,5) %><br>
</body>
</html>
JSP ã®å®è¡åçãšã©ã€ããµã€ã¯ã«#
ãŠãŒã¶ãŒãæåã« Tomcat ã«ãªã¯ãšã¹ããéä¿¡ãããšãJSP ãã¡ã€ã«ã¯ Servlet ã® Java ãã¡ã€ã«ã«ã³ã³ãã€ã«ããããã® Java ãã¡ã€ã«ã¯ã¯ã©ã¹ãã¡ã€ã«ã«ã³ã³ãã€ã«ãããã¡ã¢ãªã«ããŒããããçæãã¡ã€ã«ã¯ Tomcat ã® work ãã£ã¬ã¯ããªå ã®å¯Ÿå¿ãããããžã§ã¯ããã©ã«ãã«ä¿åãããŸããJSP ããŒãžãå€æŽãããå ŽåãJSP ãšã³ãžã³ã¯ãããåã³ã³ãã€ã«ããã¡ã¢ãªã«ããŒãããŠãŠãŒã¶ãŒã®ãªã¯ãšã¹ãã«åããŸãã泚æïŒãŠãŒã¶ãŒã JSP ããŒãžãæåã«ãªã¯ãšã¹ããããšããæåã«å®è¡ãããã¡ãœããã¯ã³ã³ã¹ãã©ã¯ã¿ã§ãã
_jspService () ã¡ãœãããåŒã³åºãããŠã¯ã©ã€ã¢ã³ãã®ãªã¯ãšã¹ããåŠçããŸããåãªã¯ãšã¹ãã«å¯ŸããŠãJSP ãšã³ãžã³ã¯ãã®ãªã¯ãšã¹ããåŠçããããã®ã¹ã¬ãããäœæããŸããè€æ°ã®ã¯ã©ã€ã¢ã³ããåæã«ãã® JSP ãã¡ã€ã«ããªã¯ãšã¹ããããšãJSP ãšã³ãžã³ã¯è€æ°ã®ã¹ã¬ãããäœæããŸããåã¯ã©ã€ã¢ã³ãã®ãªã¯ãšã¹ã㯠1 ã€ã®ã¹ã¬ããã«å¯Ÿå¿ããŸãããã«ãã¹ã¬ããæ¹åŒã§å®è¡ããããšã§ãã·ã¹ãã ãªãœãŒã¹ã®èŠæ±ãå€§å¹ ã«åæžããã·ã¹ãã ã®åæåŠçèœåãšå¿çæéãåäžãããããšãã§ããŸãããããããã«ãã¹ã¬ããããã°ã©ãã³ã°ã«ãã£ãŠåŒãèµ·ããããåæåé¡ã«ã泚æãå¿ èŠã§ãããã® Servlet ã¯åžžã«ã¡ã¢ãªã«åžžé§ããŠãããããå¿çã¯éåžžã«è¿ éã§ãã
JSP ãšã³ãžã³ã JSP ã«å¯Ÿå¿ãã jspServlet ãåŒã³åºããšãWeb éçºã«é¢é£ãã 9 ã€ã®ãªããžã§ã¯ãã jspServlet ã䜿çšããããã«æž¡ããäœæããŸããJSP æè¡ã®èšèšè ã¯ãéçºè ã JSP ããŒãžãäœæããéã«ãããã® Web ãªããžã§ã¯ãã®åç §ãååŸã§ããããã«ã9 ã€ã®å¯Ÿå¿ããå€æ°ãç¹å¥ã«å®çŸ©ããŸãããéçºè 㯠JSP ããŒãžå ã§ãããã®å€æ°ãéããŠãããã 9 ã€ã®ãªããžã§ã¯ãã®åç §ãè¿ éã«ååŸã§ããŸãã
JSP ã® 9 ã€ã®çµã¿èŸŒã¿ãªããžã§ã¯ã#
çµã¿èŸŒã¿ãªããžã§ã¯ãã®æŠèŠ#
JSP çµã¿èŸŒã¿ãªããžã§ã¯ãã¯ãWeb ã³ã³ããã«ãã£ãŠäœæãããäžé£ã®ãªããžã§ã¯ãã§ãããnew ããŒã¯ãŒãã䜿çšããã«äœ¿çšã§ããçµã¿èŸŒã¿ãªããžã§ã¯ãã§ãã
4 ã€ã®ã¹ã³ãŒãç¯å²#
- application_SCOPEïŒWeb ã¢ããªã±ãŒã·ã§ã³å šäœã«äœçšããè€æ°ã®ãŠãŒã¶ãŒéã§å ±æãããŸãã
- session_SCOPEïŒWeb ã¢ããªã±ãŒã·ã§ã³å šäœã«äœçšããåäžã®ãŠãŒã¶ãŒéã§å ±æãããŸãã
- request_SCOPEïŒãªã¯ãšã¹ãã«äœçšãã転ééãªã¯ãšã¹ãã§å ±æãããŸãã
- page_SCOPEïŒçŸåšã®ããŒãžã«äœçšããçŸåšã®ããŒãžã§ã®ã¿å¯èŠã§ãã
out ãªããžã§ã¯ã#
ãããã¡ïŒãããã¡ãšã¯ãã¡ã¢ãªå ã®äžéšã®é åã§ãäžæããŒã¿ãä¿åããããã«äœ¿çšãããŸãã
out ãªããžã§ã¯ãïŒJspWriter ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ã§ãããã¯ã©ã€ã¢ã³ãã«ããã¹ããéä¿¡ããããã«ãã䜿çšããããªããžã§ã¯ãã§ãpageContext ãªããžã§ã¯ãã® getOut ã¡ãœãããåŒã³åºãããšã§è¿ãããŸãããã®æ©èœãšäœ¿çšæ³ã¯ãServletResponse.getWriter ã¡ãœãããè¿ã PrintWriter ãªããžã§ã¯ããšéåžžã«äŒŒãŠããŸããJSP ããŒãžå ã® out æé»ãªããžã§ã¯ãã®å㯠JspWriter ã§ãããJspWriter ã¯ãã£ãã·ã¥æ©èœãæ〠PrintWriter ã«çžåœããŸããJSP ããŒãžã® page ãã£ã¬ã¯ãã£ãã® buffer å±æ§ãèšå®ããããšã§ããã®ãã£ãã·ã¥ãµã€ãºã調æŽãããããã£ãã·ã¥ãç¡å¹ã«ãããã§ããŸãããããã¡ã以äžã®æ¡ä»¶ãæºããå Žåã«ã®ã¿ãServlet ãšã³ãžã³ãæäŸãããããã¡ã«æžã蟌ãŸããŸãïŒpage ãã£ã¬ã¯ãã£ãã® buffer å±æ§ã out ãªããžã§ã¯ãã®ãã£ãã·ã¥æ©èœãç¡å¹ã«ããå ŽåïŒout ãªããžã§ã¯ãã®ãããã¡ãæºæ¯ã«ãªã£ãå ŽåïŒJSP ããŒãžå šäœãçµäºããå Žåã
<%
out.println("aaa");
response.getWriter().write("bbb");
%>
"bbbã¯aaaããå
ã«åºåãããŸã"
äž»ãªã¡ãœããïŒ
- void println (); ã¯ã©ã€ã¢ã³ãã«æååãå°å·ããŸãã
- void clear (); ãããã¡ã®å 容ãã¯ãªã¢ããŸããflush ã®åŸã«åŒã³åºããšäŸå€ãçºçããŸãã
- void clearBuffer (); ãããã¡ã®å 容ãã¯ãªã¢ããŸããflush ã®åŸã«åŒã³åºããŠãäŸå€ã¯çºçããŸããã
- void flush (); ãããã¡ã®å 容ãã¯ã©ã€ã¢ã³ãã«åºåããŸãã
- int getBufferSize (); ãããã¡ã®ãµã€ãºïŒãã€ãæ°ïŒãè¿ããŸãããããã¡ãèšå®ãããŠããªãå Žå㯠0 ã§ãã
- int getRemaining (); ãããã¡ã«æ®ã£ãŠããå¯çšæ§ãè¿ããŸãã
- boolean isAutoFlush (); ãããã¡ãæºæ¯ã®ãšããèªåçã«ã¯ãªã¢ãããäŸå€ãã¹ããŒããããè¿ããŸãã
- void close (); åºåã¹ããªãŒã ãéããŸãã
request/response ãªããžã§ã¯ã#
ãã©ãŒã ã® 2 ã€ã®éä¿¡æ¹æ³
-
GetïŒææ㧠URL ãéããŠããŒã¿ãéä¿¡ããããŒã¿ã¯ URL å ã§èŠãããšãã§ããŸããéä¿¡ãããããŒã¿ã¯æ倧 2KB ãè¶ ããããšã¯ãããŸãããå®å šæ§ã¯äœãã§ãããpost æ¹åŒãããå¹çãé«ãã§ããããŒã¿éãå°ãªããå®å šæ§ãé«ãããŒã¿ã®éä¿¡ã«é©ããŠããŸããäŸïŒæ€çŽ¢ãã¯ãšãªãªã©ã®æ©èœã
-
PostïŒãŠãŒã¶ãŒãéä¿¡ããæ å ±ã HTML ããããŒå ã«ã«ãã»ã«åããŸããããŒã¿éãå€ããå®å šæ§ãé«ããŠãŒã¶ãŒæ å ±ã®éä¿¡ã«é©ããŠããŸããäŸïŒç»é²ãå€æŽãã¢ããããŒããªã©ã®æ©èœã
request ãªããžã§ã¯ã#
ã¯ã©ã€ã¢ã³ãã®ãªã¯ãšã¹ãæ å ±ã¯ request ãªããžã§ã¯ãã«ã«ãã»ã«åãããŠããããããéããŠã¯ã©ã€ã¢ã³ãã®èŠæ±ãç解ããå¿çãè¡ããŸãããã㯠HttpServletRequest ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ã§ããrequest ãªããžã§ã¯ãã¯ãªã¯ãšã¹ãã¹ã³ãŒããæã¡ãã¯ã©ã€ã¢ã³ãã®ãªã¯ãšã¹ããå®äºãããŸã§æå¹ã§ãã
request ãªããžã§ã¯ãã®äž»ãªã¡ãœãã
- String getParameter (); æå®ããããã©ã¡ãŒã¿ã®å€ãè¿ããŸãã
- String [] getParameterValues (); æå®ããããã©ã¡ãŒã¿ã®ãã¹ãŠã®å€ãè¿ããŸãã
- void setAttribute (); ãã®ãªã¯ãšã¹ãå ã®å±æ§ãä¿åããŸãã
- Object getAttribute (); æå®ãããå±æ§ã®å€ãè¿ããŸãã
- String getContentType (); ãªã¯ãšã¹ãããã£ã® MIME ã¿ã€ããååŸããŸãã
- String getProtocol (); ãªã¯ãšã¹ãã«äœ¿çšããããããã³ã«ãšããŒãžã§ã³çªå·ãè¿ããŸãã
- String getServerName (); ãªã¯ãšã¹ããåãåã£ããµãŒããŒãã¹ãåãè¿ããŸãã
- int getServerPort (); ãµãŒããŒããã®ãªã¯ãšã¹ããåãåã£ãããŒãçªå·ãè¿ããŸãã
- String getCharacterEncoding (); æåãšã³ã³ãŒãã£ã³ã°æ¹åŒãè¿ããŸãã
- void setContentEncoding (); ãªã¯ãšã¹ãã®æåãšã³ã³ãŒãã£ã³ã°æ¹åŒãèšå®ããŸãã
- int getContentLength (); ãªã¯ãšã¹ãããã£ã®é·ãïŒãã€ãæ°ïŒãè¿ããŸãã
- String getRemoteAddr (); ãã®ãªã¯ãšã¹ããéä¿¡ããã¯ã©ã€ã¢ã³ãã® IP ã¢ãã¬ã¹ãè¿ããŸãã
- String getRealPath (String path); ä»®æ³ãã¹ã®å®éã®ãã¹ãŸãã¯çžå¯Ÿãã¹ã®çµ¶å¯Ÿãã¹ãè¿ããŸãã
- String getContextPath (); ã³ã³ããã¹ããã¹ãè¿ããŸããããªãã¡ãããžã§ã¯ãã®ã«ãŒããã£ã¬ã¯ããªã§ãã
æ¥æ¬èªã®æååãåé¡ïŒrequest.setCharacterEncoding("UTF-8");
URL å ã®æ¥æ¬èªã®æååãåé¡ïŒTomcatã®/conf/server.xmlã«å±æ§ãè¿œå ããŸã
ã
response ãªããžã§ã¯ã#
response ãªããžã§ã¯ãã¯ãã¯ã©ã€ã¢ã³ãã®ãªã¯ãšã¹ãã«å¯Ÿããå¿çã«é¢ããæ å ±ãå«ãã§ããŸãããJSP å ã§ã¯çŽæ¥äœ¿çšãããããšã¯ã»ãšãã©ãããŸããããã㯠HttpServletResponse ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ã§ããresponse ãªããžã§ã¯ãã¯ããŒãžã¹ã³ãŒããæã¡ãããŒãžã«ã¢ã¯ã»ã¹ããéããã®ããŒãžå ã® response ãªããžã§ã¯ãã¯ãã®ã¢ã¯ã»ã¹ã«å¯ŸããŠã®ã¿æå¹ã§ãããä»ã® response ãªããžã§ã¯ãã¯çŸåšã®ããŒãžã«ã¯ç¡å¹ã§ãã
response ãªããžã§ã¯ãã®äž»ãªã¡ãœãã
- String getCharacterEncoding (); å¿çã«äœ¿çšãããæåãšã³ã³ãŒãã£ã³ã°ãè¿ããŸãã
- void setContentType (); å¿çã® MIME ã¿ã€ããèšå®ããŸããäžè¬çã«ã¯ "text/html, charset=UTF-8" ã§ãã
- PrintWriter getWriter (); ã¯ã©ã€ã¢ã³ãã«æåãåºåããããã®ãªããžã§ã¯ããè¿ããŸãïŒâ泚æïŒPrintWriter ãšçµã¿èŸŒã¿ out ãªããžã§ã¯ãã®éããPrintWriter ãªããžã§ã¯ãã®åºåã¯åžžã«çµã¿èŸŒã¿ out ãªããžã§ã¯ããããå ã«è¡ãããããout å ã§æå㧠flush ãããŸãâïŒã
- sendRedirect (location); ã¯ã©ã€ã¢ã³ãã®ãªã¯ãšã¹ããå¥ã®ãªãœãŒã¹ã«ãªãã€ã¬ã¯ãããŸãã
ãªã¯ãšã¹ããªãã€ã¬ã¯ãïŒã¯ã©ã€ã¢ã³ãã®è¡åã§ãããresponse.sendRedirect () ã¯æ¬è³ªçã« 2 åã®ãªã¯ãšã¹ãã«çãããååã®ãªã¯ãšã¹ããªããžã§ã¯ãã¯ä¿åããããã¢ãã¬ã¹ããŒã® URL ã¯å€æŽãããŸãã
ãªã¯ãšã¹ã転éïŒãµãŒããŒã®è¡åã§ãããrequest.getRequestDispatcher ().forward (); 㯠1 åã®ãªã¯ãšã¹ãã§ããã転éåŸã®ãªã¯ãšã¹ããªããžã§ã¯ãã¯ä¿åãããã¢ãã¬ã¹ããŒã® URL ã¯å€æŽãããŸããã
session ãªããžã§ã¯ã#
session ã¯ã¯ã©ã€ã¢ã³ããšãµãŒããŒã® 1 åã®ã»ãã·ã§ã³ãè¡šããWeb ã«ããã session ã¯ããŠãŒã¶ãŒããããŠã§ããµã€ããé²èŠ§ããéããµã€ãã«å ¥ã£ãŠãããã©ãŠã¶ãéãããŸã§ã®éã®æéãæããŸããã€ãŸãããŠãŒã¶ãŒããã®ãŠã§ããµã€ããé²èŠ§ããã®ã«ãããæéã§ãã
ãµãŒããŒã®ã¡ã¢ãªå ã«ã¯ãç°ãªããŠãŒã¶ãŒã®ããã«ç°ãªã session ãä¿åãããŠããŸãã
session ãªããžã§ã¯ã㯠JSP ã®çµã¿èŸŒã¿ãªããžã§ã¯ãã§ãã
session ãªããžã§ã¯ãã¯æåã® JSP ããŒãžãããŒãããããšèªåçã«äœæãããã»ãã·ã§ã³æéã®ç®¡çãè¡ããŸãã
ã¯ã©ã€ã¢ã³ãããã©ãŠã¶ãéããŠãµãŒããŒã«æ¥ç¶ãéå§ããã¯ã©ã€ã¢ã³ãããã©ãŠã¶ãéããŠãã®ãµãŒããŒãé¢ãããŸã§ã 1 åã®ã»ãã·ã§ã³ãšåŒã°ããŸãã
ã¯ã©ã€ã¢ã³ãããµãŒããŒã«ã¢ã¯ã»ã¹ãããšãããµãŒããŒã¯ã¯ã©ã€ã¢ã³ããã©ã®ãŠãŒã¶ãŒã§ããããäœããã®æ¹æ³ã§ç¥ãå¿ èŠãããããã®ããã« session ãªããžã§ã¯ããå¿ èŠã§ãã
session ãªããžã§ã¯ã㯠HttpSession ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ã§ãã
session ã®äž»ãªã¡ãœãã
- long getCreationTime (); session ã®äœææéãè¿ããŸãã
- String getId (); session ãäœæããããšãã« JSP ãšã³ãžã³ãèšå®ãããŠããŒã¯ãª ID çªå·ãè¿ããŸãã
- Object setAttribute (); æå®ãããååã§ãªããžã§ã¯ãããã®ã»ãã·ã§ã³ã«ãã€ã³ãããŸãã
- Object getAttribute (); ãã®ã»ãã·ã§ã³å ã§æå®ãããååã«ãã€ã³ãããããªããžã§ã¯ããè¿ããååšããªãå Žå㯠null ãè¿ããŸãã
- String [] getValueNames (); ãã® session å ã®ãã¹ãŠã®å©çšå¯èœãªå±æ§ãå«ãé åãè¿ããŸãã
- int getMaxInactiveInterval (); 2 åã®ãªã¯ãšã¹ãééãã©ããããã®æéã§ãã® session ããã£ã³ã»ã«ããããïŒåäœã¯ç§ïŒãè¿ããŸãã
- void setMaxInactiveInterval (); session ã®åç¶æéãèšå®ããŸãã
session ã®ã©ã€ããµã€ã¯ã«
- äœæïŒã¯ã©ã€ã¢ã³ããæåã« JSP ãŸã㯠Servlet ã«ã¢ã¯ã»ã¹ãããšããµãŒããŒã¯çŸåšã®ã»ãã·ã§ã³ã«å¯Ÿã㊠SessionId ãäœæããŸããã¯ã©ã€ã¢ã³ãããµãŒããŒã«ãªã¯ãšã¹ããéä¿¡ãããã³ã«ããã® SessionId ãæã£ãŠããããµãŒããŒã¯ãã® SessionId ãæ€èšŒããŸãã
- 掻åïŒããã»ãã·ã§ã³å ã§ãã€ããŒãªã³ã¯ã䜿çšããŠæ°ããããŒãžãéãããšã¯åãã»ãã·ã§ã³ã«å±ããŸããçŸåšã®ã»ãã·ã§ã³ããŒãžããã¹ãŠéããããªãéããæ°ãããã©ãŠã¶ãŠã£ã³ããŠãéããŠåããããžã§ã¯ããªãœãŒã¹ã«ã¢ã¯ã»ã¹ããããšã¯åãã»ãã·ã§ã³ã«å±ããŸããçŸåšã®ã»ãã·ã§ã³ã®ãã¹ãŠã®ããŒãžãéããããåŸãå床 JSP ãŸã㯠Servlet ã«ã¢ã¯ã»ã¹ãããšãæ°ããã»ãã·ã§ã³ãäœæãããŸãïŒãã ãããã®æç¹ã§å ã®ã»ãã·ã§ã³ã¯ãŸã ååšãããã® SessionId ã¯ãµãŒããŒã«æ®ããŸãããã¯ã©ã€ã¢ã³ãã¯ãããæã£ãŠãããããµãŒããŒã«æ€èšŒãäŸé Œããããšã¯ãããŸãããã»ãã·ã§ã³ãã¿ã€ã ã¢ãŠããããŸã§ïŒã
- ç Žæ£ïŒç Žæ£ãããæ¹æ³ã¯ 3 ã€ãããŸãã
- session.invalidate () ã¡ãœãããåŒã³åºãããå Žåã
- ã»ãã·ã§ã³ãæéåãïŒã¿ã€ã ã¢ãŠãïŒã«ãªãå Žåãweb.xml ã§ã»ãã·ã§ã³ã®ã¿ã€ã ã¢ãŠãæéãèšå®ã§ããŸããåäœã¯åã§ãã
- ãµãŒããŒãåèµ·åãããå Žåã
application ãªããžã§ã¯ã#
application ãªããžã§ã¯ãã¯ããŠãŒã¶ãŒéã§ããŒã¿ãå
±æããããšãå®çŸããã°ããŒãã«å€æ°ãä¿åã§ããŸãã
application ã¯ãµãŒããŒã®èµ·åããå§ãŸãããµãŒããŒã®çµäºãŸã§ç¶ããŸãã
ãŠãŒã¶ãŒã®ååŸã®æ¥ç¶ãç°ãªããŠãŒã¶ãŒéã®æ¥ç¶ã§ãapplication ãªããžã§ã¯ãã®åãå±æ§ã«å¯ŸããŠæäœãè¡ãããšãã§ããŸãã
application ãªããžã§ã¯ãã®å±æ§ã«å¯Ÿããæäœã¯ãä»ã®ãŠãŒã¶ãŒã®ã¢ã¯ã»ã¹ã«åœ±é¿ãäžããŸãã
ãµãŒããŒã®èµ·åãšçµäºã application ãªããžã§ã¯ãã®ã©ã€ããµã€ã¯ã«ã決å®ããŸãã
application ãªããžã§ã¯ã㯠ServletContext ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ã§ãã
application ã®äž»ãªã¡ãœãã
- void setAttribute (); æå®ãããååã§ãªããžã§ã¯ãããã®ã»ãã·ã§ã³ã«ãã€ã³ãããŸãã
- Object getAttribute (); ãã®ã»ãã·ã§ã³å ã§æå®ãããååã«ãã€ã³ãããããªããžã§ã¯ããè¿ããååšããªãå Žå㯠null ãè¿ããŸãã
- Enumeration getAttributeNames (); ãã¹ãŠã®å©çšå¯èœãªå±æ§åã®åæãè¿ããŸãã
- String getServerInfo (); JSPïŒServletïŒãšã³ãžã³åãšããŒãžã§ã³çªå·ãè¿ããŸãã
page ãªããžã§ã¯ã#
page ãªããžã§ã¯ãã¯ãçŸåšã® JSP ããŒãžèªäœãæããã¯ã©ã¹å ã® this ãã€ã³ã¿ã®ãããªãã®ã§ãjava.lang.Object ã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ã§ããäž»ãªã¡ãœãã㯠Object ã¯ã©ã¹ã®ã¡ã³ããŒæ¹æ³ã§ãã
pageContext ãªããžã§ã¯ã#
pageContext ãªããžã§ã¯ã㯠JSP æè¡ã®äžã§æãéèŠãªãªããžã§ã¯ãã§ãããJSP ããŒãžã®å®è¡ç°å¢ãè¡šããŸãããã®ãªããžã§ã¯ãã¯ãä»ã® 8 ã€ã®æé»ã®ãªããžã§ã¯ããžã®åç §ãã«ãã»ã«åããã ãã§ãªããããŒã¿ãä¿åããããã®ã¹ã³ãŒããªããžã§ã¯ãã§ããããŸãããŸãããã®ãªããžã§ã¯ãã¯ãincludeãforward ãªã©ã®ä»ã®ãªãœãŒã¹ãååŸããããã®äžè¬çãªæäœãã«ãã»ã«åããŠããŸãã
pageContext ãªããžã§ã¯ãã¯ãJSP ããŒãžå ã®ãã¹ãŠã®ãªããžã§ã¯ãããã³åå空éãžã®ã¢ã¯ã»ã¹ãæäŸããŸãããã®ãªããžã§ã¯ãã¯ãçŸåšã®ããŒãžãæåšãã Session ã«ã¢ã¯ã»ã¹ã§ãããŸããã®ããŒãžãæåšãã application å ã®å±æ§å€ãååŸããããšãã§ããŸãããã®ãªããžã§ã¯ãã¯ãããŒãžå ã®ãã¹ãŠã®æ©èœã®é倧æè ã§ããããŸãããã®ãªããžã§ã¯ãã®ã¯ã©ã¹åã pageContext ãšåŒã°ããŸãã
pageContext ã®äž»ãªã¡ãœãã
- JspWriter getOut (); çŸåšã®ã¯ã©ã€ã¢ã³ãå¿çã«äœ¿çšãããŠãã out ãªããžã§ã¯ããè¿ããŸãã
- HttpSession getSession (); çŸåšã®ããŒãžå ã® Session ãªããžã§ã¯ããè¿ããŸãã
- Object getPage (); çŸåšã®ããŒãžã® page ãªããžã§ã¯ããè¿ããŸãã
- ServletRequest getRequest (); çŸåšã®ããŒãžã® Request ãªããžã§ã¯ããè¿ããŸãã
- ServletResponse getResponse (); çŸåšã®ããŒãžã® Response ãªããžã§ã¯ããè¿ããŸãã
- void setAttribute (); å±æ§ãšå±æ§å€ãèšå®ããŸãã
- Object getAttribute (); æå®ãããç¯å²å ã§å±æ§ã®å€ãååŸããŸãã
- int getAttributeScope (); ç¹å®ã®å±æ§ã®ã¹ã³ãŒããè¿ããŸãã
- void forward (); å¥ã®ããŒãžã«ãžã£ã³ãããã¢ãã¬ã¹ããŒã¯å€ãããŸããã
- void include (); å¥ã®ããŒãžãå«ããŸãã
- PageContext.APPLICATION_SCOPE ã¯åã¹ã³ãŒãã®å®æ°ãè¡šããŸãã
- PageContext.SESSION_SCOPE
- PageContext.REQUEST_SCOPE
- PageContext.PAGE_SCOPE
- findAttribute (); åã¹ã³ãŒãå ã®å±æ§ãæ€çŽ¢ããŸãã
Config ãªããžã§ã¯ã#
Config ãªããžã§ã¯ãã¯ãServlet ãåæåãããéã« JSP ãšã³ãžã³ãæ å ±ãæž¡ãããã«äœ¿çšããããã®æ å ±ã«ã¯ Servlet åæåæã«å¿ èŠãªãã©ã¡ãŒã¿ïŒå±æ§åãšå±æ§å€ã§æ§æãããïŒããµãŒããŒã«é¢ããæ å ±ïŒServletContext ãªããžã§ã¯ããæž¡ãããšã«ãã£ãŠïŒãå«ã¿ãŸãã
Config ã®äž»ãªã¡ãœãã
- ServletContext getServletContext (); ãµãŒããŒæ å ±ãå«ã ServletContext ãªããžã§ã¯ããè¿ããŸãã
- String getInitParameter (); åæåãã©ã¡ãŒã¿ã®å€ãè¿ããŸãã
- Enumeration getInitParameterNames (); Servlet ã®åæåã«å¿ èŠãªãã¹ãŠã®ãã©ã¡ãŒã¿ã®åæãè¿ããŸãã
Exception ãªããžã§ã¯ã#
Exception ãªããžã§ã¯ãã¯ãããŒãžãå®è¡äžã«äŸå€ãçºçãããšãã«çæãããäŸå€ãªããžã§ã¯ãã§ããJSP ããŒãžããã®ãªããžã§ã¯ãã䜿çšããå ŽåãisErrorPage ã true ã«èšå®ããå¿
èŠããããŸããããã§ãªããšãã³ã³ãã€ã«ã§ããŸãããããã¯å®éã«ã¯ java.lang.Throwable ã®ãªããžã§ã¯ãã§ããããŒãžãäŸå€ãçºçãããšãã«åŠçãåããã«ã¯ãäŸå€åŠçããŒãžãæå®ããå¿
èŠããããŸãã<% page errorPage="exception.jsp" %>
exception ã®äž»ãªã¡ãœãã
- String getMessage (); äŸå€ã®èª¬æã¡ãã»ãŒãžãè¿ããŸãã
- String toString (); äŸå€ã«é¢ããç°¡æœãªèª¬æã¡ãã»ãŒãžãè¿ããŸãã
- void printStackTrace (); äŸå€ãšãã®ã¹ã¿ãã¯ãã¬ãŒã¹ã衚瀺ããŸãã
- Throwable fillInStackTrace (); äŸå€ã®å®è¡ã¹ã¿ãã¯ãã¬ãŒã¹ãåæ§ç¯ããŸãã
JSP ã¢ã¯ã·ã§ã³ã¿ã°#
-
JSP ã¢ã¯ã·ã§ã³èŠçŽ ïŒaction elementsïŒïŒã¢ã¯ã·ã§ã³èŠçŽ ã¯ãªã¯ãšã¹ãåŠç段éã§æ å ±ãæäŸããŸããã¢ã¯ã·ã§ã³èŠçŽ 㯠XML èŠçŽ ã®æ§æã«åŸããèŠçŽ åãå«ãéå§ã¿ã°ããããå±æ§ããªãã·ã§ã³ã®å 容ãéå§ã¿ã°ã«äžèŽããçµäºã¿ã°ãæã€ããšãã§ããŸãã
JSP ã¿ã°ã¯ JSP ã¢ã¯ã·ã§ã³ïŒJSP ã¢ã¯ã·ã§ã³ïŒèŠçŽ ãšãåŒã°ããJSP ããŒãžå ã§ããžãã¹ããžãã¯æ©èœãæäŸããJSP ããŒãžå ã«çŽæ¥ Java ã³ãŒããèšè¿°ããããšãé¿ããJSP ããŒãžã®ä¿å®æ§ãåäžãããŸãã -
JSP ã¢ã¯ã·ã§ã³èŠçŽ ã«ã¯ 5 ã€ã®äž»èŠãªã«ããŽãªããããŸãã
- JavaBean ã«é¢é£ãããã®ïŒjspãjspãjspã
- JSP1.2 ä»æ§ã«ååšãã 6 ã€ã®åºæ¬ã¢ã¯ã·ã§ã³èŠçŽ ïŒjspãjspãjspãjspãjspãjspã
- JSP2.0 ã§æ°ãã«è¿œå ãããã䞻㫠JSP ããã¥ã¡ã³ãã«é¢é£ãã 6 ã€ã®ã¢ã¯ã·ã§ã³èŠçŽ ïŒjspãjspãjspãjspãjspãjspã
- JSP2.0 ã§æ°ãã«è¿œå ããããäž»ã«åçã« XML èŠçŽ ã¿ã°ã®å€ãçæãã 3 ã€ã®ã¢ã¯ã·ã§ã³ïŒjspãjspãjspã
- JSP2.0 ã§æ°ãã«è¿œå ããããã¿ã°ãã¡ã€ã«ã§ã®ã¿äœ¿çšãããïŒjspãjspã
-
JSP ã®äžè¬çãªã¿ã°
-
includeïŒãã®ã¿ã°ã¯ãå¥ã®ãªãœãŒã¹ã®åºåå 容ãçŸåšã® JSP ããŒãžã®åºåå 容ã«æ¿å ¥ããããã«äœ¿çšãããŸãããã® JSP ããŒãžã®å®è¡æã«è¡ãããã€ã³ã¯ã«ãŒãæ¹åŒã¯åçã€ã³ã¯ã«ãŒããšåŒã°ããŸãã
<jsp:include page="relativeURL | <%=expression%>" flush="true|false" />
ãflush å±æ§ã¯ãä»ã®ãªãœãŒã¹ã®åºåå 容ãæ¿å ¥ããéã«ãçŸåšã® JSP ããŒãžã®åºåå 容ãã¯ã©ã€ã¢ã³ãã«å ã«ãã©ãã·ã¥ãããã©ãããæå®ããŸãã -
jspãšincludeãã£ã¬ã¯ãã£ãã®æ¯èŒïŒjspã¿ã°ã¯åçã€ã³ã¯ã«ãŒãã§ãããjspã¿ã°ã«é¢äžãã 2 ã€ã® JSP ããŒãžã¯ 2 ã€ã® Servlet ã«ç¿»èš³ãããããã 2 ã€ã® Servlet ã®å 容ã¯å®è¡æã«çµ±åãããŸããäžæ¹ãinclude ãã£ã¬ã¯ãã£ãã¯éçã€ã³ã¯ã«ãŒãã§ãããé¢äžãã 2 ã€ã® JSP ããŒãžã¯ 1 ã€ã® Servlet ã«ç¿»èš³ããããã®å 容ã¯ãœãŒã¹ãã¡ã€ã«ã¬ãã«ã§çµ±åãããŸããjspã¿ã°ã§ã include ãã£ã¬ã¯ãã£ãã§ãã2 ã€ã® JSP ããŒãžã®å 容ãçµ±åãããŠåºåããããããããã 2 ã€ã®ããŒãžã«ã¯éè€ãã HTML ã°ããŒãã«æ§é ã¿ã°ãååšããªãããã«ããå¿ èŠããããŸããããã§ãªããšãã¯ã©ã€ã¢ã³ãã«åºåãããå 容ã¯ãã©ãŒããããä¹±ãã HTML ææžã«ãªããŸãããã ããinclude ãã£ã¬ã¯ãã£ã㯠jspã¿ã°ãããå¹çãé«ãã§ãã以äžã®è¡šãããäž¡è ã®éããããçŽæçã«ç¢ºèªã§ããŸãã
include jsp æ§æåœ¢åŒ <%@ include file="âŠ" %> <jsp page="âŠ" > å¹åãçºçããæé ããŒãžå€æäž ãªã¯ãšã¹ãäž å«ãŸããå 容 ãã¡ã€ã«ã®å®éã®å 容 ããŒãžã®åºå å€æããã Servlet äž»ããŒãžãšå«ãŸããããŒãžã 1 ã€ã® Servlet ã«å€æããã äž»ããŒãžãšå«ãŸããããŒãžãç¬ç«ãã Servlet ã«å€æããã ã³ã³ãã€ã«æé æ¯èŒçé ã - ãªãœãŒã¹ã解æããå¿ èŠããã æ¯èŒçéã å®è¡æé ããéã ããé ã - æ¯åãªãœãŒã¹ã解æããå¿ èŠããã -
jspïŒãã®ã¿ã°ã¯ãªã¯ãšã¹ããå¥ã®ãªãœãŒã¹ã«è»¢éããããã«äœ¿çšãããŸãã
<jsp:forward page="relativeURL | <%=expression%>" />
ãããã¯ãrequest.getRequestDispatcher("/url").forward(request,response)
ãšåçã§ããã€ãŸãããµãŒããŒå éšã®ãžã£ã³ãæäœãå®è¡ããŸãã -
jspïŒjspããã³ jspã¿ã°ã䜿çšããŠä»ã®ãªãœãŒã¹ãåŒã蟌ããŸãã¯ãªã¯ãšã¹ãã転éããéã«ãjspã¿ã°ã䜿çšããŠãã®ãªãœãŒã¹ã«ãã©ã¡ãŒã¿ãæž¡ãããšãã§ããŸãã
-
<jsp:include page="relativeURL | <%=expression%>">
<jsp:param name="parameterName" value="parameterValue|<%= expression %>"/>
</jsp:include>
è€æ°ã®<jsp:param>ã¿ã°ã䜿çšããŠè€æ°ã®ãã©ã¡ãŒã¿ãæž¡ãããšãã§ããŸãã
- JSP ã®ãããã³ã°
JSP ã®ãããã³ã°ãšã¯ãJSP ãä»ã®ä»»æã®åœ¢åŒã® URL ã«ãããã³ã°ããããšãæå³ããweb.xml ãã¡ã€ã«ã§èšå®ããŸãã
<servlet>
<servlet-name>SimpleJspServlet</servlet-name>
<jsp-file>/jsp/simple.jsp</jsp-file>
<load-on-startup>1</load-on-startup>
</servlet>
âŠâŠ
<servlet-mapping>
<servlet-name>SimpleJspServlet</servlet-name>
<url-pattern>/xxx/yyy.html</url-pattern>
</servlet-mapping>
JavaBeans#
-
JavaBean ã®æŠèŠ
JavaBeans ã¯ãç¹å®ã®èŠç¯ã«æºæ ãã Java ã¯ã©ã¹ã§ããJavaBeans ã䜿çšããå©ç¹ã¯ãã³ãŒãã®éè€ã解決ããã³ãŒãã®åé·æ§ãæžãããæ©èœã®åºåãæ確ã«ããã³ãŒãã®ä¿å®æ§ãåäžãããããšã§ãã
-
JavaBean ã®èšèšåå
å ¬éã¯ã©ã¹ã§ããå¿ èŠããããŸãïŒ
åŒæ°ãªãã®å ¬éã³ã³ã¹ãã©ã¯ã¿ãå«ãå¿ èŠããããŸãïŒ
ãã¹ãŠã®å±æ§ã¯ãã©ã€ããŒãã§ããå¿ èŠããããŸãïŒ
getter ããã³ setter ã¢ã¯ã»ãµã䜿çšããŠå±æ§ãžã®ã¢ã¯ã»ã¹ãã«ãã»ã«åããŸããpublic class Book { //èŠä»¶ãæºããJavaBeanã¯ã©ã¹ private String bookName; private String author; private double price; public Book() { } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } }
-
JSP ã§ã® JavaBean ã®äœ¿çšæ¹æ³
第äžã®æ¹æ³ïŒéåžžã® Java ã¯ã©ã¹ã®ããã« JavaBean ã€ã³ã¹ã¿ã³ã¹ãäœæããŸãã
第äºã®æ¹æ³ïŒJSP ããŒãžå ã§éåžžãjsp ã¢ã¯ã·ã§ã³ã¿ã°ã䜿çšã㊠JavaBean ã䜿çšããŸãã -
ã¹ã³ãŒã
äœçšïŒJSP ããŒãžå 㧠JavaBean ãã€ã³ã¹ã¿ã³ã¹åãŸãã¯æå®ãããç¯å²å ã§äœ¿çšããŸãã
<jsp:useBean id="èå¥å" class="javaã¯ã©ã¹å" scope="äœçšç¯å²"/> <!-- scopeã®ããã©ã«ãã¯pageã§ãã -->
-
ããããã£
äœçšïŒæ¢ã«ã€ã³ã¹ã¿ã³ã¹åããã JavaBean ãªããžã§ã¯ãã®å±æ§ã«å€ãèšå®ããŸãã4 ã€ã®åœ¢åŒããããŸãïŒ
<jsp:setProperty name="JavaBeanã€ã³ã¹ã¿ã³ã¹å" property="*"/>ïŒãã©ãŒã ãšé¢é£ä»ãïŒ
<jsp:setProperty name="JavaBeanã€ã³ã¹ã¿ã³ã¹å" property="JavaBeanå±æ§å"/>ïŒãã©ãŒã ãšé¢é£ä»ãïŒ
<jsp:setProperty name="JavaBeanã€ã³ã¹ã¿ã³ã¹å" property="JavaBeanå±æ§å" value="BeanValue"/>ïŒæåèšå®ïŒ
<jsp:setProperty name="JavaBeanã€ã³ã¹ã¿ã³ã¹å" property="propertyName" param="requestãªããžã§ã¯ãå
ã®ãã©ã¡ãŒã¿å"/>ïŒrequestãã©ã¡ãŒã¿ãšé¢é£ä»ãïŒURLã¢ãã¬ã¹ãéããŠæž¡ããããã©ã¡ãŒã¿
- name
äœçšïŒæå®ããã JavaBean ãªããžã§ã¯ãã®å±æ§å€ãååŸããŸãã
<jsp:getProperty name="JavaBeanã€ã³ã¹ã¿ã³ã¹å" property="å±æ§å"/>
-
JavaBean ã® 4 ã€ã®ã¹ã³ãŒãç¯å²
useBeans ã® scope å±æ§ã䜿çšã㊠JavaBean ã®ã¹ã³ãŒããæå®ã§ããŸãã
pageïŒçŸåšã®ããŒãžã§ã®ã¿æå¹
requestïŒrequest.getAttribute ã¡ãœãããéã㊠JavaBean ãªããžã§ã¯ããååŸã§ããŸãã
sessionïŒsession.getAttribute ã¡ãœãããéã㊠JavaBean ãªããžã§ã¯ããååŸã§ããŸãã
applicationïŒapplication.getAttribute ã¡ãœãããéã㊠JavaBean ãªããžã§ã¯ããååŸã§ããŸãã