web.xml
url jdbc:mysql://localhost:3306/mybatis hello com.gongyi.servlet.HelloServlet hello /hello getp com.gongyi.servlet.ServletDemo03 getp /getp
ServletDemo03.java
package com.gongyi.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 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().println(url);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3、请求转发
ServletDemo04.java
package com.gongyi.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);//调用forward实现请求转发
context.getRequestDispatcher("/gp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
web.xml
url jdbc:mysql://localhost:3306/mybatis hello com.gongyi.servlet.HelloServlet hello /hello getp com.gongyi.servlet.ServletDemo03 getp /getp sd4 com.gongyi.servlet.ServletDemo04 sd4 /sd4
转发与重定向图解
4、读取资源文件Properties
-
在java目录下新建properties
-
在resources目录下新建properties(解决资源文件未打包到目标路径问题)
在pom.xml中添加如下
src/main/java ***.xml false
发现:都被打包到了同一个路径下:classes,我们俗称这个路径为classpath
思路:需要一个文件流
db.properties
username=root password=123456
ServletDemo05.java
package com.gongyi.servlet;
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 {
InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
//InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/com/gongyi/servlet/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);
}
}
web.xml
url jdbc:mysql://localhost:3306/mybatis hello com.gongyi.servlet.HelloServlet hello /hello getp com.gongyi.servlet.ServletDemo03 getp /getp sd4 com.gongyi.servlet.ServletDemo04 sd4 /sd4 sd5 com.gongyi.servlet.ServletDemo05 sd5 /sd5
访问测试即可
彩蛋1,在maven web中,classpath包含类文件和资源文件
2.maven web打包后,观察target目录结构



