The files that need to write url and path in the web project currently encountered are:
- webContent中.jsp/.html
- servlet class in src
- src normal class of non-servlet class
.jsp/.html
form submit action
1. Jump to .jsp
Use relative paths, relative to the web project root directory
<!-- from JSP1.jsp to JSP2.jsp --> <!-- JSP1.jsp中的form --> <form action="File/JSP2.jsp">
<!-- from JSP2.jsp to JSP1.jsp --> <!-- JSP2.jsp中的form --> <form action="../JSP1.jsp">
2. Jump to servlet1
has nothing to do with package
// Mapping address of Servlet1.java@WebServlet (name = "Servlet1", urlPatterns = { "/Servlet1" })
<!-- from JSP2.jsp to Servlet1 --> <!-- JSP2.jsp中的form --> <form action="../Servlet1">
Change the mapping address of the servlet so that the servlet is mapped to a folder with JSP2.jsp
// After modifying the mapping address of servlet1@WebServlet (name = "Servlet1", urlPatterns = { "/File/Servlet1" })
<!-- from JSP2.jsp to Servlet1 --> <!-- JSP2.jsp中的form --> <form action="Servlet1">
Change the servlet mapping address again, and the servlet is mapped to the virtual directory
// After modifying the mapping address of servlet1 again, map it to the virtualFile virtual directory @WebServlet(name = "Servlet1", urlPatterns = { "/virtualFile/Servlet1" })
<!-- from JSP2.jsp to Servlet1 --> <!-- JSP2.jsp中的form --> <form action="../virtualFile/Servlet1">
servlet class in src
1. The mapping address in the url annotation, contact jsp (html)
2. Access the resource file 1.txt in webContent (according to the project structure in tomcat)
//利用getServletContext().getRealPath() String filePath = 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 BufferedReader br = new BufferedReader(new FileReader(filePath));
2. Access resource files in src
// To access the files in src, the absolute path should be obtained according to the project directory in tomcat // The servlet should not be mapped to the virtual directory String filePath = this .getServletContext().getRealPath("/WEB-INF/classes/2.txt" ) ; BufferedWriter br = new BufferedWriter(new FileWriter(filePath));
3. Redirect to servlet (jsp), only relative path can be used
// Redirect using relative path, url changes response.sendRedirect("File/JSP2.jsp");
4. The request is forwarded to the servlet (servlet), and the relative path and absolute path can be used.
// Relative path RequestDispatcher rd = request.getRequestDispatcher("Servlet2" ); rd.forward(request,response); // Absolute path RequestDispatcher rd = request.getRequestDispatcher("/Servlet2" ); rd.forward(request,response);
src normal class of non-servlet class
Access resource files in webContent
// Use class to get the directory where this class is located (excluding pacakage, only to classes), // Use getResource() to get the absolute path of the resource file // In webContent, you need to return to the upper-level directory twice (WEB-INF, project name URLTest) File filePath = new File(NormalClass.class.getClassLoader ( ).getResource("../../sourceFile/1.txt" ).getPath()); FileInputStream fn = new FileInputStream(filePath);
Access resource files in src
// Use class to get the directory where this class is located (excluding pacakage, only to classes), // Use getResource() to get the absolute path of the resource file File filePath = new File(NormalClass.class .getClassLoader ().getResource("2. txt" ).getPath()); FileInputStream fn = new FileInputStream(filePath);