获取全局配置信息
XML文件中配置全局配置信息
<context-param>
<param-name>key</param-name>
<param-value>value</param-value>
</context-param>
<servlet>
<servlet-name>demo01</servlet-name>
<servlet-class>com.lanou3g.Demo01</servlet-class>
<init-param>
<param-name>username</param-name>
<param-value>xiaoming</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>demo01</servlet-name>
<url-pattern>/demo01</url-pattern>
</servlet-mapping>
Java代码中
public class Demo01 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext application = this.getServletContext();
String value = application.getInitParameter("key");
System.out.println(value);
}
private void fun1() {
ServletConfig config = this.getServletConfig();
String value = config.getInitParmeter("username");
System.out.println(value);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
获取服务器上的真实路径并读取
/WEB-INF/classes/ 在这个路径下创建一个文件 a.properties
在XML文件中配置Demo02
<servlet>
<servlet-name>demo02</servlet-name>
<servlet-class>com.lanou3g.Demo02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo02</servlet-name>
<url-pattern>/demo02</url-pattern>
</servlet-mapping>
public class Demo02 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
private void fun1() throws IOException, FileNotFoundException {
ServletContext application = this.getServletContext();
String path = application.getRealPath("/WEB-INF/classes/a.properties")
Properties properties = new Properties();
properties.load(new FileInputStream(path));
System.out.println(properties.getProperty("key"));
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
请求转发
请求转发
注意:浏览器只是发起了一次请求
Servlet内部做的请求转发 浏览器 并不知道
在XML文件中 配置Demo03 Demo04
<servlet>
<servlet-name>demo03</servlet-name>
<servlet-class>com.lanou3g.Demo03</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo03</servlet-name>
<url-pattern>/demo03</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>demo04</servlet-name>
<servlet-class>com.lanou3g.Demo04</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo04</servlet-name>
<url-pattern>/demo04</url-pattern>
</servlet-mapping>
public class Demo03 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("我要借钱");
System.out.println("我没钱, demo4可能有");
ServletContext application = this.getServletContext();
RequestDispatcher requestDispatcher = application.getRequesDispatcher;
requestDispatcher.forward(request,response);
System.out.println("钱我搞定了");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
public class Demo04 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("我demo4 有钱");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
服务器的响应对象
HttpServlerResponse 服务器的响应对象
响应对象中又什么
响应行
响应头 告诉浏览器要做什么 例如: 响应给你的文件需要下载 以什么编码格式解析数据
响应体 响应回浏览器的数据
在XML文件中配置Demo05
<servlet>
<servlet-name>demo05</servlet-name>
<servlet-class>com.lanou3g.Demo05</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo05</servlet-name>
<url-pattern>/demo05</url-pattern>
</servlet-mapping>
public class Demo5 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWrite out = response.getWrite();
out.write("你好");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
下载文件
在XML中配置Demo06
<servlet>
<servlet-name>demo06</servlet-name>
<servlet-class>com.lanou3g.Demo06</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo06</servlet-name>
<url-pattern>/demo06</url-pattern>
</servlet-mapping>
public class Demo06 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext application = this.getServletContext();
String path = application.getRealPath("/WEB-INF/classes/团子大家族.png");
int index = path.lastIndexOf("/");
String file那么 = path.substring(index + 1);
filename = new String(filename.getBytes(), "iso-8859-1");
response.setHeader("content-disposition","attachment;filename=" + filename);
response.setHeader("content-type","image/png");
FileInputStream fis = new FileInPutStream(path);
ServletOutPutStream sos = response.getOutputstream();
int len = 0;
byte[] b = new byte[1024];
while((len = fis.read(b)) != -1){
sos.write(b, 0, len);
}
fis.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
请求重定向
请求重定向
浏览器发起请求(请求Servlet)
Servlet给浏览器一个响应
在响应中会携带一个重定向响应头(头中有重定向的访问地址)
再一次发起请求 去访问重定向头中的地址
请求重定向和请求转发的区别
请求重定向是发起了两次请求(请求地址发生了变化)
请求转发只发起了一次请求
响应时要注意的细节
从response中获取的字符流和字节流 在同一个Servlet中 不能同时使用
在XML文件中配置Demo07 Demo08
<servlet>
<servlet-name>demo07</servlet-name>
<servlet-class>com.lanou3g.Demo07</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo07</servlet-name>
<url-pattern>/demo07</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>demo08</servlet-name>
<servlet-class>com.lanou3g.Demo08</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo08</servlet-name>
<url-pattern>/demo08</url-pattern>
</servlet-mapping>
public class Demo07 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("我要借钱");
System.out.println("我没钱,找Demo08");
response.setHeader("location", "/SH-web-servlet-02/demo08");
response.setStatus(302);
System.out.println("我去了");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
public class Demo08 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("我借钱给你");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
刷新页面
在XML文件中配置Demo09
<servlet>
<servlet-name>demo09</servlet-name>
<servlet-class>com.lanou3g.Demo09</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo09</servlet-name>
<url-pattern>/demo09</url-pattern>
</servlet-mapping>
public class Demo09 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setIntHeader("refresh", 1);
response.getWriter().write(Math.random() + "");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
跳转网页
在XML文件中配置Demo010
<servlet>
<servlet-name>demo10</servlet-name>
<servlet-class>com.lanou3g.Demo10</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo10</servlet-name>
<url-pattern>/demo10</url-pattern>
</servlet-mapping>
public class Demo10 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
response.setHeader("refresh", "3;url=/SH-web-servlet-02/demo08");
response.getWriter().write("3秒后跳转");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}