???Java Web????е?·??????
???????????? ???????[ 2016/6/24 10:59:22 ] ??????????????????? ??????
???????????????????tomcat????????????????????????????
?????????????????д??????web?У????·?????д???????????
????1???????????
????2???????????????????????????棨????“???”??
????3????????
????4??form???????action
??????????·??д????????????????????????WebPath?????????????servlet??PathServlet??
????????????
?????????????index.jsp????????jxf.path.PathServlet.jave?ж?????????·????д????????????
1 package jxf.path;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5
6 import javax.servlet.ServletException;
7 import javax.servlet.http.HttpServlet;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.http.HttpServletResponse;
10
11 public class PathServlet extends HttpServlet {
12 public void doGet(HttpServletRequest request?? HttpServletResponse response)
13 throws ServletException?? IOException {
14 //1???????
15 response.sendRedirect("/WebPath/index.jsp");
16
17 //2?????
18 //request.getRequestDispatcher("/index.jsp").forward(request?? response);
19
20 //3????????
21 //response.setContentType("text/html");
22 //response.getWriter().write("<thml><head></head><body><a href='/WebPath/index.jsp'>index.jsp<a></body></html>");
23
24 //4???????
25 //response.setContentType("text/html");
26 //response.getWriter().write("<thml><head></head><body><form action='/WebPath/index.jsp'><input type='submit' value='to index.jsp'/></form></body></html>");
27 }
28
29 }
????·????д??????????????
????1??·??????????????????????????
????2??·???????? “/” ??????·???????????????·????????????????
??????????????“·????????????”??????????д????????“2?????”??????????????????????????????????
?????ù?tomcat?????????????????????????????????????????????“http://localhost:????/?????/xxx???”?????????????????????tomcat???????????%tomcat????%/webapps??????£???????????????????????????????е?“/”???????·???????????webapps?????
???????????е?“???”???????????????????????????????????????????????????????????е???????????????????????????? “???·??” ??????????????????????web?????tomcat????????????MyEclipse?е??????????????“/”??????????MyEclipse???????WebRoot?????????????%tomcat%/webapps/WebPath?????????????“/”????WebRoot?????????????????index.jsp?????д??????“/index.jsp”
???????
????1??????????????·????“/”???%tomcat????%/webapps????
????2??????????????·????“/”???????е?WebRoot????
???????????????????????“/”?????·???????λ???????д?????????·????
???????????src?е?????????·??
??????????txt????????д?????????????servlet?????SourcePath?????????????
????jxf.path.SourcePath.java
1 public class SourcePath extends HttpServlet {
2
3 public void doGet(HttpServletRequest request?? HttpServletResponse response)
4 throws ServletException?? IOException {
5 response.setContentType("text/html;charset=UTF-8");
6 //String path = request.getRealPath("/WEB-INF/classes/demo.txt");
7 //InputStream inputStream = this.getServletContext().getResourceAsStream("/WEB-INF/classes/demo.txt");
8 String path = this.getServletContext().getRealPath("/WEB-INF/classes/demo.txt");
9 File file = new File(path);
10 FileInputStream inputStream = new FileInputStream(file);
11 byte[] bytes = new byte[1024];
12 StringBuilder sb = new StringBuilder();
13 int len = 0;
14 while((len = inputStream.read(bytes))!=-1){
15 sb.append(new String(bytes??0??len));
16 }
17 inputStream.close();
18 response.getWriter().write(sb.toString());
19 }
20
21 }
????web????м??????????????·???:
????request????.getRealPath("/WEB-INF/classes/demo.txt");//????????·?????????????????java Servlet API 2.1?汾?????????????????
????Context????.getResourceAsStream("/WEB-INF/classes/demo.txt");//????????·????????InputStream
????Context????.getRealPath("/WEB-INF/classes/demo.txt");//????????·?????????
????????Щ?????е?·??д?????????url??????????????????????????????????“/”???????WebRoot????????src?μ?????????????????????????????/WEB-INF/classes??????????д?"/WEB-INF/classes/demo.txt"
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11