Friday, January 22, 2016

JSP

Location: /WEB-INF/A.jsp
WEB-INF represents web app inner files, not expose to outside.
To prevent some jsp/html file on the server directly accessed by browsers from URL, we need to put all request sensitive jsp file under WEB-INF folder.
Put other jsp/html file outside WEB-INF, so browsers can directly access those files by URL.

Normally used forward action:
<jsp:forward page="/WEB-INF/login.jsp"></jsp:forward>

When the first time visiting jsp

  1. web server will convert A.jsp to A_jsp.java
  2. A_jsp.java will compile to A_jsp.class

So when the first time visit jsp, response time will be slow


9 objects

  1. out
  2. request
  3. response
  4. session
  5. application   //servletContext
  6. pageContext  //only this jsp scope
  7. exception
  8. page   //this
  9. config   //servletConfig
3 syntax

Command
  1. <%@ page
    contentType="text/html;charset=utf-8"
    language="java"
    import="java.util.*,java.net.*"
    pageEncoding="utf-8"
    session="true"
    buffer="none|8k|"
    autoFlash="true" //autoFlash to client when buffer full
    isThreadSafe = "true"
    errorPage="/error"
    isErrorPage="false"
    %>
    We can use more than one page tag to make page nicer
    <%@ page import="java.util.*" %>
    <%@ page import="java.net.*" %>
  2. <%@ include file="a.jsp" %>  //static include
    //combine two jsp file to one servlet
  3. taglib
Script

  1. <% java code %>
  2. <%=rs.getString("name")>  //output string, no ; at the end
  3. <%! int count=10%>    //servlet scope, member variable
    <%  int count=10; %>    //function scope, local variable
    <%! public int test(String a){
        return a.length();
    }%>

Action

  1. <jsp:forward page="/a.jsp"></jsp.forward>     //forward to page
    <jsp:forward page="/WEB-INF/login.jsp"></jsp:forward>
  2. <jsp:include file=""></jsp:include>    //dynamic include
    //compile each jsp to different servlet first, then combine the output result
Comment
  1. <!-- Comments -->  HTML comment, still output comment content
  2. <%-- Comments --%>  JSP comment, does not output comment content to browser

No comments:

Post a Comment