CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2000
    Location
    India
    Posts
    6

    How to open a html page programmatically from a servlet?

    How do we open a html page, in any browser, through servlets?

    kris


  2. #2
    Join Date
    Apr 2000
    Location
    Hyderabad, India
    Posts
    23

    Re: How to open a html page programmatically from a servlet?

    hai,

    i think the following program may serv u.

    Import javax.servlet.*;
    Import javax.servlet.http.*;
    Import java.io.*;

    Public class SampleServlet extends HttpServlet
    {

    public void doGet(HttpServletRequest req, HttpServletResponce res) throws ServletException, IOException
    {
    res.setContentType(“text/html”);
    PrintWriter Out=res.getWriter();
    Out.println(“<html>”);
    Out.println(“<head>”);
    Out.println(“<Title>”);
    Out.println(“This is a sample Example”);
    Out.println(“</Title>”);
    Out.println(“</head>”);
    Out.println(“<Body>”);
    Out.println(“<center>”);
    Out.println(“This is a sample Servlet”);
    Out.println(“</center>”);
    Out.println(“</Body>”);
    Out.println(“</html>”);

    Out.close();
    }
    }

    - bye


    S.V Bharath Reddy
    Software Engineer,
    Hyderabad.

  3. #3
    Join Date
    Mar 2000
    Posts
    10

    Re: How to open a html page programmatically from a servlet?

    Hi
    You can open the HTML page thru java code.


    try
    {

    String stFileNameToSend = VB_Constant.VB_FILE_PARTC;
    //ServletOutputStream servOutstream = m_httpResponse.getOutputStream();
    PrintWriter printstream = new PrintWriter(m_httpResponse.getOutputStream());

    m_httpResponse.setContentType("text/html");


    // Open the stream to read from PDF file and write to servlet Output Stream
    BufferedInputStream buffInStream= new BufferedInputStream(new FileInputStream(stFileNameToSend));


    int iDataFromPDFFile;

    while((iDataFromPDFFile = buffInStream.read())!=-1)
    {
    printstream.write(iDataFromPDFFile);
    }
    printstream.flush();

    //closing the Buffered InputStream and Servlet OutputStream
    buffInStream.close();

    printstream.flush();

    //closing the Buffered InputStream and Servlet OutputStream
    printstream.close();




    }
    catch(IOException exp)
    {
    exp.printStackTrace();
    }






  4. #4
    Join Date
    Mar 2000
    Location
    Vancouver, BC, Canada
    Posts
    278
    this code worked excellent for me. I just had to change the 'text/html' to 'application/pdf' though

  5. #5
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    this isnt particularly efficient:

    Code:
    BufferedInputStream buffInStream= new BufferedInputStream(new FileInputStream(stFileNameToSend));
    
    
    int iDataFromPDFFile;
    
    while((iDataFromPDFFile = buffInStream.read())!=-1)
    {
    printstream.write(iDataFromPDFFile);
    }
    printstream.flush();
    this is better:

    Code:
    InputStream is = new FileInputStream(stFilenameToSend);
    
    byte[] buf = new byte[4096];
    
    for(int bytesRead = is.read(buf); bytesRead>-1; bytesRead = is.read(buf)){
      //write  bytesRead number of bytes to the output from the buf[]
      printstream.write(buf, 0, bytesRead);
    }
    printstream.flush();
    you can increase the buffer size if you like, from 4096 to some higher value..
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

  6. #6
    Join Date
    Mar 2000
    Location
    Vancouver, BC, Canada
    Posts
    278
    I had to convert buf to a new String() (making it a String,int,int) , since the printstream write did not have a byte[],int, int

    Code:
    InputStream is = new FileInputStream(stFilenameToSend);
    
    byte[] buf = new byte[4096];
    
    for(int bytesRead = is.read(buf); bytesRead>-1; bytesRead = is.read(buf)){
      //write  bytesRead number of bytes to the output from the buf[]
      printstream.write(new String(buf), 0, bytesRead);
    }
    printstream.flush();

  7. #7
    Join Date
    Oct 2003
    Location
    .NET2.0 / VS2005 Developer
    Posts
    7,104
    the API begs to differ:

    write
    public void write(byte[] buf,
    int off,
    int len)Write len bytes from the specified byte array starting at offset off to this stream. If automatic flushing is enabled then the flush method will be invoked.
    Note that the bytes will be written as given; to write characters that will be translated according to the platform's default character encoding, use the print(char) or println(char) methods.


    Overrides:
    write in class FilterOutputStream
    Parameters:
    buf - A byte array
    off - Offset from which to start taking bytes
    len - Number of bytes to write
    See Also:
    FilterOutputStream.write(int)
    "it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured