0
JSP 페이지에서 양식 매개 변수로 전달 된 서블릿으로 웹 페이지를 검색하려면 어떻게해야합니까?JSP로 서블릿을 통해 웹 페이지 가져 오기
JSP 페이지에는 url을 문자열과 제출 버튼으로 입력하는 텍스트 상자가있는 양식이 있습니다. 이 동작은 전달 된 URL에서 웹 페이지를 가져 와서 검색된 웹 페이지를 표시하는 서블릿에 의해 수행됩니다. 여기
난 내 서블릿 코드`
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();
}
}
내가 양식이 제출 서블릿에서 빈 얻고있다.
모두 코드로 시작합니다. 이 코드를 작성한 곳에서 문제가 발생 했습니까? – fivedigit
Dicey! 당신은 ../../../../etc/passwd (또는 이와 비슷한)에 들어가서 그것을 표시 할 사람이 될 것입니까? 우리가 당신이 실제로 성취하려는 것을 볼 수 있도록 약간의 코드를주십시오. – ingyhere
@ingyhere :이 문제는 의미가 없습니다. OP가 정말 멍청해서 java.io.File을 사용하여 웹 페이지를 검색하지 않는 한) – BalusC