1.ServletContext
- web容器在启动时候,它为每个web程序都创建了一个对应的ServletContext对象。
- ServletContext对象代表了当前的web应用。
- 通过ServletContext接口做如下操作,此外还有很多见源码
1 2 3 4 5 6 7 8 9
| ServletContext context = this.getServletContext();
context.getAttribute();
context.getResourceAsStream();
context.getRequestDispatcher();
context.getInitParameter();
|
1.1共享数据
共享类
1 2 3 4 5
| ServletContext con = this.getServletContext(); String username = "李晓明";
con.setAttribute("username",username);
|
接收共享类
1 2 3 4 5 6 7
| ServletContext con = this.getServletContext();
String username = (String) con.getAttribute("username");
PrintWriter out = resp.getWriter(); out.println(username);
|
1.2读取资源文件
1 2 3 4 5 6 7 8 9 10 11
| ServletContext sct = this.getServletContext();
InputStream in = sct.getResourceAsStream("/WEB-INF/classes/jdbc.properties");
Properties prop = new Properties(); prop.load(in);
PrintWriter out = resp.getWriter(); out.println(prop.getProperty("driverClassName")); out.println(prop.getProperty("username")); out.println(prop.getProperty("password"));
|
1.3请求转发(常用)
1 2 3 4 5 6 7 8
| ServletContext sct = this.getServletContext();
RequestDispatcher rd = sct.getRequestDispatcher("/指定路径文件");
rd.forward(req,resp);
req.getRequestDispatcher("/指定路径文件").forward(req,resp);
|
1.4获取初始化参数(了解)
获取类
1 2 3 4 5 6 7
| ServletContext sct = this.getServletContext();
String ipr = sct.getInitParameter("xml中的标识");
PrintWriter out = resp.getWriter(); out.println(ipr);
|
xml参数
1 2 3 4
| <context-param> <param-name>一个标识</param-name> <param-value>具体参数</param-value> </context-param>
|
2.Response
简单的文件下载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| String localPath = "文件绝对路径 taget下!";
System.out.println("控制台:获取文本路径"+localPath);
String filename = localPath.substring(localPath.lastIndexOf("\\")+1);
resp.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(filename,"utf-8"));
FileInputStream fsm = new FileInputStream(localPath);
ServletOutputStream serverOSM = resp.getOutputStream();
int len = 0; byte[] bytes = new byte[1024];
while ((len = fsm.read(bytes)) != -1){ serverOSM.write(bytes,0,len); }
|
3.Request
3.1前后端交互
后端 处理数据
1 2 3 4 5 6 7 8 9 10 11
| String name = req.getParameter("name"); String pass = req.getParameter("password");
String[] hobbies = req.getParameterValues("hobbies");
req.getRequestDispatcher("/success.jsp").forward(req,resp);
|
前端 登录页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <div style="text-align: center"> <%--这里提交的路径,需要寻找到项目的类路径--%> <form action="${pageContext.request.contextPath}/login" method="post"> 用户名:<input type="text" name="name"><br> 密码:<input type="password" name="password"><br> 爱好 <input type="checkbox" name="hobbies" value="Girls">Girls <input type="checkbox" name="hobbies" value="Code">Code <input type="checkbox" name="hobbies" value="Math">Math <input type="checkbox" name="hobbies" value="Tinnies">Tinnies <input type="submit"> <br> </form> </div>
|
后端在xml中的配置 前端读取url-pattern
1 2 3 4 5 6 7 8
| <servlet> <servlet-name>login</servlet-name> <servlet-class>qingqiu.LoginCdx</servlet-class> </servlet> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping>
|
3.2重定向和转发
两者都可以实现跳转页面
重定向
- 就是通过各种方法将各种网络请求重新定个方向转到其它位置
- 由浏览器端进行的页面跳转
- 重定向的时候,url栏会发生变化 302
- resp.sendRedirect(“具体路径”);
转发
- 由服务器端进行的页面跳转。
- 请求转发:一种在服务器内部的资源跳转方式。
- 请求转发时候,url栏不产生变化 307
- req.getRequestDispatcher(“具体路径”).forward(req,resp)