下载文件后如何将jsp中的变量写入HTMLdiv
我正在尝试构建一个非常简单的应用程序。将有一个密码字段。如果用户输入了错误的密码,则会显示一条消息“密码不正确”。如果密码正确,则我必须执行两项任务:下载文件后如何将jsp中的变量写入HTMLdiv,html,jsp,Html,Jsp,我正在尝试构建一个非常简单的应用程序。将有一个密码字段。如果用户输入了错误的密码,则会显示一条消息“密码不正确”。如果密码正确,则我必须执行两项任务: 将下载一个文件(假设为A.txt)。该文件位于WEB-INF文件夹中 显示消息“密码正确” 我试图以以下方式显示该消息: <div style=""> <%=passwordMatchText%> </div> 当用户输入正确的密码时,文件将被下载,但div中的文本不会更改。jsp似乎没有在div中写
<div style="">
<%=passwordMatchText%>
</div>
当用户输入正确的密码时,文件将被下载,但div中的文本不会更改。jsp似乎没有在div中写入passwordMatchText的值。我已经用system.out.print()打印了文本;文本在console中打印正确,但在div中写入不正确
我还尝试将该值放入会话变量并写入div,但没有一个有效。我怎样才能解决这个问题
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page language="java" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.io.FileInputStream" %>
<%@ page import="java.io.FileOutputStream" %>
<%@ page import="java.io.File" %>
<%
String header=request.getHeader("user-agent");
String fileName="a.txt";
String originalPassword="1";
String passwordMatchText="";
String passwordTextStyle="";
String password = request.getParameter("password");
if(password==null ||!password.equals(originalPassword)){
passwordMatchText="Password does not match.";
passwordTextStyle="color:red;";
}
else {
passwordMatchText="Password matched.";
passwordTextStyle="color:green;";
String baseFilePath = request.getServletContext().getRealPath("/") + "WEB-INF/" + fileName;
File newFile= new File(baseFilePath);
if(newFile.exists()){
ServletOutputStream outputStream = response.getOutputStream();
FileInputStream inputStream = new FileInputStream(baseFilePath);
int fileLength = inputStream.available();
response.setContentType("application/octet-stream");
response.setContentLength(fileLength);
response.setHeader("Content-Disposition","attachment;filename=" + fileName);
byte[] outputByte = new byte[4096];
// copy binary content to output stream
while (inputStream.read(outputByte, 0, 4096) != -1) {
outputStream.write(outputByte, 0, 4096);
//out.write(outputByte.toString());
}
inputStream.close();
outputStream.flush();
outputStream.close();
}
else {
response.getWriter().println("File does not exists.");
}
}
%><!DOCTYPE HTML>
<html>
<head>
<html:base />
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!-- header menu closed -->
<div > <!-- container for body -->
<div style="border:1px solid black;margin-top:10px;padding:10px;"> <!-- div for content -->
<form action="test2.jsp" method="POST">
<div>
<div style=""><%=passwordMatchText%>
</div>
</div>
<div >
<div sytle ="width:100px">
Enter Password :
</div>
<div >
<input type="password" name="password" size="20" style="width: 100%;" />
</div>
</div>
<div >
<div >
<input class="cmd" type="submit" value="Submit" name="B2">
</div>
</div>
</form>
</div>
</div>
<!-- body closed -->
</body>
</html>
输入密码:
使用自动提交可以解决此问题。例如:
<%@ page trimDirectiveWhitespaces="true"%>
<%@ page language="java"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="java.io.FileInputStream"%>
<%@ page import="java.io.FileOutputStream"%>
<%@ page import="java.io.File"%>
<%
String header = request.getHeader("user-agent");
String fileName = "a.txt";
String originalPassword = "1";
String passwordMatchText = "";
String passwordTextStyle = "";
String password = request.getParameter("password");
if ("".equals(password) && "val1".equals(request.getParameter("name1"))) {
String baseFilePath = request.getServletContext().getRealPath("/") + "/WEB-INF/" + fileName;
File newFile = new File(baseFilePath);
if (newFile.exists()) {
ServletOutputStream outputStream = response.getOutputStream();
FileInputStream inputStream = new FileInputStream(baseFilePath);
int fileLength = inputStream.available();
response.setContentType("application/octet-stream");
response.setContentLength(fileLength);
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
byte[] outputByte = new byte[4096];
// copy binary content to output stream
while (inputStream.read(outputByte, 0, 4096) != -1) {
outputStream.write(outputByte, 0, 4096);
//out.write(outputByte.toString());
}
inputStream.close();
outputStream.flush();
outputStream.close();
} else {
response.getWriter().println("File does not exists.");
}
} else if (password == null || !password.equals(originalPassword)) {
passwordMatchText = "Password does not match.";
passwordTextStyle = "color:red;";
} else {
passwordMatchText = "Password matched.";
passwordTextStyle = "color:green;";
}
%><!DOCTYPE HTML>
<html>
<head>
<html:base />
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!-- header menu closed -->
<div>
<!-- container for body -->
<div style="border: 1px solid black; margin-top: 10px; padding: 10px;">
<!-- div for content -->
<form name="test2form" action="test2.jsp" method="POST">
<%
if (password != null && password.equals(originalPassword)) {
%>
<input type="hidden" name="name1" value="val1">
<%
}
%>
<div>
<div style="<%=passwordTextStyle%>"><%=passwordMatchText%>
</div>
</div>
<div>
<div sytle="width:100px">Enter Password :</div>
<div>
<input type="password" name="password" size="20"
style="width: 100%;" />
</div>
</div>
<div>
<div>
<input class="cmd" type="submit" value="Submit" name="B2">
</div>
</div>
</form>
</div>
</div>
<!-- body closed -->
</body>
<%
if (password != null && password.equals(originalPassword)) {
%>
<SCRIPT language="JavaScript">
document.test2form.submit();
</SCRIPT>
<%
}
%>
输入密码:
document.test2form.submit();
response.setHeader(“内容处置”、“附件;文件名=“+filename”)如果密码正确,您将不再发送HTML文档。Great@Kohei TAMURA。你的解决方案有效。非常感谢您的帮助和支持。但我想了解更多。您能解释一下它是如何工作的吗?响应应该分为两个响应,一个HTML响应和一个文件响应。首先,服务器发送一个HTML响应,客户端自动提交一个带有隐藏参数的请求。其次,如果请求包含参数,服务器接收请求并发送文件响应。