title: Retrieving web page through servlet in JSP
body: <p>How do I retrieve a webpage by a Servlet passed as a form parameter from a JSP page? </p>
  
  <p>The JSP page has a form with textbox to enter the url as a string and a submit button. The action is performed by a servlet which fetches the webpage from the url passed and displays the retrieved webpage. </p>
  
  <p>Here i have my servlet code `</p>
  
  <pre><code>    import java.io.IOException;
      import java.io.InputStream;
      import java.io.PrintWriter;
      import java.net.MalformedURLException;
      import java.net.URL;
      import java.net.URLConnection;
  
      import javax.servlet.ServletException;
      import javax.servlet.annotation.WebServlet;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
  
      /**
      * Servlet implementation class Search
      */
      @WebServlet("/Search")
      public class Search extends HttpServlet {
  private static final long serialVersionUID = 1L;
  
      /**
      * @see HttpServlet#HttpServlet()
      */
       public Search() {
      super();
      // TODO Auto-generated constructor stub
      }
  
  /**
   * @param  
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
   protected void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {
      String html = null;
  
      String server = request.getParameter("browsebox");
      if(server != null &amp;&amp; server !="")
          {
  
              response.sendRedirect("browse.jsp");
  
          }
  
          try {
              html = getWebPageFromUrl(server);
  
          }catch(Exception e) {
              System.err.println(e.toString());
              return;
  
          }
          response.setHeader("serchbox", server);
          response.setContentType("text/html");
  
          PrintWriter out = response.getWriter();
  
          if(html == null) {
              out.println("&lt;html&gt;");
              out.println("&lt;head&gt;&lt;title&gt;Refresher&lt;/title&gt;&lt;/head&gt;");
              out.println("&lt;body bgcolor=\"#ffffff\"&gt;");
              out.println("&lt;p&gt;The servlet has received a POST. This is the reply.    &lt;/p&gt;");
              out.println("&lt;/body&gt;&lt;/html&gt;");
          } else {
  
  
  
              out.print(html);
          }
      }
  
  
  
  
  
  
  
  
  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      doGet(request,response);
  }
  
  
  private String getWebPageFromUrl(String urlString) throws Exception {
  
      // Create a URL object from urlString
      URL stockURL;
  
      try {
          stockURL = new URL(urlString);
          } catch (MalformedURLException e) {
  
              String msg = "Invalid url: " + urlString;
              throw new Exception(msg);
              }
  
      // Open a connection to the URL
      URLConnection stockConnection;
  
      try {
          stockConnection = stockURL.openConnection();
          } catch (IOException e) {
  
              String msg = "Can't open connection to " + urlString;
              throw new Exception(msg);
              }
  
      // Get the InputStream from the URL connection
      InputStream webPageInputStream;
  
      try {
          webPageInputStream = stockConnection.getInputStream();
          } catch (IOException e) {
  
              // Could be any server error, but the most likely is 404
              String msg = "404 File Not Found: " + urlString;
              //throw new WebPageGrabberException(msg);
              throw new Exception(e.toString());
              }
  
      // Read the web page via the InputStream
  
      StringBuffer webPageData = new StringBuffer(32000);
      int totalBytesRead = 0;
      boolean moreToRead = true;
      byte[] readBuf = new byte[4096]; // Read the web page in 4K chunks
  
      while (moreToRead) {
  
          int numBytesRead = 0;
  
          try {
              numBytesRead = webPageInputStream.read(readBuf);
              } catch (IOException e) {
  
                  moreToRead = false;
                  numBytesRead = -1;
                  }
  
          if (numBytesRead &gt; 0) {
  
              totalBytesRead += numBytesRead;
              webPageData.append(new String(readBuf, 0, numBytesRead));
              } else {
                  moreToRead = false;
                  }
  
          }
  
      try {
          webPageInputStream.close();
  
          } catch (IOException e) {
  
          // Ignore any exception that might occur
              }
  
      webPageData.setLength(totalBytesRead);
       webPageData.toString();
  }
  }
  </code></pre>
  
  <p>I am getting blank from the servlet, when the form is submitted.</p>