Java 如何使用jquery向portlet发送参数
我的jsp页面上有一个表单。在这个表单中,我选择一个文件zip存档,然后单击submmit call servlet上传这个文件。对于文件上传im,请使用Apache Commons FileUlpoad库。上传后im解压存档。我将它们重新映射到这个jsp jsp代码:Java 如何使用jquery向portlet发送参数,java,jquery,jsp,portlet,Java,Jquery,Jsp,Portlet,我的jsp页面上有一个表单。在这个表单中,我选择一个文件zip存档,然后单击submmit call servlet上传这个文件。对于文件上传im,请使用Apache Commons FileUlpoad库。上传后im解压存档。我将它们重新映射到这个jsp jsp代码: <form action="Upload_Servlet" method="post" enctype="multipart/form-data"> <div id="up"> <
<form action="Upload_Servlet" method="post" enctype="multipart/form-data">
<div id="up">
<input id="fileUpload1" type="file" name="filename1"value="Browse..."/>
</div>
<div>
<input id="btnSubmit" type="submit" value="Загрузить">
<input type="button" id="del" onclick="deleting()" value="Удалить">
</div>
</form>
servlet代码:
public class uploadfile extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
System.out.println(response.getCharacterEncoding());
response.setCharacterEncoding("UTF-8");
System.out.println(response.getCharacterEncoding());
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println("wtpwebapps<br/>");
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
writer.println("<HTML>");
writer.println("<HEAD <TITLE> Upload4 </TITLE> </HEAD>");
writer.println("<BODY>");
writer.println("<FORM action = \"Upload_Servlet\" method = \"post\" enctype = \"multipart/form-data\">");
writer.println("<INPUT type = file name = ufile>");
writer.println("<INPUT type = submit value = \"Attach\">");
writer.println("<h1>its not multipart</h1>");
writer.println("</FORM>");
writer.println("</BODY>");
writer.println("</HTML>");
return;
}
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> list=null;
String mifpath= "1";
String path = " ";
String mif = " ";
String from = "\\\\";
String to ="/";
String error="";
try{
list = upload.parseRequest(request);
Iterator<FileItem> it = list.iterator();
response.setContentType("text/html");
while ( it.hasNext() )
{
FileItem item = (FileItem) it.next();
File disk = new File("C:/uploaded_files/"+item.getName());
path = disk.toString();
String code = new String(path.substring(path.lastIndexOf("."), path.length()).getBytes("ISO-8859-1"),"utf-8");
if (code.equalsIgnoreCase(".zip"))
{
mifpath=path;
mif = mifpath.replaceAll(from, to);
item.write(disk);
error=unzip.unpack(mif, "C:/uploaded_files");
}
else
{
error = "Выбранный файл не является архивом zip";
}
}
}
catch ( Exception e ) {
log( "Upload Error" , e);
}
request.setAttribute("error", error);
request.getRequestDispatcher("/Home.jsp").forward(request, response);
// String redictedURL="http://localhost:8080/redicted_test/Home.jsp";
// response.sendRedirect(redictedURL);
writer.close();
}
}
现在我想在门户网站上执行此操作。这意味着我不想在上传文件后重新加载jsp。所以我必须使用Jquery。我有一些问题:
在我的案例中,如何提交表单以使用jquery?
我的servlet代码将在portlet中工作?
如何从portlet向jps发送参数?
使用Jquery可以很容易地完成: 在提交按钮或表单提交上设置单击事件。 将数据发布到servlet:
$.ajax({
url : base_url + 'Upload_Servlet',
type : "post",
data:$('form').serialize(),
cache : false,
success : function(data) {
//do some stuff
},
error : function(xhr, status, err) {
//do error stuff
},
timeout : 3000
});
//End ajax call
servlet完成后,只要使用响应编写器写回aswer(如果它包含大量数据),我建议以json的形式发送响应,请参阅,然后调用成功回调,您可以对这些数据执行任何操作
重要提示:由于您要提交表单,因此需要使用,以便表单不会实际提交,而是由ajax处理。Tnx for anawer。你能再告诉我一件事吗。在portlet中使用什么更好的文件上载?org.apache.commons.fileupload.portlet或jquery脚本?以及我将在portlet中编写什么来解析jquery发送的数据。关于fileupload,我从未做过,所以我不知道。至于java中的json操作,请使用Google的Gson。如果你喜欢,请标出答案。