作為一個java web開發程式員,手工建立一個Web工程應該是必須會的。 首先我們必須了解Java web工程的基本機構和每個部分究竟完成什麼樣的作用。
1. 建立一個檔案,比如MyFirstWebProject, 此為web工程的根目錄
2. 在其下建立WEN-INF檔案夾,必須注意的是要大寫, 因為這是規範,友善WEB容器的讀取。
3. 在WEB-INF下建立兩個檔案夾和一個檔案,分别叫做:lib、classes檔案夾和web.xml檔案。 lib用于存放需要用到的jar包等,包括mysql驅動等;classes檔案夾用于存放編譯好了的java檔案,它們是以class檔案存在在這個目錄下的;最後是web.xml檔案,它必須以<web-app>作為該xml檔案的根标簽。
4. 在工程MyFirstWebProject目錄下建一個檔案夾pages(這不是必須的)用于存在index.html等頁面,把需要的頁面寫好放在該檔案夾下面就可以了。
5. 将整個工程打成war包,放入tomcat或jboss等web容器的webapp目錄下,啟動容器,則通過http://localhost:8080/MyFirstWebProject/pages/index.html就可正常通路html頁面了。 打war可以在進入工程MyWebProject中,用"jar -cvf MyFirstWebProject.war ."即可。
6. 在Tomcat6.0上經過測試,以上的做法是正确的。 同時注意, 如果沒有配置JAVA_HOME的環境變量,直接點選Tomcat6.0會迅速消失, 此時應該用指令行啟動。
2、Web常用方法
【注意】action是以Web程式的根目錄作為相對參照物,“../servletForm”表示在本頁面上面的一級目錄來找到servletForm, 而"/servletForm"則以在web.xml中的配置為參考,它是在'/'根下面。
<form action="../servletForm" method="get">
username: <input type="text" name="uname">
<input type="submit" value="送出">
</form>
(1)頁面Form表單
index.html
<form method="get" action="handle">
<table>
<tr>
<th><label for="usercode">工号</label></th>
<td><input type="text" id="usercode" name="usercode" /></td>
</tr>
<th><label for="ip">機器IP</label></th>
<td><input type="text" id="ip" name="ip" /></td>
<th><label for="reason">申請原因</label></th>
<td><input type="text" id="reason" name="reason" /></td>
<th><label for="submit"></label></th>
<td>
<button type="submit" id="submit" name="submit">submit</button>
<label for="reset"></label>
<button type="reset" id="reset" name="reset">reset</button>
</td>
</table>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>server.manager</display-name>
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.mortbay.jetty.servlet.DefaultServlet</servlet-class>
<init-param>
<param-name>useFileMappedBuffer</param-name>
<param-value>false</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-name>HandleServlet</servlet-name>
<servlet-class>com.alibaba.servermanager.HandleServlet</servlet-class>
<servlet-mapping>
<url-pattern>/handle</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
(2)Servlet處理及跳轉
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
public class HandleServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String usercode = request.getParameter("usercode");
HttpSession session = request.getSession();
session.setAttribute("usercode", usercode);
System.out.println("usercode: " + usercode);
response.sendRedirect("success.jsp");
}
或者使用request來傳遞資料:
request.setAttribute("hello", "Hello World");
request.getRequestDispatcher("/jstl_el.jsp").forward(request, response);
(3) jsp接收資料
<body>
susercode: <%=session.getAttribute("usercode") %>
</body>
或在jstl_el.jsp頁面中通過request方式取出參數
<li>普通字元串</li><br>
hello(jsp腳本):<%=request.getAttribute("hello") %><br>
hello(el表達式,el表達式使用$和{}):${hello }<br>
(4)配置tomcat的虛拟目錄
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="" reloadable="true" />
</Host>
Tomcat的conf\server.xml檔案中上面配置 context的reloadable屬性的時候,設定了path和docBase,
如果說path是Tomcat的wabapps目錄下的項目名稱,那麼docBase是設定的那個路徑呢?
答疑:
path對應的是我們每次在通路一個網站的時候在浏覽器上輸入的虛拟目錄路徑,而伺服器上的具體的對應的目錄就是docBase。實際上二者是一個映射過程。
<Context path="/helloworld" docBase="d:\demoes\webdemo\WebRoot" reloadable="true" />
則可通過http://localhost:8080/helloworld/index.jsp來通路目錄d:\demoes\webdemo\WebRoot下面的index.jsp頁面。
============================================================
問題1:幾秒之後自動跳轉
2秒之後自動跳轉,示例如下:
<meta http-equiv="refresh" content="2;URL=/page/frame/main.html">
或者
<script type="text/javascript">
location="/page/frame/main.html"
</script>
問題2:在jetty中允許修改頁面,不鎖定,在web.xml中配置如下:
<servlet>
本文轉自 tianya23 51CTO部落格,原文連結:http://blog.51cto.com/tianya23/245986,如需轉載請自行聯系原作者