|
-
May 2nd, 2000, 05:54 AM
#1
How to open a html page programmatically from a servlet?
How do we open a html page, in any browser, through servlets?
kris
-
May 3rd, 2000, 02:15 AM
#2
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.
-
May 3rd, 2000, 09:03 AM
#3
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();
}
-
May 9th, 2004, 09:12 PM
#4
this code worked excellent for me. I just had to change the 'text/html' to 'application/pdf' though
-
May 10th, 2004, 03:41 AM
#5
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..
-
May 10th, 2004, 10:11 PM
#6
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();
-
May 11th, 2004, 03:16 AM
#7
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)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|