创建ServletDemo03
package com.Li.servlet;
//P11的学习,测试获取web.xml中context-param的内容
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 ServletDemo03 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 {
doGet(req, resp);
}
}
web.xml :context-param与注册
url jdbc:mysql://localhost:3306/mybatis hello com.Li.servlet.HelloServlet hello /hello getc com.Li.servlet.GetServlet getc /getc gp com.Li.servlet.ServletDemo03 gp /gp
Demo03效果:
ServletDemo04:转发效果
package com.Li.servlet;
import javax.servlet.RequestDispatcher;
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 ServletDemo04 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
System.out.println("进入了ServletDemo04");
//RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp");
//requestDispatcher.forward(req, resp); 这两句等同于下面这句
context.getRequestDispatcher("/gp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
Demo04的注册:
sd4 com.Li.servlet.ServletDemo04 sd4 /sd4
效果:实现了转发。但是url路径还是用自己的。
关系:
A想要C的资源,但是自己只能和B交互。于是转发过程为:ABCBA 。
*读取资源文件(重点)*:建立:resources下建立文件。======CSDN也太垃圾了。去不了水印,很影响效果。。。。
文件里面的内容为:
Demo05
package com.Li.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;
import java.io.InputStream;
import java.util.Properties;
public class ServletDemo05 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取web文件中properties的内容。并将它输出为一个流
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
//读取流中的内容
Properties properties = new Properties();
properties.load(is);
String username = properties.getProperty("username");
String password = properties.getProperty("password");
//返回前端打印结果。
resp.getWriter().print(username+":"+password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
配置web.xml
sd5 com.Li.servlet.ServletDemo05 sd5 /sd5
效果:



