非常に単純なアプリケーションを作成しようとしています。パスワードフィールドがあります。ユーザーが間違ったパスワードを入力すると、「パスワードが間違っています」というメッセージが表示されます。パスワードが正しい場合は、2つのタスクを実行する必要があります。ファイルをダウンロードした後、jspからhtml divに変数を書き込む方法
ファイルがダウンロードされます(a.txtと仮定します)。ファイルはWEB-INFフォルダーにあります。
表示するメッセージ「パスワードが正しい」
私は次のようにメッセージを表示しようとしたユーザが正しいパスワードを入力すると、ファイルが
<div style="">
<%=passwordMatchText%>
</div>
ダウンロードされますが、 div内のテキストは変更されません。 jspはdiv内にpasswordMatchTextの値を書き込んでいないようです。私はsystem.out.print()でテキストを印刷しました。テキストはコンソールに正しく表示されますが、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>
'response.setHeader(" Content-Disposition "、" attachment; filename = "+ fileName);パスワードが正しい場合、HTML文書を送信しなくなりました。 – mehulmpt