Web project URL address, there is a slash and no slash path difference and use

Source: Internet
Author: User

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" > 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.