Various paths in Web engineering (Eclipse development)

Source: Internet
Author: User

Currently encountered in the Web project to write URLs and path files have

    • In WebContent. jsp/.html
    • servlet classes in SRC
    • SRC generic class for non-servlet classes

. jsp/.html

Action for Form form submission

1. Jump to. jsp

Use relative paths relative to the Web engineering root directory

    

 <!--  from jsp1.jsp to jsp2.jsp  Span style= "COLOR: #008000" >-->  <!--  jsp1.jsp in the form  -->  <  form  action  = "file/jsp2.jsp"  >  
<!---<!---<Action =".. /jsp1.jsp ">

2. Jump to Servlet1

Not relevant to the package

    

// Servlet1.java Map Address @WebServlet (name = "Servlet1", Urlpatterns = {"/servlet1"})
<!---<!---<Action =".. /servlet1 ">

Change the map address of the servlet so that the servlet maps to a folder with jsp2.jsp

// @WebServlet After modifying the map address of the Servlet1 (name = "Servlet1", Urlpatterns = {"/file/servlet1"})
<!---<!---<Action  = "Servlet1">

Change the servlet mapping address again, the servlet maps to the virtual directory

// after modifying the mapping address of the SERVLET1 again, map to the Virtualfile virtual directory @WebServlet (name = "Servlet1", Urlpatterns = {"/virtualfile/ Servlet1 "})
<!---<!---<Action =".. /virtualfile/servlet1 ">

servlet classes in SRC

1. Map address in URL annotations, contact with JSP (HTML)

2. Access resource files in WebContent 1.txt (according to the engineering structure in Tomcat)

// use Getservletcontext (). Getrealpath () to get the absolute path to the Web project this. Getservletcontext (). Getrealpath ("/sourcefile/1.txt"); // filepath= // D:\tomcat\apache-tomcat-8.0.50-windows-x64\apache-tomcat-8.0.50\webapps\test3\sourceFile\1.txt New BufferedReader (new FileReader (FilePath));

 

2. Access the resource file in SRC

// access the files in src to get the absolute path according to the project directory in Tomcat // servlet does not map to virtual directory this. Getservletcontext (). Getrealpath ("/web-inf/classes/2.txt"new bufferedwriter (  New FileWriter (FilePath));

 

 3. Redirect to Servlet (JSP), only with relative path

// REDIRECT uses relative path, URL changes response.sendredirect ("file/jsp2.jsp");

4, request forward to servlet (servlet), you can use relative path, absolute path

// relative Path RequestDispatcher rd = Request.getrequestdispatcher ("Servlet2"); Rd.forward (request,response); // Absolute Path RequestDispatcher rd = Request.getrequestdispatcher ("/servlet2"); Rd.forward (request,response);              

SRC generic class for non-servlet classes

  

1. General class access to resource files in WebContent

// use class to get the directory where this class is located (without Pacakage, only to classes), // get the absolute path of the resource file using GetResource () // in WebContent two times to return to the parent directory (Web-inf, project name Urltest) New File (Normalclass.  Class. getClassLoader (). GetResource (".. /.. /sourcefile/1.txt "new FileInputStream (FilePath);

2. General class interview resource file in src

// use class to get the directory where this class is located (without Pacakage, only to classes), // get the absolute path of the resource file using GetResource () New File (Normalclass.  Class. getClassLoader (). GetResource ("2.txt"new FileInputStream (FilePath);

Various paths in Web engineering (Eclipse development)

Related Article

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.