gpt4 book ai didi

java - JSP中通过servlet检索网页

转载 作者:行者123 更新时间:2023-12-02 07:48:37 25 4
gpt4 key购买 nike

如何通过从 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 中的内容为空白。

最佳答案

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

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

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

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

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

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

关于java - JSP中通过servlet检索网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10508683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com