Profile image for allen lsy allenlsy
jsp
Language
Java
Tags
html jsp

jsp

1 1. comment 2 <!-- --> 3 /* */ java comment 4 <%-- --%> JSP comment 5 6 2. scriptlet 7 1) <% %>: code 8 2) <%! %>: global variables, functions, classes. CANNOT write any statements 9 <%! 10 public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver" ; 11 public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:MLDN" ; 12 %> 13 <% 14 out.print("<h1>DBDRRIVER = " + DBDRIVER + "</h1>") ; 15 out.print("<h1>DBURL = " + DBURL + "</h1>") ; 16 %> 17 3) <%= %>: variables 18 19 <% 20 out.println("<table>") ; 21 for(int i=0;i<100;i++){ 22 out.println("<tr>") ; 23 for(int j=0;j<100;j++){ 24 out.print("<td>" + (i * j) + "</td>") ; 25 } 26 out.println("</tr>") ; 27 } 28 out.println("</table>") ; 29 %> 30 31 32 3. Page instruction 33 34 1) set MIME 35 <%@page contentType="text/html;charset=GBK"%> 36 37 conf/web.xml 38 <mime-mapping> 39 <extension>htm</extension> 40 <mime-type>text/html</mime-type> 41 </mime-mapping> 42 43 <mime-mapping> 44 <extension>doc</extension> 45 <mime-type>application/msword</mime-type> 46 </mime-mapping> 47 48 2) import instruction 49 <%@page import="java.sql.*"%> 50 51 jar file: in tomcat/common/lib 52 53 54 example: 55 <%! 56 public static final String DBDRIVER = "..."; 57 public static final String DBURL = "..."; 58 ... 59 %> 60 <% 61 Connection conn = null; 62 PreparedStatement pstmt = null; 63 ResultSet rs = null; 64 %> 65 66 4. include instruction 67 1) static include: include the content only, text include 68 <%@include file="content.htm" %> 69 70 2) dynamic include 71 <jsp:include page="content.jsp"> 72 <jsp:param name="ref1" value="HELLO WORLD"/> 73 </jsp:include> 74 75 5. forward instruction 76 <jsp:forward page="demo.jsp" /> 77 browser address will not change. It is server forward. 78 79 80 6. Servlet 81 Servlet put in WEB-INF/classes 82 83 Import requirement: 84 java package: java.util.* 85 java subpackage: java.lang.reflect; 86 java extension: javax.*; 87 java extension subpackage: javax.servlet.*; 88 custom package; 89 90 1) RESPONSE 91 92 PrintWriter out = resp.getWriter(); 93 out.println("<h1><font color=\"red\">Hello World</font></h1>"); 94 out.close(); 95 96 Under J2SE environment, javax.servlet etc. need to be imported. 97 98 99 IMPORTING PACKAGES: 100 Tomcat procedure: jsp -> java -> class 101 In Tomcat, \common\lib has a servlet-api.jar, many classes are inside. 102 Set CLASSPATH, to this jar, then j2se java will check this jar as a library. 103 In Windows, we can copy this jar to jdk ext, so that we don't need to import this jar every time we use it. 104 105 jre\lib\ext\: store all extension jars 106 This is a unsafe folder. 107 108 in jre\lib\security\java.policy, there is 109 "grant codeBase "file:..." 110 111 WEB-INF is the most safe folder in web container, 112 113 CONFIG SERVLET 114 <servlet> 115 <servlet-name>hello</servlet-name> 116 <servlet-class>org.allen.HelloServlet</servlet-class> 117 </servlet> 118 <servlet-mapping> 119 <servlet-name>hello</servlet-name> 120 <url-pattern>/demo</url-pattern> 121 </servlet-mapping> 122 123 ex. 124 /demo 125 HelloServlet called. 126 127 2) REQUEST 128 String param = req.getParameter("param"); 129 130 <ex1> 131 web.xml: 132 servlet-name: input 133 url-pattern: /input (this is under root directory) 134 the form that post to /input is /serdemo/index.htm 135 <form action="input" method="post"> 136 137 *ClassNotFoundException: recompile servlet class 138 servlet 路径是表单和url拼凑出来的 139 建议在web.xml中url-pattern中填写完整路径 140 141 doPost(req, resp) 142 { 143 this.doGet(req, resp); 144 } 145 146 147 3) LifeCycle 148 HttpServlet 149 - init() 150 - init(ServletConfig conf) 151 conf.getInitParameter(); 152 153 in web.xml, <init-param> <param-name></> <param-value></> </> 154 - service() 155 if service() is override, then doPost(), doGet() will not be run, because they all in a abstract service() in its father class. 156 Container -> service() -> doXXX() 157 - destroy() 158 159 in web.xml, if a servlet is declared <load-on-startup>1</> then it will start on server start up. 160 161 4) get other global object 162 HttpSession getSession() 163 164 5) redirect 165 CLIENT REDIRECT 166 resp.sendRedirect() is a client redirect. Address in browser will change. 167 If pass attribute to redirect, use Session object is the best way 168 169 req.getSession().setAttribute("name", "Hello world"); 170 171 If attribute used only one time, use request 172 173 SERVER REDIRECT( frequently used) 174 RequestDispatcher 175 176 req.setAttribute("name", "hello world"); 177 RequestDispatcher rd = req.getRequestDispatcher("demo.jsp"); 178 rd.forward(req, resp);

Comments