网站导航:首页 -> 软件水平考试 -> 软件水平考试指导 -> 实例:Servlet处理POST请求

实例:Servlet处理POST请求

下面介绍一个http servlet处理post方式的例子,见示例14-3。
    【程序源代码】

1	// ==================== program description ==========================
2	// 程序名称:示例14-3 : surveyexample.html
3	// 程序目的:surveyexample源代码
4	// ==============================================================
5	
6	
7	  
8	    
9	  
10	
11	  
12	  

13

14 15
16 17

how many employees in your company?
18
1-100 19
100-200 20
200-300 21
300-400 22
500-more 23

general comments?
24
25

what ides do you use?
26
javaworkshop 27
j++ 28
cafe' 29

30
31
32
33 34


    【程序输出结果】
    在客户端ie浏览的效果如图14-5所示。


图14-5 客户端浏览效果



    服务器端的servlet就是要将客户端填写并发送的表单数据写入一个文件,并且用一个'thank you'信息响应用户。示例14-4就是servlet程序。
    【程序源代码】

1	// ==================== program description ==========================
2	// 程序名称:示例14-4 : surveyservlet.java
3	// 程序目的:处理http post的表单请求
4	// ==============================================================
5	package examples.servlets;
6	import java.io.*;
7	import java.util.*;
8	import javax.servlet.*;
9	import javax.servlet.http.*;
10	
11	public class surveyservlet extends httpservlet 
12	{
13		string resultsdir;
14	  
15	  	public void init(servletconfig config)
16	       	throws servletexception
17	  	{
18	    		super.init(config);
19	    		resultsdir = getinitparameter('resultsdir');
20	    		if (resultsdir == null) {
21	      		enumeration initparams = getinitparameternames();
22	      		system.err.println('the init parameters were: ');
23	      		while (initparams.hasmoreelements()) {
24	        			system.err.println(initparams.nextelement());
25	      		}
26	      		system.err.println('should have seen one parameter name');
27	      		throw new unavailableexception (this,
28	                                      'not given a directory to 
write survey results!');
29	    		}
30	  	}
31	  
32	  	public void dopost(httpservletrequest req, httpservletresponse res)
33	     	throws servletexception, ioexception
34	  	{
35	    		res.setcontenttype('text/html');   
36	    		printwriter toclient = res.getwriter();
37	    
38	    		try {
39	      		//打开文件将调查结果写入文件
40	      		string surveyname = req.getparametervalues('survey')[0];
41	      		filewriter resultsfile = new filewriter(resultsdir
42	                     + system.getproperty('file.separator')
43	                          + surveyname + '.txt', true);
44	      		printwriter tofile = new printwriter(resultsfile);
45	      		tofile.println('');
46	      		enumeration values = req.getparameternames();
47	      		while(values.hasmoreelements()) {
48	        			string name = (string)values.nextelement();
49	        			string value = req.getparametervalues(name)[0];
50	        			if(name.compareto('submit') != 0) {
51	          			tofile.println(name + ': ' + value);
52	        			}
53	      		}
54	      		tofile.println('');  
55	      		//关闭文件
56	      		resultsfile.close();
57	      
58	      		// 响应客户
59	      		toclient.println('');
60	      		toclient.println('');
61	      		toclient.println('

' + 63 '' + 64 'thank you for participating'); 65 toclient.println(''); 66 67 } 68 catch(ioexception e) { 69 e.printstacktrace(); 70 toclient.println('a problem occured while recording your answers. ' + 71 'please try again.'); 72 } 73 74 } 75 }