1. 程式人生 > 實用技巧 >簡單web頁面第一步---表單

簡單web頁面第一步---表單

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 
 5 
 6 
 7 <html>
 8 <head>
 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8
"> 10 <title>Insert title here</title> 11 </head> 12 <body> 13 14 15 <font color="blue" size="4" face="黑體">課程名稱</font> 16 17 <form value="form0" name="form0" method="post" action="bb.jsp"> 18 19 <select 20 onchange="document.all.classname.value=this.options[this.options.selectedIndex].value;
"> 21 <option value="Java">Java</option> 22 <option value="大學物理">大學物理</option> 23 <option value="離散數學">離散數學</option> 24 <option value="資料結構">資料結構</option> 25 <option value="體育">UML</option> 26
</select> <input type="text" name="classname" value="1"> 27 28 <font color="blue" size="4" face="黑體">任課教師</font> 29 30 <select 31 onchange="document.all.teachers.value=this.options[this.options.selectedIndex].value;"> 32 <option value="劉立嘉">劉立嘉</option> 33 <option value="劉丹">劉丹</option> 34 <option value="王建民">王建民</option> 35 <option value="王輝">王輝</option> 36 <option value="楊子光">楊子光</option> 37 </select> <input type="text" name="teachers" value="2"> 38 39 40 41 <font color="green" size="4" face="黑體">上課地點</font> 42 43 <select 44 onchange="document.all.classplace.value=this.options[this.options.selectedIndex].value;"> 45 <option value="一教">一教</option> 46 <option value="二教">二教</option> 47 <option value="三教">三教</option> 48 <option value="基教">基教</option> 49 </select> <input type="text" name="classplace" value="3"> 50 <input type="submit" value="提交"> 51 </form> 52 53 54 55 </body> 56 </html>
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ page language="java" import="java.util.*"%>
 4 <%@ page import="java.io.*,java.lang.*"%>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 9 <title>Insert title here</title>
10 </head>
11 <body>
12     <% request.setCharacterEncoding("utf-8");%>
13     <%
14         File file=new File("d:\\1.txt");
15         out.println(request.getParameter("classname"));
16         String classname0=request.getParameter("classname");
17         String teachers0=request.getParameter("teachers");
18         String classplace0=request.getParameter("classplace");
19         BufferedWriter xie=new BufferedWriter(new FileWriter(file,true));
20         xie.write(classname0+teachers0+classplace0);
21         xie.flush();
22         xie.close();
23 %>
24 </body>
25 </html>

使用form封裝表單,用提交標記提交了表單的內容,使用request類獲取表單資料,使用File類和BufferedOutputStream類輸出資料到指定的檔案。