這篇是稍微筆記一下如何從無到有搞一個 jsp的hello world站 目前先只介紹放在eclipse
首先先把eclipse的server設定起來,需要指定向tomcat資料夾
跑server 出現 資訊: Server startup in 771 ms 就對了 不然有可能會是8080 8009port被站走的可能
要安裝完server才可以匯入jsp專案 因為匯入時要選tomcat來跑 才可以過
新創一個web->Dynamic Web Project 專案 target runtime記得要在apache tomcat下
然後再webcontent下創一個index.jsp檔
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <!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=UTF-8" > <title>my hello world</title> </head> <body> <Form action= "Secend" method= "POST" > 訪客姓名:<input type= "text" name= "visitor" size = "10" ><P/> <%java.util.Date d = new java.util.Date(); %> <input type= "submit" value= "確定" ><P/> <%=d %> </Form> </body> </html> |
然後在webcontent->web-inf下創一個web.xml 內容如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?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>jspExercise</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file> default .html</welcome-file> <welcome-file> default .htm</welcome-file> <welcome-file> default .jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>Secend</display-name> <servlet-name>Secend</servlet-name> <servlet- class >ch01.Secend</servlet- class > </servlet> <servlet-mapping> <servlet-name>Secend</servlet-name> <url-pattern>/Secend</url-pattern> </servlet-mapping> </web-app> <!-- 104 --> |
在webcontent下創一個ch01的資料夾再創一個secend.jsp檔
1 2 3 4 5 6 7 8 9 10 11 12 | <%@ page language= "java" contentType= "text/html; charset=UTF-8" pageEncoding= "UTF-8" %> <!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=UTF-8" > <title>Insert title here</title> </head> <body> ${visitName},您好,<BR> </body> </html> |
最後最重要的java檔來了
在java resources->src下創一個ch01的package然後創一個Secend.java檔
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package ch01; import java.io.IOException; import java.io.UnsupportedEncodingException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Secend extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { // 說明瀏覽器送來之使用者所輸入資料的編碼 request.setCharacterEncoding( "UTF-8" ); // 讀取使用者在 <input name='visitor' ...>標籤內所輸入的資料,放入變數 name內 String name = request.getParameter( "visitor" ); // 將name資料放入request物件內,成為它屬性物件,屬性物件可以讓別的程式共用。 request.setAttribute( "visitName" , name); RequestDispatcher rd = request.getRequestDispatcher( "/ch01/secend.jsp" ); rd.forward(request, response); } catch (UnsupportedEncodingException e) { throw new ServletException(e); } } } |
然後run server就結束了
概念就是我開啟index.jsp然後傳送了一個post資料過去給secend 然後server會去看web.xml去參照找到secend 是對應到哪個java檔案 然後去執行他
而這個檔案(class)需要 extends HttpServlet這個 然後要實作doPost 或是doget去接資料
用request.getParameter去接
接完 資料處理完 通通都丟到request.setAttribute(“visitName”, name);後告訴她request.getRequestDispatcher
就是告訴server這些處理完的資料要送去給誰
然後secend.jsp收到這些變數後 就可以透過EL的方式把變數SHOW出來 就是${visitName} 這樣的東西
結束