我正在尝试构建一个非常简单的应用程序。将有一个密码字段。如果用户输入了错误的密码,则会显示一条消息"密码不正确。"。如果密码正确,那么我必须完成两项任务:
将下载文件(假设为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>
答案 0 :(得分:1)
此问题可以解决使用自动提交。例如:
<%@ 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>
<%
}
%>