Click to See Complete Forum and Search --> : How to open a html page programmatically from a servlet?


S Krishnan
May 2nd, 2000, 05:54 AM
How do we open a html page, in any browser, through servlets?

kris

bharath
May 3rd, 2000, 02:15 AM
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.

gkshankar
May 3rd, 2000, 09:03 AM
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();
}

dmeikle
May 9th, 2004, 09:12 PM
this code worked excellent for me. I just had to change the 'text/html' to 'application/pdf' though

cjard
May 10th, 2004, 03:41 AM
this isnt particularly efficient:


BufferedInputStream buffInStream= new BufferedInputStream(new FileInputStream(stFileNameToSend));


int iDataFromPDFFile;

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


this is better:


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..

dmeikle
May 10th, 2004, 10:11 PM
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


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();

cjard
May 11th, 2004, 03:16 AM
the API (http://java.sun.com/j2se/1.4.2/docs/api/java/io/PrintStream.html#write(byte[],%20int,%20int)) 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)