上一節介紹了Eclipse中集成Tomcat環境搭建及javaweb項目的創建,下面說說什么是web服務器及javaweb的結構。
1.web應用的演變
1.1 b/s與c/s模式
B/S:Browser/Server或瀏覽器/服務器模式
優點是用戶使用簡單只要有瀏覽器和網絡即可
常見的程序:淘寶網、京東網等
C/S:Client/Server或客戶端/服務器模式
C/S的優點是能充分發揮客戶端PC的處理能力
缺點是對用戶的電腦配置要求較高
常見的CS程序:LOL、穿越火線、QQ
1.2 web服務器簡介
web服務器有多種只需要知道下面三個就行
2 javaweb項目的結構
2.1Javaweb項目結構講解
Java web工程下的webapp或WebContent就是工程的發布文件夾,發布時會把該文件夾發布到tomcat的webapps里。
開發時classes文件存放路徑:
buildpath:在eclipse中項目的右鍵java build path-source中指定工程中class文件的編譯路徑,一般為:test/build/classes。(test是工程名)
發布時classes文件存放路徑:
發布到tomcat時(在eclipse里啟動tomcat),src文件夾里的java文件經過編譯后,會把.class文件放在WEB-INF文件夾里的classes文件夾中。
有一些配置文件需要放到WEB-INF的classes文件夾下,所以,通常的做法是手動在工程的WEB-INF文件夾下建立classes文件夾。如果不在工程的WEB-INF下手動建立classes,發布到tomcat時tomcat里的WEB-INF中也會有classes文件夾。
2.2 web的jar
Web App Libraries:一般是指web工程的web-inf/lib下的包(可以將此包cope到此目錄下 然后刷新工程 加入的包一般能自動找到 如果找不到 右鍵|Properties | java Build Path | Libraries | Add Jars 可以加入)
build path中add jar可以引用工程以外的jar,
web工程參照了很多文件,因此發布的時候要打成war包,使其變成單獨的個體,放到web容器里。開發環境中eclipse會將工程打包放到tomcat下,進行關聯。
2.3 web.xml
<display-name>Tomcat Example</display-name>
2.3.2.Web 應用描述:給出於此相關的說明性文本
<desciption>Tomcat Example servlets and JSP pages.</desciption>
2.3.3.上下文參數:聲明應用范圍內的初始化參數
1 <context-param>
2 <param-name>參數名</para-name>
3 <param-value>參數值</param-value>
4 <description>參數描述</description>
5 </context-param>
在servlet里面可以通過 getServletContext().getInitParameter(“context/param”)得到
2.3.4.過濾器配置:將一個名字與一個實現javaxs.servlet.Filter接口的類相關聯
1 <filter>
2 <filter-name>setCharacterEncoding</filter-name>
3 <filter-class>com.myTest.setCharacterEncodingFilter</filter-class>
4 <init-param>
5 <param-name>encoding</param-name>
6 <param-value>GB2312</param-value>
7 </init-param>
8 </filter>
9 <filter-mapping>
10 <filter-name>setCharacterEncoding</filter-name>
11 <url-pattern>/*</url-pattern>
12 </filter-mapping>
2.3.5.監聽器配置
1 <listener>
2 <listerner-class>org.springframework.web.context.ContextLoaderListener</listener-class>
3 </listener>
2.3.6 Servlet配置
1 <servlet>
2 <servlet-name>servlet名稱</servlet-name>
3 <servlet-class>servlet類全路徑</servlet-class>
4 <init-param>
5 <param-name>參數名</param-name>
6 <param-value>參數值</param-value>
7 </init-param>
8 <run-as>
9 <description>Security role for anonymous access</description>
10 <role-name>tomcat</role-name>
11 </run-as>
12 <load-on-startup>指定當Web應用啟動時,裝載Servlet的次序</load-on-startup>
13 </servlet>
14 <servlet-mapping>
15 <servlet-name>servlet名稱</servlet-name>
16 <url-pattern>映射路徑</url-pattern>
17 </servlet-mapping>
1 <session-config>
2 <session-timeout>120</session-timeout>
3 </session-config>
2.3.8.MIME類型配置
1 <mime-mapping>
2 <extension>htm</extension>
3 <mime-type>text/html</mime-type>
4 </mime-mapping>
2.3.9.指定歡迎文件頁配置
1 <welcome-file-list>
2 <welcome-file>index.jsp</welcome-file>
3 <welcome-file>index.html</welcome-file>
4 <welcome-file>index.htm</welcome-file>
5 </welcome-file-list>
2.3.10.配置錯誤頁面
(1).通過錯誤碼來配置error-page
1 <!--配置了當系統發生404錯誤時,跳轉到錯誤處理頁面NotFound.jsp-->
2 <error-page>
3 <error-code>404</error-code>
4 <location>/NotFound.jsp</location>
5 </error-page>
1 <!--配置了當系統發生java.lang.NullException(即空指針異常)時,跳轉到錯誤處理頁面error.jsp-->
2 <error-page>
3 <exception-type>java.lang.NullException</exception-type>
4 <location>/error.jsp</location>
5 </error-page>
2.3.11.TLD配置
1 <taglib>
2 <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
3 <taglib-location>/WEB-INF/jsp/debug-taglib.tld</taglib-location>
4 </taglib>
如果開發工具一直在報錯,應該把<taglib> 放到 <jsp-config>中
1 <jsp-config>
2 <taglib>
3 <taglib-uri>http://jakarta.apache.org/tomcat/debug-taglib</taglib-uri>
4 <taglib-location>/WEB-INF/pager-taglib.tld</taglib-location>
5 </taglib>
6 </jsp-config>