Servlet/JSP
1 1 3 1.1 Web............................ 3 1.2 Servlet/JSP.................................. 3 2 JSP 7 2.1................................... 7 2.2........................................ 7 2.3.................................. 8 2.4.......................................... 8 2.5 EL......................................... 9 2.6..................................... 10 2.7....................................... 10 3 13 3.1 page......................................... 13 3.2 request........................................ 13 3.3 session........................................ 14 3.4 application...................................... 14 4 15 4.1 out.......................................... 15 4.2 request........................................ 16 4.3 session........................................ 17 4.4 application...................................... 18 4.5 response....................................... 19 5 Tomcat 21 5.1.................................. 21
2 5.2 Servlet/JSP.............................. 22 5.3 web.xml....................................... 23 5.4 context.xml..................................... 24
3 1 1.1 Web Web Web. Web HTTP, HTML XML. Web, /. Web,., (HTML XML ). 1.2 Servlet/JSP Web Java. Servlet Java HTML, JSP HTML Java.
4 1 public class SampleServlet extends HttpServlet { public void doget(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException { response.setcontenttype( text/html; charset=utf-8 ); PrintWriter out = response.getwriter(); out.println( <html> ); out.println( <head> ); out.println( <title>loop </title> ); out.println( </head> ); out.println( <body> ); for (int i=1; i<=5; i++) { out.print( <p> ); out.print( i ); out.println( </p> ); } out.println( </body> ); out.println( </html> ); } } Servlet @ page language= java @ page contenttype= text/html; charset=utf-8 <html> <head> <title>loop </title> </head> <body> for (int i=1; i<=5; i++) { <p>= i </p> } </body> </html> JSP <html> <head> <title>loop </title> </head> <body> <p>1 </p> <p>2 </p>
1.2. Servlet/JSP 5 <p>3 </p> <p>4 </p> <p>5 </p> </body> </html> HTML
7 2 JSP HTML.jsp JSP ( ). Java,. 2.1 JSP.. @ 1= 1 2= 2...., UTF-8 HTML. @ page contenttype= text/html; charset=utf-8 2.2!,,.. @ page language= java @ page contenttype= text/html; charset=utf-8! // // int count = 0;
8 2 JSP // 1 count++; <html> <head> <title> </title> </head> <body>= count.</body> </html> (test01.jsp) 2.3, JSP Java. JSP, if while.,. @ page language= java @ page contenttype= text/html; charset=utf-8 // // int count = 0; // 1 count++; <html> <head> <title> </title> </head> <body>= count.</body> </html> (test02.jsp) 2.4 = (Expression),.
2.5. EL 9 @ page language= java @ page contenttype= text/html; charset=utf-8 // String str = ; java.util.list<string> list = new java.util.arraylist<string>(); list.add( ); list.add( ); session.setattribute( list, list); java.util.map<string, String> map = new java.util.hashmap<string, String>(); map.put( str1, ); map.put( str2, ); request.setattribute( map, map); <html> <head> <title> </title> </head> <body> [= str ]<br /> [= 2 * 5 ]<br /> [= ((java.util.list<string>)session.getattribute( list )).get(0) ]<br /> [= ((java.util.map<string, String>)request.getAttribute( map )).get( str1 ) ] </body> </html> (test03 1.jsp) 2.5 EL ${ } EL (Expression Language), ( ). JSP2.0. @ page language= java @ page contenttype= text/html; charset=utf-8 // String str = ; pagecontext.setattribute( str, str); java.util.list<string> list = new java.util.arraylist<string>(); list.add( ); list.add( ); session.setattribute( list, list); java.util.map<string, String> map = new java.util.hashmap<string, String>(); map.put( str1, );
10 2 JSP map.put( str2, ); request.setattribute( map, map); <html> <head> <title>el </title> </head> <body> [${ pagescope.str }] [${ str }]<br /> [${ 2 * 5 }]<br /> [${ sessionscope.list[0] }] [${ list[0] }]<br /> [${ requestscope.map.str1 }] [${ map.str1 }] </body> </html> (test03 2.jsp) 2.6.. < 1= 1 2= 2... > </ > < 1= 1 2= 2... />., to.jsp. <jsp:forward page= to.jsp /> 2.7 - - - -,. // /* */,.
2.7. 11 /* out.println( ); */ - - out.println( ); - -
13 3,.,.. 3.1 page JSP. JSP.. 3.2 request HTTP. 1) 2) Servlet JSP 3) HTTP. Servlet JSP page, forward.
14 3 3.3 session PC.,. HTTP, Cookie URL PC,. 3.4 application Web.,.
15 4 JSP... 4.1 out..,.. @ page language= java @ page contenttype= text/html; charset=utf-8 <html> <head> <title> -out-</title> </head> <body> 3.<br /><br /> <br /> out.print( <br /> ); = <br /> </body> </html> (test04.jsp)
16 4 4.2 request, Cookie HTTP. request.. @ page language= java @ page contenttype= text/html; charset=utf-8 <html> <head> <title> -request-</title> </head> <body> <form action=./test06.jsp method= post > <input type= text name= loginname /> <input type= submit value= /> </form> </body> </html> (test05.jsp) @ page language= java @ page contenttype= text/html; charset=utf-8 // request.setcharacterencoding( UTF-8 ); // String name = request.getparameter( loginname ); <html> <head> <title> -request-</title> </head> <body>
4.3. session 17! = name. </body> </html> (test06.jsp) 4.3 session session.. @ page language= java @ page contenttype= text/html; charset=utf-8 // session Integer count = (Integer)session.getAttribute( COUNT ); if(null == count) { count = Integer.valueOf(0); } // 1 count = Integer.valueOf(count.intValue() + 1); // session session.setattribute( COUNT, count); <html> <head> <title> -session-</title> </head> <body> test08.jsp <br /> = count.intvalue(). </body> </html> (test07.jsp)
18 4 @ page language= java @ page contenttype= text/html; charset=utf-8 // session Integer count = (Integer)session.getAttribute( COUNT ); if(null == count) { count = Integer.valueOf(0); } // 1 count = Integer.valueOf(count.intValue() + 1); // session session.setattribute( COUNT, count); <html> <head> <title> -session-</title> </head> <body> test07.jsp <br /> = count.intvalue(). </body> </html> (test08.jsp) 4.4 application application.. @ page language= java @ page contenttype= text/html; charset=utf-8 // application Integer count = (Integer)application.getAttribute( COUNT ); if(null == count) { count = Integer.valueOf(0); } // 1 count = Integer.valueOf(count.intValue() + 1);
4.5. response 19 // application application.setattribute( COUNT, count); <html> <head> <title> -application-</title> </head> <body> test10.jsp <br /> = count.intvalue(). </body> </html> (test09.jsp) @ page language= java @ page contenttype= text/html; charset=utf-8 // application Integer count = (Integer)application.getAttribute( COUNT ); if(null == count) { count = Integer.valueOf(0); } // 1 count = Integer.valueOf(count.intValue() + 1); // application application.setattribute( COUNT, count); <html> <head> <title> -application-</title> </head> <body> test09.jsp <br /> = count.intvalue(). </body> </html> (test10.jsp) 4.5 response. out, out, response. Cookie.
20 4. @ page language= java @ page contenttype= text/html; charset=utf-8 // // ( ) Cookie cook = new Cookie( test key, test value ); cook.setmaxage(-1); // response.addcookie(cook); <html> <head> <title> -response-</title> </head> <body>cookie.</body> </html> (test11.jsp)
21 5 Tomcat Servlet JSP. Jakarta, Apache Tomcat Project. Apache License, Version2.0. Web,. 5.1 Tomcat.. bin Tomcat common conf
22 5 Tomcat logs webapps work JSP java/class, webapps. webapps. 5.2 Servlet/JSP META-INF. WEB-INF,. classes, jar lib, web.xml,. html js, css JSP..,. 1 war jar war..
5.3. web.xml 23 jar cvf c : v : f : D:Y=app1, C:Y=> D: D:Y=> cd app1 D:Y=app1> dir 2010/04/01 10:00 <DIR> META-INF 2010/04/01 10:00 214 index.jsp 2010/04/01 10:00 1,011 file2.jsp 2010/04/01 10:00 <DIR> WEB-INF D:Y=app1> jar cvf app1.war * D:Y=app1> app1.war. 2 webapps war webapps. 3 Tomcat %CATALINA HOME%/bin/startup.bat Tomcat. #%CATALINA HOME% Tomcat 4 http://127.0.0.1:8080/app1/ 5 Tomcat %CATALINA HOME%/bin/shutdown.bat Tomcat. 5.3 web.xml. filter( ) listener(
24 5 Tomcat ), context-param( ). Servlet,. <web-app> <servlet> <servlet-name>testservlet</servlet-name> <servlet-class>package.testservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>testservlet</servlet-name> <url-pattern>/test.do</url-pattern> </servlet-mapping> </web-app> servlet servlet-name Servlet ( ) servlet-class Servlet servlet-mapping servlet-name URL Servlet url-pattern URL, /test.do, package.testservlet( dopost do- Get). 5.4 context.xml Web.. <Context> <! > <Resource name= jdbc/ossmc auth= Container type= javax.sql.datasource factory= org.apache.commons.dbcp.basicdatasourcefactory driverclassname= org.postgresql.driver username= user password= passwd
5.4. context.xml 25 url= jdbc:postgresql://127.0.0.1:5432/db name maxactive= 10 maxidle= 5 maxwait= -1 /> </Context>
27 2.1 http://creativecommons.org/licenses/by/2.1/jp/legalcode