From: http://blog.csdn.net/xiangxiaofeng12/article/details/5564756
String Path = "/index. jsp"; // This is the URL of an absolute path in the current application.
Servlet:
Requestdispatcher dispatcher = getservletcontext (). getrequestdispatcher (PATH );
JSP:
Requestdispatcher dispatcher = application. getrequestdispatcher (PATH );
ALL:
Dispatcher. Forward (); // go to the path page (no other output can be provided before or after this page)
Dispatcher. Include (); // output the execution result of the page path to the browser (other outputs can be made before or after this)
1. servletrequest. getrequestdispatcher (string path)
Path can be an absolute or relative path.
2. servletcontext. getrequestdispatcher (string path)
The path must start with "/", indicating the context Root
3. Another method is servletcontext. getnamedispatcher (string name)
The parameter is not a path, but its name. If multiple servlets have the same name, the configuration is different in Web. xml.
4. The above method returns a requestdispatcher object, and then forward () or include ()
5. The difference between forward () and include () is that after the include () method forwards the HTTP request to another servlet or JSP, the servlet or JSP can process the request, however, the original servlet or JSP is dominant, that is, the called servlet or JSP will be incorporated into the original httpresponse object if any HTTP response is generated.
<% @ Page contenttype = "text/html; charset = UTF-8" Language = "Java" Import = "Java. SQL. * "errorpage =" "%> <% @ page import =" java.net. urlencoder "%> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml">
void downLoaded(String fileName, HttpServletRequest request, HttpServletResponse response) throws IOException { response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); response.setContentType("application/x-download;charset=UTF-8"); // response.setContentType("application/octet-stream;charset=UTF-8"); ServletOutputStream outputStream = response.getOutputStream(); BufferedInputStream inputStream = new BufferedInputStream( new FileInputStream(fileName)); int size = 2048; byte[] data = new byte[size]; while ((size = inputStream.read(data, 0, data.length)) != 0) { outputStream.write(data, 0, size); outputStream.flush(); } }
Supplement:
Problem getoutputstream () has already been called for this response, see:
Http://qify.iteye.com/blog/747842