当在html中使用
时(使用fileupload上传文件),request.getParameter("name")得到的是null,为了避免这一问题,并且为了检验输入框是否为空,通过js向servlet提交。这样值可以传过去了,但是上传图片中解析request的值却为null现在想要得到即可以检验输入是否为空,又可以得到传值并将图片保存起来的效果。顺便想请教一下 js跳转的servlet的request和action跳转到servlet的request有什么不同。 P.S:请您不要怀疑什么没有进到servlet等类似问题。如果您有更好的解决方案,非常感谢您的告知,我会在一段日子后研究,但现在我的主要问题是使用html、js、servlet这三种技术解决问题。
非常感谢!
代码如下
html:
<%@ page contentType="text/html;charset=GBK" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
新增商家
body,td{font-size:12px;} //验证填入值不为空 function checkEmpty(){ var name = document.getElementById("merchantName").value; var call = document.getElementById("merchantCall").value; var picture = document.getElementById("picture").value; var address = document.getElementById("merchantAdd").value; var validate = document.getElementById("validatetCall").value; if(name==""||call==""||picture==""||address==""||validate==""){ alert("请您填写所需要的信息"); } else{ // 跳转到AddMerchantServlet中 document.merchantForm.action = "/discountCoupon/AddMerchant?name="+name+"&call="+call+"&picture="+picture+"&address="+address+"&verifyCall="+validate ; document.merchantForm.submit(); } }
* 商家名称: | ||
* 商家电话: | ||
* 商家海报: | ||
* 商家地址: | ||
* 验证手机:    添加 | ||
    |
servlet
package com.ygsoft.servlet;
import java.io.BufferedInputStream;
public class AddMerchant extends HttpServlet {
private static final long serialVersionUID = 1L;
File tmpDir = null;
File saveDir = null;
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
//从addMerchant.jsp中取得数据
String name = request.getParameter("name");
//对得到的数据进行转码
byte [] bytes1 = name.getBytes("iso-8859-1");
String merchantName = new String(bytes1,"GBK");
String merchantCall = request.getParameter("call");
String merchantPicture = request.getParameter("picture");
//对得到的数据进行转码
byte [] bytes2 = merchantPicture.getBytes("iso-8859-1");
String picture = new String(bytes2,"GBK");
String address = request.getParameter("address");
//对得到的数据进行转码
byte [] bytes3 = address.getBytes("iso-8859-1");
String merchantAdd = new String(bytes3,"GBK");
String validateCall = request.getParameter("verifyCall");
//向数据库中存储数据
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
MerchantDao dao = (MerchantDao) factory.getBean("merchantDao");
Merchant merchant = new Merchant();
MerchantAssistant merchantAssistant = new MerchantAssistant();
HashSet<MerchantAssistant> set = new HashSet<MerchantAssistant>();
merchantAssistant.setVerifyUserPhoneNum(validateCall);
merchantAssistant.setActive(1);
set.add(merchantAssistant);
merchant.setMerchantName(merchantName);
merchant.setMerchantAdd(merchantAdd);
merchant.setMerchantPhoneNum(merchantCall);
merchant.setMerchantPic(picture);
merchantAssistant.setMerchant(merchant);
merchant.setMerchantAssistantSet(set);
dao.addMerchant(merchant);
//跳转到商家管理页面
RequestDispatcher rd = request.getRequestDispatcher("/merchantManage");
rd.forward(request, response);
}
public AddMerchant() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//对图片进行存储
try {
if (ServletFileUpload.isMultipartContent(request)) {
// 创建该对象
DiskFileItemFactory dff = new DiskFileItemFactory();
// 指定上传文件的临时目录
dff.setRepository(tmpDir);
// 指定在内存中缓存数据大小,单位为byte
dff.setSizeThreshold(1024000);
// 创建该对象
ServletFileUpload sfu = new ServletFileUpload(dff);
// 指定上传文件的最大尺寸
sfu.setFileSizeMax(5000000);
// 指定依次上传多个文件的总尺寸
sfu.setSizeMax(10000000);
// 解析request请求,并返回FileItemIterator集合
FileItemIterator fii = sfu.getItemIterator(request);
// 解析request请求,并返回FileItemIterator
while (fii.hasNext()) {
// 从集合中获得一个文件流
FileItemStream fis = fii.next();
// 过滤表单中空文件域
if ((!fis.isFormField()) && fis.getName().length() > 0) {
// 获得的上传文件的文件名
String fileName = fis.getName().substring(
fis.getName().lastIndexOf("//") + 1);
if (fis.getName().lastIndexOf("//") == -1) {
fileName = fis.getName().substring(
fis.getName().lastIndexOf("/") + 1);
// 获得文件输入流
BufferedInputStream in = new BufferedInputStream(
fis.openStream());
// 获得文件输出流
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(new File(saveDir,
fileName)));
// 开始把文件写到你指定的上传文件夹
Streams.copy(in, out, true);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void init() throws ServletException {
super.init();
String tmpPath = "d://tempdir";
String savePath = "d://savedir";
tmpDir = new File(tmpPath);
saveDir = new File(savePath);
if (!tmpDir.isDirectory())
tmpDir.mkdir();
if (!saveDir.isDirectory())
saveDir.mkdir();
}
}