如何通过从 JSP 页面作为表单参数传递的 Servlet 检索网页?
JSP 页面有一个带有文本框的表单,用于输入作为字符串的 url 和一个提交按钮。该操作由 servlet 执行,该 servlet 从传递的 url 获取网页并显示检索到的网页。
这里我有我的 servlet 代码`
    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 && 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("<html>");
            out.println("<head><title>Refresher</title></head>");
            out.println("<body bgcolor=\"#ffffff\">");
            out.println("<p>The servlet has received a POST. This is the reply.    </p>");
            out.println("</body></html>");
        } 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 > 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();
}
}
提交表单时,我从 servlet 得到空白。