1.mapping的问题:
一个servlet可以指定一个映射路径。
一个servlet可以指定多个映射路径
一个servlet可以指定通用映射路径
指定一些后缀或者前缀
默认请求路径
2.servletcontext
在web容器在启动的时候,它会为每个web程序都创建一个对应的servletcontex对象,它代表了当前web应用。
1.共享数据(作用于session一样)
我们在这个servlet中保存的数据,可以再另外的servlet中可以使用。
package com.kuang.Servlet; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class GetServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //this.getInitParameter(); //初始化参数 //this.getServletConfig(); //servlet配置 //this.getServletContext();//servlet上下文 ServletContext context = this.getServletContext(); context.setAttribute("username","1234"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
另一个
package com.kuang.Servlet; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class HelloServlet2 extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = this.getServletContext(); String username = (String) context.getAttribute("username"); resp.setContentType("text/htm1"); resp.getWriter().print(username); } }
web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!--声明servlet映射类路径--> <servlet> <servlet-name>GetServlet</servlet-name> <servlet-class>com.kuang.Servlet.GetServlet</servlet-class> </servlet> <!--声明servlet映射web路径--> <servlet-mapping> <servlet-name>GetServlet</servlet-name> <url-pattern>/servlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>HelloServlet2</servlet-name> <servlet-class>com.kuang.Servlet.HelloServlet2</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet2</servlet-name> <url-pattern>/servlet2</url-pattern> </servlet-mapping> </web-app>
2.获取初始化参数
在web.xml中
<!--配置一些web应用初始化参数。--> <context-param> <param-name>url</param-name> <param-value>jdbc:mysql://localhost:3306/ssm</param-value> </context-param>
在servlet中写入:
package com.kuang.Servlet; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class ServletDmeo3 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = this.getServletContext(); String url = context.getInitParameter("url"); resp.getWriter().print(url); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } }
然后再web.xml中加入映射
<servlet> <servlet-name>gp</servlet-name> <servlet-class>com.kuang.Servlet.ServletDmeo3</servlet-class> </servlet> <servlet-mapping> <servlet-name>gp</servlet-name> <url-pattern>/gp</url-pattern> </servlet-mapping>
3.请求转发
使用ServletContext 进行请求转发,请求转发路径不发生变化
package com.kuang.Servlet; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class ServletDemo04 extends Helloserlvt { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletContext context = this.getServletContext(); //RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");//转发的请求路径 //requestDispatcher.forward(req,resp);//调用forward实现请求转发 context.getRequestDispatcher("/gp").forward(req,resp); } } //web.xml <servlet> <servlet-name>sd4</servlet-name> <servlet-class>com.kuang.Servlet.ServletDemo04</servlet-class> </servlet> <servlet-mapping> <servlet-name>sd4</servlet-name> <url-pattern>/sd4</url-pattern> </servlet-mapping>
4.读取资源文件
Properties
在Java目录下新建properties
在resources目录下新建properties
发现:都被打爆到同一个路径下:classes,我们俗称这个路径为classpath
思路:需要一个文件流
package com.kuang.Servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ServletDemo05 extends Helloserlvt { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties"); Properties prop = new Properties(); prop.load(is); String username = prop.getProperty("username"); String password = prop.getProperty("password"); resp.getWriter().print(username+" "+password); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }