Display PDF based on Request Parameter : XML Word PDF Mp3 « Servlet « Java Tutorial

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.net.*;
public class MyServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      String fileName = (String) request.getParameter("file");
      if (fileName == null || fileName.equals(""))
           throw new ServletException(
            "Invalid or non-existent file parameter in UrlServlet servlet.");
      if (fileName.indexOf(".pdf") == -1)
          fileName = fileName + ".pdf";
      URL pdfDir = null;
      URLConnection urlConn = null;
      ServletOutputStream stream = null;
      BufferedInputStream buf = null;
       pdfDir = new URL(getServletContext().getInitParameter("remote-pdf-dir") + fileName);
    } catch (MalformedURLException mue){
           throw new ServletException(mue.getMessage());
     stream = response.getOutputStream();
        "Content-Disposition","attachment; filename="+fileName );
      urlConn = pdfDir.openConnection();
      response.setContentLength( (int) urlConn.getContentLength()  );
     buf = new BufferedInputStream(urlConn.getInputStream());
     int readBytes = 0;

     while((readBytes = buf.read()) != -1)

     } catch (IOException ioe){
        throw new ServletException(ioe.getMessage());
     } finally {
     if(stream != null)
      if(buf != null)
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"


  Download:  ServletDisplayPdfBaseonRequestParameter.zip( 90 k)

25.31.XML Word PDF Mp3
25.31.1.Send Word file to client in a Servlet
25.31.2.Send MP3 file back to client with Servlet
25.31.3.Send XML data to client
25.31.4.Display PDF based on Request Parameter
25.31.5.Send XML to client using Servlet
25.31.6.Send Pdf to client using Servlet
25.31.7.Use XPath in servlet