2017-04-01 3 views
1

非常に単純なアプリケーションを作成しようとしています。パスワードフィールドがあります。ユーザーが間違ったパスワードを入力すると、「パスワードが間違っています」というメッセージが表示されます。パスワードが正しい場合は、2つのタスクを実行する必要があります。ファイルをダウンロードした後、jspからhtml divに変数を書き込む方法

  1. ファイルがダウンロードされます(a.txtと仮定します)。ファイルはWEB-INFフォルダーにあります。

  2. 表示するメッセージ「パスワードが正しい」

私は次のようにメッセージを表示しようとしたユーザが正しいパスワードを入力すると、ファイルが

<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

'response.setHeader(" Content-Disposition "、" attachment; filename = "+ fileName);パスワードが正しい場合、HTML文書を送信しなくなりました。 – mehulmpt

答えて

1

をsovleすることができます。この問題は、自動提出を使用し解決することができます。例:

<%@ 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> 
<% 
    } 
%> 
+0

偉大な@コヘイタムラ。あなたのソリューションは動作します。あなたの助けとサポートに感謝します。しかし、もっと学びたい。どのように動作するか説明してください。 – Rumel

+1

レスポンスは、HTMLレスポンスとファイルレスポンスの2つのレスポンスに分割する必要があります。まず、サーバーはHTML応答を送信し、クライアントは自動的に非表示のパラメーターで要求を送信します。第2に、サーバは要求を受信し、要求にパラメータが含まれている場合はファイル応答を送信します。 –

関連する問題