这个没什么难度,其实说到底jsp也是一个servlet,只是我们不建议把业务的处理以及控制放在jsp里面,但是有时候项目中没办法,
必须得用到所以只能用jsp处理
下面是具体代码
初始的jsp中:
<body>
<form action="" id="form1"
enctype="multipart/form-data" method="post">
文本1:
<input type="text" name="text1" value="文本1">
<br>
文件2:
<input type="text" name="text2" value="文本2">
<br>
文件1:
<input type="file" name="file1">
<br>
文件2:
<input type="file" name="file2">
<br>
文件3:
<input type="file" name="file3">
<br>
<input type="button" οnclick="tijiao()" value="开始上传">
</form>
</body>
js代码:
function tijiao(){
$.ajax( {
type : "POST",
url : "showImg.jsp",
data: $('#form1').serialize(), //整个表单提交
success : function(msg) {
alert(111)
}
});
}
所要跳转的jsp:
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
response.getWriter().print("true");//这段代码是用于跳回原来页面的
%>
<html>
<head>
<title>My JSP 'showImg.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="../js/jquery-1.7.2.min.js"></script>
</head>
<body>
<div id="imgDiv">
<img id="img1" src=''></img><br>
<img id="img2" src=''></img><br>
<img id="img3" src=''></img><br>
</div>
</body>
</html>
<script language="javascript" type="text/javascript">
var fileUrl = "../"+"<%=request.getParameter("fileURL1")%>" ;
var fileUr2 = "../"+"<%=request.getParameter("fileURL2")%>" ;
var fileUr3 = "../"+"<%=request.getParameter("fileURL3")%>" ;
//alert(fileUrl+"+"+fileUr2+"+"+fileUr3)
document.getElementById("img1").src = fileUrl;
document.getElementById("img2").src = fileUr2;
document.getElementById("img3").src = fileUr3;
//alert("<%=basePath%>");
</script>
这里面我没做任何的业务处理,只是实现了来回的跳转,具体的业务可以自己新增