09-04
28
jsp的request.getParameter获取不到表单参
作者:Java伴侣 日期:2009-04-28
可能原因:
1: form表单中的控件没有定义name属性
2: form表单中的控件被设置成disabled
3: form表单的enctype="multipart/form-data"是一种用于上传文件的表单
解决方法:
对于第三种情况我们直接request.getParameter来取参数值是取不到的
我们可以借用apache的上传组件来解决
用到jar包commons-fileupload-1.1.1.jar
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
*获取各种类型表单的表单参数
*@paramrequest HttpServletRequest请求对像
* @paramparamName 参数名
*@return
*@throwsFileUploadException
*/
publicstatic String getParameterValue(HttpServletRequest request,String paramName) throws FileUploadException{
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart==true){
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List fileItemList = upload.parseRequest(request);
if(fileItemList!=null){
for(Iterator itr=fileItemList.iterator();itr.hasNext();){
FileItem fileItem = (FileItem)itr.next();
if(fileItem.getFieldName().equalsIgnoreCase(paramName)){
return new String(fileItem.getString().getBytes("ISO8859-1"))//中文转码
}
}
}
}else{
return new String(request.getParameter(paramName).getBytes("ISO8859-1"))//中文转码
}
return"";
}
1: form表单中的控件没有定义name属性
2: form表单中的控件被设置成disabled
3: form表单的enctype="multipart/form-data"是一种用于上传文件的表单
解决方法:
对于第三种情况我们直接request.getParameter来取参数值是取不到的
我们可以借用apache的上传组件来解决
用到jar包commons-fileupload-1.1.1.jar
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
/**
*获取各种类型表单的表单参数
*@paramrequest HttpServletRequest请求对像
* @paramparamName 参数名
*@return
*@throwsFileUploadException
*/
publicstatic String getParameterValue(HttpServletRequest request,String paramName) throws FileUploadException{
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(isMultipart==true){
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List fileItemList = upload.parseRequest(request);
if(fileItemList!=null){
for(Iterator itr=fileItemList.iterator();itr.hasNext();){
FileItem fileItem = (FileItem)itr.next();
if(fileItem.getFieldName().equalsIgnoreCase(paramName)){
return new String(fileItem.getString().getBytes("ISO8859-1"))//中文转码
}
}
}
}else{
return new String(request.getParameter(paramName).getBytes("ISO8859-1"))//中文转码
}
return"";
}
评论: 0 | 引用: 0 | 查看次数: 533
发表评论