一,Servlet访问URL映射配置
由于客户端是通过URL地址访问web服务器中的资源,所以Servlet程序若想被外界访问,必须把servlet程序映射到一个URL地址上,这个工作在web.xml文件中使用<servlet>元素和<servlet-mapping>元素完成。
Servlet:该元素用于注册Servlet,它包含有两个主要的子元素:<servlet-name>和<servlet-class>,分别用于设置Servlet的注册名称和Servlet的完整类名。
servlet-mapping:该元素用于映射一个已注册的Servlet的一个对外访问路径,它包含有两个子元素:<servlet-name>和<url-pattern>,分别用于指定Servlet的注册名称和Servlet的对外访问路径。
<servlet> <servlet-name>HelloServlet</servlet-name><!-- servlet名称 --> <servlet-class>com.zender.servlet.HelloServlet</servlet-class><!-- servlet关联的类 --> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello.html</url-pattern><!-- servlet访问路径 --> </servlet-mapping>
同一个Servlet可以被映射到多个URL上,即多个<servlet-mapping>元素的<servlet-name>子元素的设置值可以是同一个Servlet的注册名
<servlet> <servlet-name>HelloServlet</servlet-name><!-- servlet名称 --> <servlet-class>com.zender.servlet.HelloServlet</servlet-class><!-- servlet关联的类 --> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello.html</url-pattern><!-- servlet访问路径 --> </servlet-mapping> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello1.html</url-pattern><!-- servlet访问路径 --> </servlet-mapping> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello2.html</url-pattern><!-- servlet访问路径 --> </servlet-mapping>
二,ServletURL通配符:*
在Servlet映射到的URL中也可以使用*通配符,但是只能有两种固定的格式:
格式一:*.扩展名"
<servlet> <servlet-name>HelloServlet</servlet-name><!-- servlet名称 --> <servlet-class>com.zender.servlet.HelloServlet</servlet-class><!-- servlet关联的类 --> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>*.html</url-pattern><!-- servlet访问路径 --> </servlet-mapping>
格式二:以正斜杠(/)开头并以"/*"结尾
<servlet> <servlet-name>HelloServlet</servlet-name><!-- servlet名称 --> <servlet-class>com.zender.servlet.HelloServlet</servlet-class><!-- servlet关联的类 --> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello/*</url-pattern><!-- servlet访问路径 --> </servlet-mapping>
三,缺省Servlet
Servlet的映射路径仅仅为一个正斜杠(/),那么这个Servlet就成为当前Web应用程序的缺省Servlet。 凡是在web.xml文件中找不到匹配的<servlet-mapping>元素的URL,它们的访问请求都将交给缺省Servlet处理。
<servlet> <servlet-name>HelloServlet</servlet-name><!-- servlet名称 --> <servlet-class>com.zender.servlet.HelloServlet</servlet-class><!-- servlet关联的类 --> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/</url-pattern><!-- servlet访问路径 --> </servlet-mapping>
四,ServletConfig
ServletConfig代表当前Servlet在web.xml中的配置信息。
1,Servlet初始化参数
在Servlet的配置文件中,可以使用一个或多个<init-param>标签为servlet配置一些初始化参数。
例如:
<servlet> <servlet-name>HelloServlet</servlet-name><!-- servlet名称 --> <servlet-class>com.zender.servlet.HelloServlet</servlet-class><!-- servlet关联的类 --> <init-param> <param-name>name</param-name> <param-value>Zender</param-value> </init-param> <init-param> <param-name>encode</param-name> <param-value>UTF-8</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello.html</url-pattern><!-- servlet访问路径 --> </servlet-mapping>
2,通过ServletConfig获取Servlet的初始化参数
当servlet配置了初始化参数后,web容器在创建servlet实例对象时,会自动将这些初始化参数封装到ServletConfig对象中,并在调用servlet的init方法时,将ServletConfig对象传递给servlet。进而,通过ServletConfig对象就可以得到当前servlet的初始化参数信息。
public class HelloServlet extends HttpServlet{ private static final long serialVersionUID = 3903946972744326948L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取ServletConfig ServletConfig config = this.getServletConfig(); //获取初始化参数 String name = config.getInitParameter("name"); String encode = config.getInitParameter("encode"); //设置编码格式,否则中文会出现乱码 resp.setCharacterEncoding("GBK"); PrintWriter out = resp.getWriter(); out.println("name:" + name); out.println("encode:" + encode); } }
五,ServletContext
WEB容器在启动时,它会为每个WEB应用程序都创建一个对应的ServletContext对象,它代表当前web应用。
ServletConfig对象中维护了ServletContext对象的引用,开发人员在编写servlet时,可以通过ServletConfig.getServletContext()方法获得ServletContext对象。
由于一个WEB应用中的所有Servlet共享同一个ServletContext对象,因此Servlet对象之间可以通过ServletContext对象来实现通讯。ServletContext对象通常也被称之为context域对象。
1,获取WEB应用的初始化参数
在web.xml文件中使用<context-param>标签配置WEB应用的初始化参数:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ServletDemo</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置WEB应用的初始化参数 --> <context-param> <param-name>name</param-name> <param-value>Zender</param-value> </context-param> <context-param> <param-name>encode</param-name> <param-value>UTF-8</param-value> </context-param> <servlet> <servlet-name>HelloServlet</servlet-name><!-- servlet名称 --> <servlet-class>com.zender.servlet.HelloServlet</servlet-class><!-- servlet关联的类 --> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello.html</url-pattern><!-- servlet访问路径 --> </servlet-mapping> </web-app>
获取Web应用的初始化参数:
public class HelloServlet extends HttpServlet{ private static final long serialVersionUID = 3903946972744326948L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取ServletContext ServletContext context = this.getServletContext(); //获取初始化参数 String name = context.getInitParameter("name"); String encode = context.getInitParameter("encode"); //设置编码格式,否则中文会出现乱码 resp.setCharacterEncoding("GBK"); PrintWriter out = resp.getWriter(); out.println("name:" + name); out.println("encode:" + encode); } }
2,多个Servlet通过ServletContext对象实现数据共享
例如 :ServletTwo获得ServletOne所共享的数据
ServletOne:
public class ServletOne extends HttpServlet{ private static final long serialVersionUID = 3903946972744326948L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //共享的数据 String name = "zender"; //获取ServletContext ServletContext context = this.getServletContext(); //将name存储到ServletContext对象中 context.setAttribute("name", name); } }
ServletTwo:
public class ServletTwo extends HttpServlet{ private static final long serialVersionUID = 3903946972744326948L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取ServletContext ServletContext context = this.getServletContext(); //获取初始化参数 String name = (String) context.getAttribute("name"); PrintWriter out = resp.getWriter(); out.println("name:" + name); } }
Web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ServletDemo</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>ServletOne</servlet-name><!-- servlet名称 --> <servlet-class>com.zender.servlet.ServletOne</servlet-class><!-- servlet关联的类 --> </servlet> <servlet-mapping> <servlet-name>ServletOne</servlet-name> <url-pattern>/ServletOne.html</url-pattern><!-- servlet访问路径 --> </servlet-mapping> <servlet> <servlet-name>ServletTwo</servlet-name><!-- servlet名称 --> <servlet-class>com.zender.servlet.ServletTwo</servlet-class><!-- servlet关联的类 --> </servlet> <servlet-mapping> <servlet-name>ServletTwo</servlet-name> <url-pattern>/ServletTwo.html</url-pattern><!-- servlet访问路径 --> </servlet-mapping> </web-app>
先访问:http://localhost:8081/ServletDemo/ServletOne.html
再访问:http://localhost:8081/ServletDemo/ServletTwo.html
3,用servletContext实现请求转发
context.getRequestDispatcher()方法实现请求转发
ServletOne:
public class ServletOne extends HttpServlet{ private static final long serialVersionUID = 3903946972744326948L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //共享的数据 String name = "zender"; //获取ServletContext ServletContext context = this.getServletContext(); //将name存储到ServletContext对象中 context.setAttribute("name", name); context.getRequestDispatcher("/ServletTwo.html").forward(req, resp); } }
ServletTwo:
public class ServletTwo extends HttpServlet{ private static final long serialVersionUID = 3903946972744326948L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取ServletContext ServletContext context = this.getServletContext(); //获取初始化参数 String name = (String) context.getAttribute("name"); PrintWriter out = resp.getWriter(); out.println("name:" + name); } }
Web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ServletDemo</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>ServletOne</servlet-name><!-- servlet名称 --> <servlet-class>com.zender.servlet.ServletOne</servlet-class><!-- servlet关联的类 --> </servlet> <servlet-mapping> <servlet-name>ServletOne</servlet-name> <url-pattern>/ServletOne.html</url-pattern><!-- servlet访问路径 --> </servlet-mapping> <servlet> <servlet-name>ServletTwo</servlet-name><!-- servlet名称 --> <servlet-class>com.zender.servlet.ServletTwo</servlet-class><!-- servlet关联的类 --> </servlet> <servlet-mapping> <servlet-name>ServletTwo</servlet-name> <url-pattern>/ServletTwo.html</url-pattern><!-- servlet访问路径 --> </servlet-mapping> </web-app>
访问:http://localhost:8081/ServletDemo/ServletOne.html
访问的是ServletOne.html,浏览器显示的却是ServletTwo.html的内容,这就是使用ServletContext实现了请求转发。
4,利用ServletContext对象读取资源文件
获取上图的config.properties资源文件内容:
GetProperties.Java:
public class GetProperties extends HttpServlet{ private static final long serialVersionUID = 3903946972744326948L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //设置浏览器编码格式为UTF-8 resp.setHeader("content-type","text/html;charset=UTF-8"); resp.getWriter().println("读取src目录下的config.properties配置文件:"); //读取src目录下的properties配置文件 readProperties(resp,"/WEB-INF/classes/config.properties"); resp.getWriter().println("<hr/>读取WebRoot目录下的config.properties配置文件:"); //读取WebRoot目录下的properties配置文件 readProperties(resp,"config.properties"); } /** * * @方法名: readProperties * @描述: 获取properties配置文件内容 * @param resp * @param fileUrl * @throws IOException * @创建人 zender */ protected void readProperties(HttpServletResponse resp, String fileUrl) throws IOException{ //2种方式都能获取到config.properties //InputStream in = new FileInputStream(this.getServletContext().getRealPath(fileUrl)); InputStream in = this.getServletContext().getResourceAsStream(fileUrl); Properties prop = new Properties(); prop.load(in); String url = prop.getProperty("database.url"); String username = prop.getProperty("database.username"); String password = prop.getProperty("database.password"); resp.getWriter().println( MessageFormat.format("url={0},username={1},password={2}", url,username, password) ); } }
Web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ServletDemo</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>GetProperties</servlet-name><!-- servlet名称 --> <servlet-class>com.zender.servlet.GetProperties</servlet-class><!-- servlet关联的类 --> </servlet> <servlet-mapping> <servlet-name>GetProperties</servlet-name> <url-pattern>/GetProperties.html</url-pattern><!-- servlet访问路径 --> </servlet-mapping> </web-app>
访问路径:http://localhost:8081/ServletDemo/GetProperties.html