title: Sending request.setAttribute?
body: <p>What I am trying to do is read file and send the text to jsp</p>
  <p>first, I read the context of my text file from fileGet.java. Then using request.setAttribute(&quot;chat&quot;, chat), I want to send the context to Live_index6.jsp</p>
  <p>However, as I use request.getAttribute(&quot;chat&quot;) in Live_index6.jsp to receive the data, it kept print &quot;null&quot;</p>
  <p>I checked that request.getAtrribute successfully printed the data I wanted in fileController.java,
  but it does not in Live_index6.jsp</p>
  <p>I searched internet and it said code in controller</p>
  <pre><code>RequestDispatcher rd1 = request.getRequestDispatcher(str);
  rd1.forward(request, response);
  </code></pre>
  <p>will send the request data to place where I directed, which is str == &quot;Live_index6.jsp&quot; that I setted.</p>
  <p>can you guys please check my code and what is the problem?</p>
  <p>Thank you so much!</p>
  <p>this is my Live_index6.jsp</p>
  <pre><code>&lt;%@page import=&quot;java.util.ArrayList&quot;%&gt;
  &lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=EUC-KR&quot;
      pageEncoding=&quot;EUC-KR&quot;%&gt;
   &lt;%@ taglib prefix=&quot;c&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot; %&gt;
   
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta charset=&quot;EUC-KR&quot;&gt;
  &lt;title&gt;Insert title here&lt;/title&gt;
  &lt;script src=&quot;jquery-3.3.1.js&quot;&gt;&lt;/script&gt;
  &lt;style&gt;
  
   textarea {
      width: 300px;
      height: 300px;
    }
  
  &lt;/style&gt;
  
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;% String user = request.getParameter(&quot;userName&quot;); 
  user = &quot;hong&quot;;
  %&gt;
  &lt;%=request.getAttribute(&quot;chat&quot;)  %&gt;
   &lt;h3&gt;Chat&lt;/h3&gt;
   &lt;textarea row = &quot;20&quot; col=&quot;50&quot; readonly id = &quot;ta1&quot;&gt;
   
  
   
   
   &lt;/textarea&gt;&lt;br&gt;
   &lt;h3&gt;Who?&lt;/h3&gt;
   &lt;input type = &quot;Text&quot; id = &quot;who1&quot; value = &quot;&lt;%=user%&gt;&quot; height = &quot;300px&quot; disabled&gt;
   &lt;h4&gt;보낼 글&lt;/h4&gt;
  &lt;input type = &quot;text&quot; id = &quot;chat1&quot; onkeyup=&quot;enterkey()&quot;&gt;
  &lt;input type = &quot;button&quot; value = &quot;가자&quot; onclick=&quot;insertFile(); kajaChool()&quot;&gt;
  
   
   
   &lt;br&gt;
   귓속말시 /wnickname (chatting)을 해야합니다. 닉네임과 chatting에 빈공간이 없을시 귓속말이 안보내집니다.
  
  &lt;script&gt;
      window.onload = function(){
          const name = document.getElementById(&quot;who1&quot;).value;
          $.ajax({ // $.ajax() $.get $.post
              url:&quot;getAllFile.do&quot;,
              data: { 
                  name : name
              }, // kaja.jsp?irum=hong&amp;na2=35
              dataType:&quot;text&quot;,
              type:&quot;post&quot;,
              success: function(result1){ 
                  //place where I want to print my file context
                  alert();
              },
              error: function(xhr1,status){
                  alert(&quot;에러상태: &quot; + &quot;\t&quot; + xhr1.status);
              }
          });
      }
      
      function enterkey(){
          if(window.event.keyCode == 13){
              insertFile();
              kajaChool();
          }
      }
      
      function insertFile(){
          const name = document.getElementById(&quot;who1&quot;).value;
          const chat = document.getElementById(&quot;chat1&quot;).value;
          $.ajax({ // $.ajax() $.get $.post
              url:&quot;insertFile.do&quot;,
              data: { 
                  name : name,
                  chat : chat
              }, // kaja.jsp?irum=hong&amp;na2=35
              dataType:&quot;text&quot;,
              type:&quot;post&quot;,
              success: function(result1){ 
                  
              },
              error: function(xhr1,status){
                  alert(&quot;error: &quot; + &quot;\t&quot; + xhr1.status);
              }
          });
      }
      
      function kajaChool(){
          document.getElementById(&quot;who1&quot;).disabled = true; 
          var tmp = document.getElementById(&quot;chat1&quot;).value;
          
          
          if(tmp.substring(0,2) != &quot;/w&quot;){
              ta1.value += &quot;[&quot; + who1.value + &quot;]&quot; + chat1.value + &quot;\n&quot;;
              ws1.send(&quot;[&quot; + who1.value + &quot;]&quot; + chat1.value);
          }else{
              var index = tmp.indexOf(&quot; &quot;) + 1;
              var toNickName = tmp.substring(2,index-1);
              
              if(true){
                  ta1.value += &quot;[&quot; + who1.value + &quot;]&quot; + &quot; &gt;&gt; &quot; + &quot;[&quot; + toNickName + &quot;]&quot; + tmp.substring(index) + &quot;\n&quot;;
                  ws1.send(&quot;[&quot; + who1.value + &quot;]&quot; + chat1.value);
              }else{
                  alert(&quot;존재하지 않는 닉네임입니다.&quot;);
                  chat1.value = &quot;&quot;;
                  chat1.focuse();
                  return;
              }
          }
          /*
          if(tmp.indexOf(&quot;/w&quot;) == -1){
              ta1.value += &quot;[&quot; + who1.value + &quot;]&quot; + chat1.value + &quot;\n&quot;;
          }else{
              var index = tmp.indexOf(&quot; &quot;) + 1;
              var toNickName = tmp.substring(2,index-1);
              ta1.value += &quot;[&quot; + who1.value + &quot;]&quot; + &quot; &gt;&gt; &quot; + &quot;[&quot; + toNickName + &quot;]&quot; + tmp.substring(index) + &quot;\n&quot;;
          }
          */
          // ws1.send(&quot;[&quot; + who1.value + &quot;]&quot; + chat1.value);
          
          chat1.value = &quot;&quot;;
          chat1.focuse();
      }
  
  
  &lt;/script&gt;
  
  &lt;script&gt;
  
      const ws1 = new WebSocket(&quot;ws://localhost:8080/Live_Chat6/kaja&quot;);
      
      
      const ta1 = document.getElementById(&quot;ta1&quot;);
      
      ws1.onerror = function(aa){
          alert(&quot;error&quot;);
      };
      ws1.onopen = function(aa){
          alert(&quot;[chat]&quot;);
          who1.focus();
          who1.select();
      };
      
      ws1.onmessage = function(aa){ 
          const name = document.getElementById(&quot;who1&quot;).value;
          const arr = aa.data.split(&quot;]&quot;);
          const tmp = arr[1];
          const comp = tmp.substring(0,2) == &quot;/w&quot; ;
          
          //ta1.value += comp + &quot;\n&quot;;
          
          
          if(comp){
              var index = tmp.indexOf(&quot; &quot;) + 1;
              var toNickName = tmp.substring(2,index-1);
              if(name == toNickName){
                  ta1.value += &quot;[&quot;+arr[0].substring(1)+&quot;] &gt;&gt; &quot; + &quot;[&quot; + toNickName + &quot;]&quot; + tmp.substring(index) + &quot;\n&quot;;
              }
          }else{
              ta1.value += aa.data + &quot;\n&quot;;
          }
          
      
      };
      
  &lt;/script&gt;
  
  
  &lt;/body&gt;
  &lt;/html&gt;
  
  </code></pre>
  <p>my FileController.java</p>
  <pre><code>package frontController;
  
  import java.io.IOException;
  
  import javax.servlet.RequestDispatcher;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import Services.fileGet;
  import Services.fileImpl;
  import Services.fileInsert;
  
  /**
   * Servlet implementation class FileController
   */
  @WebServlet(&quot;*.do&quot;)
  public class FileController extends HttpServlet {
         
      /**
       * @see HttpServlet#HttpServlet()
       */
      public FileController() {
          super();
          // TODO Auto-generated constructor stub
      }
  
      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
          //response.getWriter().append(&quot;Served at: &quot;).append(request.getContextPath());
          System.out.println(&quot;완료&quot;);
      }
  
      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
          //doGet(request, response);
          
          
          request.setCharacterEncoding(&quot;UTF-8&quot;);
          response.setCharacterEncoding(&quot;UTF-8&quot;);
          /*******************************/
          String c =request.getRequestURI().substring
                       (request.getContextPath().length());
          /**********************************/
          
          String str = &quot;Live_index6.jsp&quot;;  ///////////
          fileImpl fi = null; 
          switch(c) {
          
          case &quot;/insertFile.do&quot;:
              fi = new fileInsert();
              try {
                  fi.fileWork(request, response);
              }catch(Exception e){
                  System.out.println(e.getMessage());
              }
              break;  
              
              
          case &quot;/getAllFile.do&quot;:
              fi = new fileGet();
              try {
                  fi.fileWork(request, response);
              }catch(Exception e) {
                  System.out.println(e.getMessage());
              }
              break;
          } //swich -end 
          //String chat = (String) request.getAttribute(&quot;chat&quot;);
          //System.out.println(chat);
          
          RequestDispatcher rd1 = request.getRequestDispatcher(str);
          rd1.forward(request, response);
          
      }
  
  }
  
  </code></pre>
  <p>My fileGet.java</p>
  <pre><code>package Services;
  
  import java.io.BufferedReader;
  import java.io.DataInputStream;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileReader;
  import java.util.ArrayList;
  
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  public class fileGet implements fileImpl{
  
      @Override
      public void fileWork(HttpServletRequest request, HttpServletResponse response) throws Exception {
          // TODO Auto-generated method stub
          
          String path = request.getSession().getServletContext().getRealPath(&quot;/&quot;) + &quot;/Chat_Log/chat_log.txt&quot;;
          //String path = &quot;c:/Chat_Log/chat_log.txt&quot;;
          //ArrayList&lt;String&gt; chat = new ArrayList&lt;String&gt;();
          String chat = &quot;&quot;;
          try{
              
              File file = new File(path);
              
              FileReader filereader = new FileReader(file);
                          BufferedReader bufReader = new BufferedReader(filereader);
              String line = &quot;&quot;;
              while((line = bufReader.readLine()) != null){
                  chat += line + &quot;\n&quot;;
              }            
              bufReader.close();
          }catch (Exception e) {
              System.out.println(e.getMessage());
          }
          request.setAttribute(&quot;chat&quot;, chat);
          
      }
      
  
  }
  
  </code></pre>