当前位置: 技术问答>java相关
关于servlet中的dopost()的处理原理问题?
来源: 互联网 发布时间:2015-07-09
本文导语: 各位DX; 请看我的如下代码: 网页代码: JdcSurvey How Many Emploees are there in your Company? 1-100 100-200 200-300 300-400 500-more General Comments? What IDEs do you use? Java WorkShop J++ Cafe s...
各位DX;
请看我的如下代码:
网页代码:
JdcSurvey
How Many Emploees are there in your Company?
1-100
100-200
200-300
300-400
500-more
General Comments?
What IDEs do you use?
Java WorkShop
J++
Cafe
servlet代码:
package untitled1;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class survey extends HttpServlet
{
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private static final String resultsDir="c:\survey";
//Initialize global variables
public void init() throws ServletException
{
}
//Process the HTTP Get request
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
res.setContentType(CONTENT_TYPE);
PrintWriter toClient = res.getWriter();
try
{
String surveyName=req.getParameterValues("survey") [0];
FileWriter resultsFile=new FileWriter(resultsDir+surveyName+".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);
}
}
toFile.println("");
resultsFile.close() ;
toClient.println("");
toClient.println("survey");
toClient.println("");
toClient.println("
toClient.println("");
}
catch(IOException e)
{
e.printStackTrace() ;
toClient.println("A problem occrued while recording your answers.Please tyr again");
}
toClient.close();
//Clean up resources
}
}
执行结果:
employee:200-300 //(我选的)
ide:JavaWorkShop //(我选的)
survey:Survey01Results
comment:ok //(我写入的)
我的问题是:一,在values中是否存储了4个元素:survey,employee,comment,ide?
二:String value=req.getParameterValues(name)[0]中,当当前的name为employee时,由于有5个名字为emploee的单选项,是不是只打印出被选择的项呢?还是由于
name.compareTo("submit")!=0才使得只打印了被选择的项?
三:读取values时是否有先后的顺序?象以上的结果是先employee,再ide.....那么它是随机的还是应该有顺序的打印呢(按网页中name出现的先后次序,象这张网页就应该是:
survey,employee,comment,ide)????
希望各位DX能不吝赐教,学生谢谢了!
请看我的如下代码:
网页代码:
JdcSurvey
How Many Emploees are there in your Company?
1-100
100-200
200-300
300-400
500-more
General Comments?
What IDEs do you use?
Java WorkShop
J++
Cafe
servlet代码:
package untitled1;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class survey extends HttpServlet
{
private static final String CONTENT_TYPE = "text/html; charset=GBK";
private static final String resultsDir="c:\survey";
//Initialize global variables
public void init() throws ServletException
{
}
//Process the HTTP Get request
public void doPost(HttpServletRequest req,HttpServletResponse res) throws ServletException, IOException
{
res.setContentType(CONTENT_TYPE);
PrintWriter toClient = res.getWriter();
try
{
String surveyName=req.getParameterValues("survey") [0];
FileWriter resultsFile=new FileWriter(resultsDir+surveyName+".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);
}
}
toFile.println("");
resultsFile.close() ;
toClient.println("");
toClient.println("survey");
toClient.println("");
toClient.println("
The servlet has received a POST. This is the reply.
");toClient.println("");
}
catch(IOException e)
{
e.printStackTrace() ;
toClient.println("A problem occrued while recording your answers.Please tyr again");
}
toClient.close();
//Clean up resources
}
}
执行结果:
employee:200-300 //(我选的)
ide:JavaWorkShop //(我选的)
survey:Survey01Results
comment:ok //(我写入的)
我的问题是:一,在values中是否存储了4个元素:survey,employee,comment,ide?
二:String value=req.getParameterValues(name)[0]中,当当前的name为employee时,由于有5个名字为emploee的单选项,是不是只打印出被选择的项呢?还是由于
name.compareTo("submit")!=0才使得只打印了被选择的项?
三:读取values时是否有先后的顺序?象以上的结果是先employee,再ide.....那么它是随机的还是应该有顺序的打印呢(按网页中name出现的先后次序,象这张网页就应该是:
survey,employee,comment,ide)????
希望各位DX能不吝赐教,学生谢谢了!
|
1:values中存储的是你提交过来的所有数据。
2:由于是单选,所以只会传过来一个参数,而你根据name也只能得到长度为1的数组。
3:读取的顺序具体没有规范规定,所以不同厂商的实现不同有可能不一样。一般传过来的数据都是按照你的网页自顶向下的顺序存放的。
2:由于是单选,所以只会传过来一个参数,而你根据name也只能得到长度为1的数组。
3:读取的顺序具体没有规范规定,所以不同厂商的实现不同有可能不一样。一般传过来的数据都是按照你的网页自顶向下的顺序存放的。