0
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
         BufferedReader br = new BufferedReader(new FileReader("src/suraj.txt"));
         String everything="rahul";

                StringBuilder sb = new StringBuilder();
                String line = br.readLine();

                while (line != null)
                {
                    sb.append(line);
                    sb.append("\n");
                    line = br.readLine();
                     everything = sb.toString();

                }

                request.setAttribute("date",everything);
                RequsetDispatcher rd= Requ//here i got stuck how to forward the request to a jsp file           } 

    }

I want to send the value of my String variable that is everything to a jsp file so that I can display it on the browser.

suraj
  • 1
  • 1
  • 5

3 Answers3

0

You Forward the request to the jsp file and use request.getattribute in the jsp file.... better u take up a basic tutorial and understand how this works.

Abhijit Mazumder
  • 8,641
  • 7
  • 36
  • 44
0

In the JSP, to receive an attribute, try to do this thing:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<% 
    String everything = (String) request.getAttribute("date");
    System.out.println(everything);
%>

UPDATE:
In addition, if you want set the value to a Java Script variable:

<script language="JavaScript" type="text/JavaScript">
   var strEverything= '<%=everything%>';
</script>

UPDATE ACTION:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
             BufferedReader br = new BufferedReader(new FileReader("src/suraj.txt"));
             String everything="rahul";

                    StringBuilder sb = new StringBuilder();
                    String line = br.readLine();

                    while (line != null)
                    {
                        sb.append(line);
                        sb.append("\n");
                        line = br.readLine();
                         everything = sb.toString();

                    }

                    request.setAttribute("date",everything);
                    RequestDispatcher requestDispatcher ; 
                    requestDispatcher = request.getRequestDispatcher("/thankYou.jsp" ) ;
                    requestDispatcher.forward( request, response ) ;          

        }
Khanh Tran
  • 447
  • 6
  • 21
0

in JSP u need to get like String data=request.getAttribute("date")+""; then u will get the value in jsp Make sure your response should be that jsp then only u got the request scope. other wise put session.setAttribute("date",everthing); in jsp session.getAttribute("date"); then u will get any where.