我正在尝试从JSP页面中选择一个文件并尝试将文件路径发送到servlet。在servlet页面中,我获取了文件路径的NULL值。
JSP页面
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome Page</title>
</head>
<body>
<script type="text/javascript">
function getFileName()
{
var image_file=document.getElementById('file');
var filename=image_file.value;
document.elements["filenm"].value=filename;
document.
}
</script>
<form action="Preprocess" method="post">
<table align="center">
<tr>
<td><input type="file" value="file" id="file" onclick="return getFileName();"></td>
<td><input type="text" name="filenm" id="filenm"></td>
<td><input type="submit" value="GO" id="button1"></td>
</tr>
</table>
</form>
</body>
</html>
Servlet页面:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Preprocess preprocess = new Preprocess();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1 align=\"CENTER\" style=\"text-align: center; vertical-align: center; color: GREEN\">Preprocessing...</h1>");
String path = request.getParameter("filenm");
try {
// starting of preprocessing
Instances instances = new Instances(new BufferedReader(new FileReader(path)));
input(preprocess,path);
} catch (Exception ex) {
Logger.getLogger(Preprocess.class.getName()).log(Level.SEVERE, null, ex);
}
}
在上面的代码中,path变量的值为null。请纠正我的方法错误。