在JSP中通过servlet检索网页

时间:2012-05-09 01:21:21

标签: java jsp servlets

如何通过从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变为空白。

1 个答案:

答案 0 :(得分:3)

如果您需要完整显示网站,只需重定向即可。

response.sendRedirect(request.getParameter("url"));

如果您需要显示JSP页面中嵌入的网站,请使用iframe。

<iframe src="${fn:escapeXml(param.url)}"></iframe>

JSTL fn:escapeXml()就是为了防止可能的XSS攻击)

无论哪种情况,您可能只想提前验证URL是以http://或https://等开头的。