[英]Get parameter when multipart request in JSP
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Add new product</title>
<link type="text/css" rel="Stylesheet" href="css/SiteStyle.css" />
<script src="js/jquery-1.3.2.js" type="text/javascript"></script>
</head>
<body>
<form id="MainForm" action="Relay" method="post" enctype="multipart/form-data">
<input name="destination" type="hidden" value="AddNewProduct" />
<script src="js/AddNewProduct.js" type="text/javascript"></script>
<center>
<h1>Add new product</h1>
<hr />
<table class="TableLogin">
<tr>
<td align="center" colspan="2" class="TDLoginHeader">
Product information</td>
</tr>
<tr>
<td align="right" class="TDLoginTitle">
Product Title
</td>
<td align="left" class="TDLoginText">
<input id="TxtTitle" type="text" class="LoginTextBoxes" name="Title" maxlength="75" /></td>
</tr>
<tr>
<td align="right" class="TDLoginTitle">
Dollar price
</td>
<td align="left" class="TDLoginText">
<input id="TxtPrice" type="text" class="LoginTextBoxes" name="Price" maxlength="15" /></td>
</tr>
<tr>
<td align="center" colspan="2" class="TDLoginTitle">
Product number</td>
</tr>
<tr>
<td align="center" colspan="2">
<table style="width:100%;">
<tr>
<td align='center' class='TDProductNumber'>
<input id='Txtdigit1' name='Txtdigit1' class='TxtProductNumber' type='text' />
</td>
<td align='center' class='TDProductNumber'>
<input id='Txtdigit2' name='Txtdigit2' class='TxtProductNumber' type='text' />
</td>
<td align='center' class='TDProductNumber'>
<input id='Txtdigit3' name='Txtdigit3' class='TxtProductNumber' type='text' />
</td>
<td align='center' class='TDProductNumber'>
<input id='Txtdigit4' name='Txtdigit4' class='TxtProductNumber' type='text' />
</td>
<td align='center' class='TDProductNumber'>
<input id='Txtdigit5' name='Txtdigit5' class='TxtProductNumber' type='text' />
</td>
<td align='center' class='TDProductNumber'>
<input id='Txtdigi6' name='Txtdigit6' class='TxtProductNumber' type='text' />
</td>
<td align='center' class='TDProductNumber'>
<input id='Txtdigit7' name='Txtdigit7' class='TxtProductNumber' type='text' />
</td>
</tr>
<tr>
<td colspan="7" class="TDLoginTitle">
<label>Product Image</label>
<input id="ProductImage" name="ProductImage" type="file" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center" colspan="2">
<input id="BtnAdd" type="button" value="Add" class="BtnSize" /></td>
</tr>
</table>
<div id="DivAddMessage">
</div>
<div>
<a href='Login.jsp'>Back</a>
</div>
</center>
</form>
</body>
</html>
Servlet“中繼”代碼:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String destination=request.getParameter("destination");
out.print(destination);
}
這是用於輸入產品信息(標題,價格,7位數字,產品圖片)的一段代碼,當我想在servlet中獲取“目標”參數時,得到的值為null我知道這是因為有多重請求,但是我可以得到這個價值和文件上傳價值?
由於您使用多部分編碼('multipart / form-data')發布,因此參數未如您所願。
例如,如果您使用commons-fileupload,則這些參數將以FileItem對象上的'isFormField'方法顯示並可以識別。
這個關於coderanch的主題解釋了如何: coderanch
大多數(每個)現代Web框架都將其抽象化,並且使這種工作變得更加容易。 引用此站點將為您提供幫助
碼
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (item.isFormField()) {
// Process regular form field (input type="text|radio|checkbox|etc", select, etc).
String fieldname = item.getFieldName();
String fieldvalue = item.getString();
// ... (do your job here)
} else {
// Process form file field (input type="file").
String fieldname = item.getFieldName();
String filename = FilenameUtils.getName(item.getName());
InputStream filecontent = item.getInputStream();
// ... (do your job here)
}
}
} catch (FileUploadException e) {
throw new ServletException("Cannot parse multipart request.", e);
}
// ...
}
使用注釋
@MultipartConfig
為您的Servlet
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.