2011年12月26日星期一

第20天第2节: JSP基础语法(下)

==include: <jsp:include page=...
1. @include : static include, just adding original file in
    <%@include file="content.html"%>

2. jsp:include: dyanmic include, include the file after parsing jsp code. can use parameters. this is a tag
    <jsp:include page="content.html/>

    <jsp:include page="content.jsp>
         <jsp.param name="paraname" value="paravalue"/>
    </jsp:include>

==forwared: <jsp:forward page=...
   <jsp:forward page="content.html">

    <jsp:forward page="content.jsp>
         <jsp.param name="paraname" value="paravalue"/>

    </jsp:forward>

第20天第1节: JSP基础语法(中)

==Page: <%@page contentType= ...%>
1. MIME tells how the webfile is handled by webserver. Defined by conf/web.xml:
    <%@page contentType="application/msword%>

2. MIME tells the character set of this page: <%@page contentType=...%>  
   html file: http-equiv is required
       <META http-equiv=Content-Type content="text/html;charset=gbk">
    jsp file:
       <%@page contentType="text/html;charset=GBK"%> 

==Page: <%@page import= ...%>
   <%@page import="java.sql.*"%>
   <%@page import="java.util.*,java.io.*%>

第19天第4节: JSP基础语法(上)

==comments
1. Type 1: <!-- --> : HTML style, shown in the browser
2. Type 2: //  or  /* ... */ : Java style, not shown in the browser
3. Type 3: <%-- --%> : JSP styple, not shown in the browser

==Scriptlet
1. Type 1: <% %> : define variable and write java code
2. Type 2: <%! %>: define global variables
3. Type 3: <%= %>: output a variable content

    <table border=1>
    <%for(int i=0;i<10;i++) {%>
        <tr>
        <%for(int j=0;j<10;j++) {%>
            <td><%=(i*j)%></td>
        <%}%>
        </tr>
    <%}%>
    </table>

==Dynamically print table
 
demo.html
        <form method='post' action='demo.jsp'>
            <table border=0>
                <tr>
                    <td colspan=2>打印表格</td>
                </tr>
                <tr>
                    <td>行数</td>
                    <td><input type='text' name='rows'</td>
                </tr>
                <tr>
                    <td>列数</td>
                    <td><input type='text' name='cols'</td>
                </tr>
                <tr>
                    <td><input type='submit' value='打印'</td>
                    <td><input type='reset' value='重设'</td>
                </tr>
            </table>
        </form>

demo.jsp
        <%
            int rows=Integer.parseInt(request.getParameter("rows"));
            int cols=Integer.parseInt(request.getParameter("cols"));
        %>
            <table border=1>
            <%for(int i=0;i<rows;i++) {%>
                <tr>
                <%for(int j=0;j<cols;j++) {%>
                    <td><%=(i*j)%></td>
                <%}%>
                </tr>
            <%}%>
            </table>

第19天第3节: MyEclipse 中配置Tomcat

==MyEclispse Installation
    1. MyEclipse is a commercial plugin for Eclipse
    2. Current version of MyEclipse includes Eclipse installation

==Creating and Publishing WebProject in MyEclipse
    1. Create a Java Web Project
        a) it automatically creates the following in the web project dir
            META-INF/
            WEB-INF/web.xml
            hello.jsp
    2. Publish WebProject
        a) Manual, just as in last post
        b) Auto, Let MyEclipse copy your file into Tomcat's webapps dir
           b.1 Set Tomcat dir: Perference->MyEclipse->Application Servers->Tomcat->tomcat<version>
           b.2 Set JDK path: Perference->...->Tomcat->tomcat<version>->JDK: Add JDK
           b.3 Toolbar: Manage deploy: Select Tomcat<version>

==The first interactive web 
      input.htm

        <html>
         <head>
          <title> Input </title>
         </head>
         <body>
           <form method="post" action="input.jsp" onSubmit="validate(this)">
            Input: <input type="text" name="info">
            <input type="submit">
           </form>
         </body>
        </html>

      input.jsp
        <%
        String str = request.getParameter("info");
        out.println(str);
        %>

      input.htm with Javascript to check the input not being blank

        <html>
         <head>
          <title> Input </title>
         </head>
         <script language="javaScript">
           function validate(f) {
              if(/^\s*$/.test(f.info.value)){
                alert("Input could not be empty");
                f.info.focus();
                return false;
              }else{
                return true;
              }
           }
         </script>
         <body>
           <form method="post" action="input.jsp" onSubmit="validate(this)">
            Input: <input type="text" name="info">
            <input type="submit">
           </form>
         </body>
        </html>

第19天第2节: 使用Tomcat搭建WEB开发环境

==Tomcat Install and Configure (http://www.coreservlets.com/Apache-Tomcat-Tutorial/)
-实现了JAVAEE标准的最小WEB服务器, Apache开发。
0. No separate Apache installation needed
1. Installed folders
    C:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0
        bin/ : all tomcat binaries, including exe to start tomcat server
        lib/  : all java jar files used to compile and run java
        conf/ : configure, setting port, map between web dir and local dir
        log/:
        webapp/: all web project files, could be added by user. This is default web file location
        work/ : all temporary files including .java and .class, clean it if needed

2. Set JAVA_HOME pointing to JDK top dir. Tomcat use JD

3. Start tomcat:
     C:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0\bin\Tomcat7.exe
4.  Check the web: http://localhost:8080/

5.  Further configuration
    5.1 (optional) webserver port. by server.xml

         <Connector port="8080" protocol="HTTP/1.1"  ==> port
    5.2 (optional but recommended) set map between web dir and local dir (virtual dir) ,
         a. create a local dir which has the following structure
             <application_root>/WEB-INF/web.xml  ( copy from webapps\ROOT\WEB-INF\web.xml)
         b. configure map on server.xml: Adding the following right before </Host>
                 <Context path="/demo" docBase="E:\linfa\work\webdemo"/>
                
                 Note: path is the path in the web browser, docBase is the path of files
    5.3 (optional but recommended on development) enable list files on dir. web.xml
            Fix: modify conf/web.xml: change "false" to "true"
                       <init-param>
                            <param-name>listings</param-name>
                             <param-value>false</param-value>
                       </init-param>
            restart the server, should have "Directory Listing For /"
    5.4 (optional but recommended on development) enable apps management on tomcat-user.xml: Adding the following
      <role rolename="admin"/>
       <role rolename="manager"/>
       <user username="admin" password="admin" roles="admin,manager,tomcat"/>

==The first jsp program
    1. creating E:\linfa\work\webdemo\hello.jsp  . jst file is adding java code into html file
    <html>
    <head>
        <title> Hello Jsp World!!!</title>
    </head>
    <body>
        <%
            out.println("Hello World with Newline<br>");
            out.println("Hello World without Newline");
        %>
    </body>
    </html>

==Under the hood
    


  1. The first time *.jsp->*.java->*.class->run *.class
  2. The second time *.jsp -> run *.class
  3. the *.java and *.class should be in tomcat work dir ( I could not find it?)