Dynamic Web Project(動態web專案)實現
阿新 • • 發佈:2018-11-09
在建立的java工程中的java resources 的src檔案中新建一個包(package)在包裡建立一個servlet,
在java檔案中
這個位置新增程式碼,如下。
String name = request.getParameter("name"); String school = request.getParameter("school");//獲取html中name為name的請求 System.out.println("姓名::" + name + "學校::" + school);//獲取name為school的請求 response.setContentType("text/html;charset=UTF-8");//設定為utf-8字型 response.setCharacterEncoding("UTF-8"); response.getWriter().append("姓名: ").append(name).append("學校:").append(school);//輸出姓名,學校
在web content目錄下建立folder檔案,新建一個html檔案,在body裡面加入一下程式程式碼:
<form id="form" action="http://localhost:8080/QCBY-1/StudentInfo" metod="GET">
姓名:<input name="name" type="text"/>
學校:<input name="school" type="school"/>
<input id="sub_1" type="submit" value="提交">
</form>
執行servlet,然後執行html網頁:
點選提交,會跳轉網頁
連線伺服器成功,輸出正確