这是我的第一个HTML + Servlet 的例子:
1.
F:\Tomcat 5.0\webapps\ROOT\hfservlet
hfservlet.html
<html>
<head><title>JdcSurvey</title></head>
<body>
<form action="/HFServlet" method=POST>
<input type=hidden name=survey value=Survey01Results>
<BR><BR>How Many Employees in your Company?<BR>
<BR>1-100<input type=radio name=employee value=1-100>
<BR>100-200<input type=radio name=employee value=100-200>
<BR>200-300<input type=radio name=employee value=200-300>
<BR>300-400<input type=radio name=employee value=300-400>
<BR>500-more<input type=radio name=employee value=500-more>
<BR><BR>General Comments?<BR>
<BR><input type=text name=comment>
<BR><BR>What IDEs do you use?<BR>
<BR>JavaWorkShop<input type=checkbox name=ide value=JavaWorkShop>
<BR>J++<input type=checkbox name=ide value=J++>
<BR>Cafe'<input type=checkbox name=ide value=Cafe'>
<BR><BR><input type=submit><input type=reset>
</form>
</body>
</html>
2.
F:\Tomcat 5.0\webapps\ROOT\WEB-INF\classes\hfservlet
HFServlet.java
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HFServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
// 首先设置响应的 "content type" 头部
res.setContentType("text/html");
//得到响应的 PrintWriter以返回文本给客户端.
PrintWriter toClient = res.getWriter();
String resultsDir = "F:Tomcat 5.0/webapps/ROOT/hfservlet";
try {
//打开一个文件写入Survey的结果.
//String surveyName = req.getParameterValues("survey")[0];
//FileWriter resultsFile = new FileWriter(resultsDir + System.getProperty("file.separator")
//+ surveyName + ".txt", true);
//File myFile = new File(resultsDir + System.getProperty("file.separator")
//+ "survey.txt");
//if( !(myFile.exists()) ){
// 从客户端得到表单数据 & 存贮在这个文件中
FileWriter resultsFile = new FileWriter(resultsDir + System.getProperty("file.separator")
+ "survey.txt", true);
PrintWriter toFile = new PrintWriter(resultsFile);
toFile.println("");
Enumeration values = req.getParameterNames();
while(values.hasMoreElements()) {
String name = (String)values.nextElement();
String value = req.getParameterValues(name)[0];
if(name.compareTo("submit") != 0) {
toFile.println(name + ": " + value);
} //if(name.compareTo("submit") != 0)
} //while(values.hasMoreElements())
toFile.println("");
resultsFile.close();
//} //if
/*
else{
RandomAccessFile RASaveFile = new RandomAccessFile(resultsDir +
System.getProperty("file.separator")
+ "survey.txt", "rw");
RASaveFile.writeBytes("\n");
Enumeration values = req.getParameterNames();
while(values.hasMoreElements()) {
String name = (String)values.nextElement();
String value = req.getParameterValues(name)[0];
if(name.compareTo("submit") != 0) {
RASaveFile.writeBytes(name + ": " + value);
}
}
RASaveFile.writeBytes("\n");
RASaveFile.close();
}
*/
//关闭文件.
//resultsFile.close();
//RASaveFile.close();
// 用一个thank you返回客户端
toClient.println("");
toClient.println("");
toClient.println("Thank you for participating");
toClient.println("");
} catch(FileNotFoundException ex){
ex.printStackTrace();
toClient.println( "File Not Found, Please try again.");
}
catch(IOException e) {
e.printStackTrace();
toClient.println( "A problem occured while recording your answers. "
+ "Please try again.");
}
// 关闭writer; 响应完成.
toClient.close();
}
}
3.
F:\Tomcat 5.0\webapps\ROOT\hfservlet
survey.txt(运行后自动生成)
employee: 100-200
ide: Cafe'
survey: Survey01Results
comment: 211
employee: 100-200
ide: Cafe'
survey: Survey01Results
comment: 211
employee: 500-more
ide: JavaWorkShop
survey: Survey01Results
comment: 51
本文地址:http://com.8s8s.com/it/it11454.htm