我正在使用jquery.form.js从文件中获取一些值。当我尝试从jsp页面提交该值时,我在查询字符串中获取值。但是我无法获取这些值。 servlet。这是我的查询字符串值
这是我的带有Jquery的HTML页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>
<script type="text/javascript">
$(function() {
$('#fileUploadForm').ajaxForm({
beforeSubmit: ShowRequest,
success: SubmitSuccesful,
error: AjaxError
});
});
function ShowRequest(formData, jqForm, options) {
var queryString = $.param(formData);
var url = document.URL;
alert('BeforeSend method: \n\nAbout to submit: \n\n' +url+"?"+queryString);
return true;
}
function AjaxError() {
alert("An AJAX error occured.");
}
function SubmitSuccesful(responseText, statusText) {
alert("SuccesMethod:\n\n" + responseText);
}
</script>
</head>
<body>
<form id="fileUploadForm" method="post" action="AServlet" enctype="multipart/form-data">
<input type="text" name="filename" />
<input type="file" id="postedFile" name="postedFile" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
protected void doGET(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("------------------");
String a = request.getParameter("filename");
System.out.println(a);
}
}
此处也是Web.xml
<servlet>
<description></description>
<display-name>AServlet</display-name>
<servlet-name>AServlet</servlet-name>
<servlet-class>com.servlet.AServlet</servlet-class>
<servlet-mapping>
<servlet-name>AServlet</servlet-name>
<url-pattern>/AServlet</url-pattern>
</servlet-mapping>
答案 0 :(得分:0)
GET
请求,而不是POST
请求。所以在你的servlet中,你应该在doGet
方法中获取参数。您也可以在doPost
方法中调用doGet
方法。因此,您的所有请求都将以doPost
方法处理。答案 1 :(得分:0)
我找到了原因,因为您使用enctype="multipart/form-data"
提交表单,该表单无法通过request.getParameter(key)
获取字段值。因为这不是正常的纯文本提交。
如果要获取字段值,可以使用SmartUpload
插件或使用servlet上传组件。
以下是代码:
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
Map<String, Object> fields = new HashMap<String, Object>();
try {
Map<String,List<FileItem>> items = upload.parseParameterMap(request);
for (Entry<String, List<FileItem>> item: items.entrySet()) {
for (FileItem fileItem : item.getValue()) {
fields.put(item.getKey(), fileItem.getString());
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
for (Entry<String, Object> field : fields.entrySet()) {
System.out.println(field.getKey() + ":" + field.getValue());
}