Servlet学习
1.请求转发
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
context.getRequestDispatcher("/gp").forward(req,resp);
}
1.请求转发时,页面不变,地址变化
2.读取资源文件
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext context = this.getServletContext();
InputStream is = context.getResourceAsStream("/WEB-INF/classes/db.properties");
Properties prop = new Properties();
prop.load(is);
String name = prop.getProperty("username");
String pwd = prop.getProperty("password");
resp.getWriter().print(name+":"+pwd);
}
1.在resources目录下创建db.properties
2.如果需要在java目录下创建properties可以在pom.xml中加上
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
1864

被折叠的 条评论
为什么被折叠?



