JDK(Java開發工具包)下載和安裝 Http://www.oracle.com/index.htmlhtml
設置JDK環境變量:計算機→屬性→高級系統設置→環境變量→系統變量中的新建,變量名:JAVA_HOME,變量值:JDK所在路徑,雙擊系統變量中的Path,在原有的變量值最前面添加.;%JAVA_HOME%\bin;java
確認環境變量是否設置成功:點開始,在運行中輸入cmd,點擊肯定,在出現的界面中輸入javac。git
Tomcat服務器下載、安裝 Http://tomcat.apache.orgweb
tomcat→bin→startup.batapache
確認Tomcat是否配置成功:瀏覽器中地址欄輸入:http://localhost:8080windows
Eclipse下載、安裝 http://www.eclipse.org瀏覽器
如下爲建立一個名稱爲first的動態網站項目示例以及代碼註釋:tomcat
在WebContent中的WEB-INF建立一個名爲NewFile.xml的xml文件,代碼註釋以下安全
<?xml version="1.0" encoding="UTF-8"?> <!-- 說明是XML文檔,指定其版本爲1.0,編碼格式爲UTF-8 --> <!-- 定義XML文檔的根元素web-app,並設置其屬性xmlns:xsi、xmlns、xmlns:web、xsi:schemaLocation、id、version--> <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>servlet</display-name> <!-- welcome-file-list元素可指定顯示的默認文件 --> <welcome-file-list> <!-- 若是用戶鍵入的URL不包含servlet名稱、JSP頁面或其餘資源,則找不到index.html、index.htm文件,這時就會顯示index.jsp文件 --> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <!-- 若是用戶鍵入的URL不包含servlet名稱、JSP頁面或其餘資源,則找不到default.html、default.htm文件,這時就會顯示default.jsp文件 --> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 註冊兩個servlet,Sample和JspSample --> <servlet> <!-- 描述信息 --> <description>第一個例子</description> <!-- servlet顯示的的名稱爲servletSample --> <display-name>servletSample</display-name> <!-- servlet的名稱爲Sample --> <servlet-name>Sample</servlet-name> <!-- servlet類的完整類名jp.co.nttdata.servlet.Sample,鏈接到Sample這個類 --> <servlet-class>jp.co.nttdata.servlet.Sample</servlet-class> </servlet> <!-- Sample印射 --> <servlet-mapping> <!-- servlet的名稱爲Sample --> <servlet-name>Sample</servlet-name> <!-- 訪問Sample的URL地址爲/servlet/Sample --> <url-pattern>/servlet/Sample</url-pattern> </servlet-mapping> <servlet> <!-- 描述信息 --> <description>第二個例子</description> <!-- servlet顯示的的名稱爲servletJspSample --> <display-name>servletJspSample</display-name> <!-- servlet的名稱爲JspSample --> <servlet-name>JspSample</servlet-name> <!-- servlet類的完整類名jp.co.nttdata.servlet.JspSample --> <servlet-class>jp.co.nttdata.servlet.JspSample</servlet-class> </servlet> <!-- JspSample印射 --> <servlet-mapping> <!-- servlet的名稱爲JspSample --> <servlet-name>JspSample</servlet-name> <!-- 訪問JspSample的URL地址爲/servlet/JspSample --> <url-pattern>/servlet/JspSample</url-pattern> </servlet-mapping> </web-app>
在WebContent中添加兩個JSP文件,名稱爲index.jsp、response.jsp,固然這兩個文件也能夠建在WebContent下的單獨的文件夾中,代碼註釋以下服務器
<!-- 使用頁面指令page,並指定其語言爲java,contentType屬性中MIME爲text/html 字符編碼爲windows-31j,編碼格式爲windows-31j --> <%@ page language="java" contentType="text/html; charset=windows-31j" pageEncoding="windows-31j"%> <!-- 文檔類型爲html --> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <!-- 存放HTML文檔信息 --> <head> <!-- 用meta指定了文件爲html文件,且使用windows-31j編碼 --> <meta http-equiv="Content-Type" content="text/html; charset=windows-31j"> <!-- 文檔的標題爲Insert title here --> <title>Insert title here</title> </head> <!-- 頁面中的內容 --> <body> <!-- 在頁面中插入表單,表單名稱爲 loginForm,表單提交的URL地址爲/servlet/servlet/JspSample,表單提交方式爲POST,POST方式更安全--> <form name="loginForm" action="/servlet/servlet/JspSample" method="POST"> <!-- 頁面可直接顯示出此內容 --> index <!-- 表單輸入標記,能夠向頁面添加單行、多行文本或按鈕等 --> <!-- 此處 --> <input name="loginId" type="text" /> <input name="loginName" type="text" value="${loginName}"/> <input type="submit" value="sub" /> </form> </body> </html>
<!-- 註釋與index.jsp頁面相似 --> <%@ page language="java" contentType="text/html; charset=windows-31j" pageEncoding="windows-31j"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-31j"> <title>Insert title here</title> </head> <body> <form name="loginForm" action="/servlet/servlet/JspSample" method="POST"> response <input name="loginId" type="text" /> <input name="loginName" type="text" value="${loginName}"/> <input type="submit" value="sub" /> </form> </body> </html>
在Java Resource中的src中新建兩個Servlet,名稱爲Sample.java、JspSample.java,代碼註釋以下
//打包成名稱爲jp.co.nttdata.servlet package jp.co.nttdata.servlet; //倒入要用到的這些包 import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //繼承 HttpServlet的公共類Sample public class Sample extends HttpServlet { /** * serialVersionUID */ /* * 根據類名、接口名、成員方法及屬性等來生成一個64位的哈希字段,若是你沒有考慮到兼容性問題時,就把它關掉,Window ==> * Preferences ==> Java ==> Compiler ==> Error/Warnings ==> Potential * programming problems 將Serializable class without * serialVersionUID的warning改爲ignore便可 */ private static final long serialVersionUID = -1936901196555528367L; // Servlet 初始化,輸出This is // initMethod,默認初始有參初始化法,若是沒有有參初始化方法,才調用無參初始化。若是傳遞ServletConfig對象失敗,則會發生ServletException異常,Servlet將不能正常工做 public void init() throws ServletException { System.out.println("This is initMethod"); } // 處理GET類型的Http請求。繼承HttpServlet的servlet必須有public void // doGet(HttpServletRequest req,HttpServletResponse resp)方法 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 輸出This is doGet System.out.println("This is doGet"); // 設置resp的contentType屬性中MIME爲text/html 字符編碼爲UTF-8 resp.setContentType("text/html;charset=UTF-8"); // 從resp中獲取PrintWriter對象 PrintWriter out = resp.getWriter(); // 向客戶端發送字符數據 out.println("<html>"); out.println("<body>"); out.println("This is Servlet"); out.println("</body>"); out.println("</html>"); } // 處理POST類型的Http請求,若是失敗,則拋出ServletException, IOException異常 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 調用doGet( )方法 doGet(req, resp); } // 銷燬servlet對象,釋放資源 public void destroy() { // 釋放HttpServlet類佔用的資源 super.destroy(); // 輸出This is destroy System.out.println("This is destroy"); } }
//註釋相似Sample.java package jp.co.nttdata.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class JspSample extends HttpServlet { /** * serialVersionUID */ private static final long serialVersionUID = 1751356779488685371L; public void init() throws ServletException { System.out.println("This is initMethod"); } public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("This is doGet"); resp.setContentType("text/html;charset=UTF-8"); // 獲取loginId參數值 String loginId = req.getParameter("loginId"); // 將loginId放置於req範圍中 req.setAttribute("loginId", loginId); // 將loginName更改成nttdata req.setAttribute("loginName", "nttdata"); // 獲取RequestDispatcher對象,請求轉發到/response.jsp頁面 req.getRequestDispatcher("/response.jsp").forward(req, resp); } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } public void destroy() { super.destroy(); System.out.println("This is destroy"); } }
注意事項:從安全性出發,WebContent的META-INF、WEB-INF在網頁上(前臺)是禁止訪問的。代碼中的name後臺使用,type前臺展現,value後臺傳給前臺的值。全部地址欄都是經過GET方法提交的,GET方法提交最高只能有250個字符,POST則沒有限制。完整代碼地址:https://git.oschina.net/lcxx/first01.git