一、设置全局配置信息
public class Demo01 extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// fun1();
//获取全局配置信息
ServletContext appliction = this.getServletContext();
String key = appliction.getInitParameter("name");
System.out.println(key);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
private void fun1() {
//取出servlet信息,首先,获取ConfigServlet
ServletConfig config = this.getServletConfig();
//取出配置信息,通过键取出对应的值
String value = config.getInitParameter("username");
System.out.println(value)
}
}
Demo01的配置信息
<context-param>
<param-name>name</param-name>
<param-value>value</param-value>
</context-param>
<servlet>
<servlet-name>demo01</servlet-name>
<servlet-class>com.lanou3g.com.Demo01</servlet-class>
<init-param>
<param-name>username</param-name>
<param-value>wanglong</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>demo01</servlet-name>
<url-pattern>/demo01</url-pattern>
</servlet-mapping>
二、读取文件
public class Demo02 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
fun3();
}
private void fun3() throws IOException, FileNotFoundException {
ServletContext context = this.getServletContext();
String path = context.getRealPath("/WEB-INF/c.properties");
System.out.println(path);
Properties properties = new Properties();
properties.load(new FileInputStream(path));
System.out.println(properties.getProperty("key"));
}
private void fun2() throws IOException, FileNotFoundException {
ServletContext context = this.getServletContext();
String path = context.getRealPath("/WEB-INF/classes/com/lanou3g/b.properties");
System.out.println(path);
Properties properties = new Properties();
properties.load(new FileInputStream(path));
System.out.println(properties.getProperty("key"));
}
private void fun1() throws IOException, FileNotFoundException {
ServletContext context = this.getServletContext();
String path = context.getRealPath("WEB-INF/classes/a.properties");
System.out.println(path);
Properties properties = new Properties();
properties.load(new FileInputStream(path));
System.out.println(proerties.getProperty("key"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
三、请求转发
1.浏览器只是发送了一次请求,servlet内部做了请求转发,浏览器/用户并不知道
public class Demo03 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("我要借钱");
System.out.println("没有,我去找demo04借给你");
ServletContext context =this.getServletContext();
RequestDispatcher dispatcher = context.getRequestDispatcher("/demo04");
dispatcher.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("我有钱,我demo04,借给你了Demo03了兄弟");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
四、HttpServletResponse服务器响应对象
public class Demo05 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html/;charset=UTF-8");
PrintWriter out = response.getWriter();
out.write("很开心");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
下载文件:用户发送请求,请求到访问的servlet
servlet处理请求:把服务器上图片,以流的形式,使用response,响应给用户浏览器
public class Demo06 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path = this.getServletContext().getRealPaht("/WEB-INF/classes/团子大家族.pnt");
int index = path.lastIndexOf("/");
String filename = 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);
}
}
五、请求重定向
1.响应要注意的细节:
a)从response中获取的字符流和字节流不能同时使用
b)在同一个servlet不能同时使用
public class Demo07 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
private void fun3(HttpServletResponse response) throws IOException {
response.setContentType("text/html;charset=UTF-8");
response.setHeader("refresh", "3;url=/sh0web-day02/demo08");
response.getWriter().write("3秒后跳转");
}
private void fun2(HttpServletResponse response) throws IOException {
response.setIntHeader("refresh", 1);
response.getWriter().write(Math.random() + "");
}
private void fun1(HttpServletResponse response) {
System.out.println("我要借钱");
System.out.println("我没有去找demo08");
response.setHeader("location", "/sh0web-day02/demo08");
response.setStatus(302);
System.out.println("我去了");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
六。、HttpServletRequest 用户的请求对象
public class Demo08 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("我借了");
System.out.println(request.getRequestURL());
System.out.println(request.getRequestURI());
System.out.println(request.getMethod());
System.out.println(request.getContextPath());
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username);
System.out.println(password);
String header = request.getHeader("User-Agent");
if (header.toLowerCase().contains("firefox")) {
System.out.println("用的是火狐浏览器");
}else if (header.toLowerCase().contains("chrome")) {
System.out.println("用的是谷歌浏览器");
}else {
System.out.println("其他浏览器");
}
System.out.println(header);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}