For web-based URL addresses, there are two ways to do this: ① absolute path ("Path beginning with '/') ② relative path (" Path not beginning with '/')
The URL for the absolute path (the path beginning with '/') needs to be used for the server or client browser depending on the URL. ① if it is for the server, then "/" represents the current application (such as the requestandresponse here or your Web project path webcontent, WebRoot); ② if it is for the client browser, then "/" stands for the current site (such as the http://localhost:8080/in this case or the > WebApps under Tomcat);
Example:
public void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException { 1. Forwarding type: (address to server)//request.getrequestdispatcher ("/page/test1.jsp"). Forward (request, response);//Correct//re Quest.getrequestdispatcher ("page/test1.jsp"). Forward (request, response);//correct//request.getrequestdispa relative to current Web project Tcher ("/requestandresponse/page/test1.jsp"). Forward (request, response); HTTP Status 404-/requestandresponse/requestandresponse/page/test1.jsp//2. REDIRECT type: (address to browser)//response.sendredi Rect ("/page/test1.jsp");//error:http://localhost:8080/page/test1.jsp//response.sendredirect ("page/test1.jsp") ;//correct//response.sendredirect ("/requestandresponse/page/test1.jsp");//correct//3. Get the Resource Real path: (for server) Response.sethea
Der ("Content-type", "text/html;charset=utf-8");
OutputStream out = Response.getoutputstream (); String path = This.getservletcontext (). Getrealpath ("/page/test1.jsp");//(1) Correct: string path = tHis.getservletcontext (). Getrealpath ("page/test1.jsp"); (2) Correct: String path = This.getservletcontext (). Getrealpath ("/requestandresponse/page/test1.jsp");//(3) The error address is: E : \workspace\jsp\.metadata\.me_tcat\webapps\requestandresponse\requestandresponse\page\test1.jsp//4. Get the resource file input stream pair
Image: (for server) InputStream in = This.getservletcontext (). getResourceAsStream ("/page/test1.jsp");//correct: output test1.jsp page correctly InputStream in = This.getservletcontext (). getResourceAsStream ("page/test1.jsp");//correct: Correct output test1.jsp page InputStream I n = This.getservletcontext (). getResourceAsStream ("/requestandresponse/page/test1.jsp");//
Error java.lang.NullPointerException; }
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%> <% String Path = Request.getcontextpath ()
;
String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >