servlet配置信息:
konglinglei
haha
注意:配置信息是初始化配置信息(加载完Servlet时初始化),所以要添加在Servlet中,且要加在Servlet中的servlet-class标签后,不能在前面,否则会出错. |
获取方式一:声明成员变量来保存ServletConfig对象
这种方式需要初始化servlet配置信息,
配置代码如下:
practice
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
kll
kll.com.Kll
key
value
kll
/kll
代码例子:
package kll.com;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Kll extends HttpServlet {
// 声明成员变量
private ServletConfig config;
// 初始化servlet配置信息
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config);
// 接收参数中的配置对象
this.config = config;
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String value = config.getInitParameter("key");
System.out.println(value);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
运行结果如下:
获取方式二:通过父类中的方法 直接获取ServletConfig对象
代码如下:
package kll.com;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Kll extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletConfig config = this.getServletConfig();
String value = config.getInitParameter("key");
System.out.println(value);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
运行结果与上边一样.
①使用绝对路径 /kll (常用)
②使用匹配后缀 *.后缀名 *.action
③直接使用 : / * 匹配 只要是工程下的都能访问到
④注意:2和3不能放一起使用
⑤直接使用 / 匹配 (注意:/匹配一般不使用,易出错,系统默认的是使用/匹配的)
域对象有4个.
context获取方式一:通过ServletConfig对象获取
在doGet方法中使用 |
this.getServletConfig().getServletContext(); |
获取并接收 |
获取context域方式二:直接通过父类获取
this.getServletContext()
也是在doGet方法中.
设置全局配置信息
在display-name标签后设置,关键代码如下:
lei
啦啦
获取全局配置信息
application.getInitParameter(" ")
字符串填的是:xml中配置的全局信息中的,context-name标签内的字符串.
存值
this.getServletContext().setAttribute("key", "kl");// (字符串填你需要的)
取值
this.getServletContext().getServletContext().getAttribute("key"); |
字符串填你存进去的key值的字符串 |
获取文件在服务器上的真实路径
package kll.com;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Kll extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext application = this.getServletContext();
String realPath = application.getRealPath("/WEB-INF/classes/kll/com/a.properties");
System.out.println(realPath);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
如果要读取文件内容使用properties集合,路径使用获取到的真实路径 |
Properties properties = new Properties();
FileReader fr = new FileReader(realPath);
properties.load(fr);
String value = properties.getProperty("key");
// 关流 只需要关闭自己创建的流,获取的不需要自己关闭
fr.close();
1.利用context域进行请求转发
从Kll转发到Kll1,代码如下:
Kll类的代码:
package kll.com;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Kll extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("请求转发到kll1");
System.out.println("可以吗?");
ServletContext application = this.getServletContext();
// 获取请求转发器
RequestDispatcher dispatcher = application.getRequestDispatcher("/kll1");
// 发送转发的请求
dispatcher.forward(request, response);
System.out.println("转发结束!");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Kll1类的代码:
package kll.com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Kll1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("已经转发到了kll1,成功转发!");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
可以在站内或站外响应.
设置浏览器解析响应的编码格式方式
①方式一
// 设置编码格式
response.setCharacterEncoding("utf8");
// 设置响应头 告诉浏览器以什么编码格式来解析响应
response.setHeader("Content-type", "text/html;charset=utf8");
②方式二
// 相当于上边的两句二合一
response.setContentType("text/html;charset=utf8");
注意:只针对字符串形式需要设置
利用response获取字节流和字符流
// 字符流
PrintWriter writer = response.getWriter();
writer.write("孔令蕾");
// 字节流
ServletOutputStream os = response.getOutputStream();
下载文件(图片)
需要设置图片名编码格式 iso-8859-1
通过设置响应头 来告诉浏览器 我给你资源 下载用
key:content-disposition value:attachment;filename=图片名
package kll.com;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Kll extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 下载图片
// 获取图片在服务器上的真实路径
ServletContext application = this.getServletContext();
String path = application.getRealPath("/WEB-INF/classes/哈哈.png");
// 通过file类来获取文件名
File file = new File(path);
String fileName = file.getName();
// 需要设置图片名编码格式 iso-8859-1
byte[] bytes = fileName.getBytes();
fileName = new String(bytes, "iso-8859-1");
// 通过设置响应头 来告诉浏览器 我给你资源 下载用
// key:content-disposition value:attachment;filename=图片名
response.setHeader("content-disposition", "attachment;filename=" + fileName);
// 设置下载内容的格式(去web.xml中查找资源后缀的格式)
response.setHeader("content-type", "image/png");
// 使用字节流读取图片
FileInputStream fis = new FileInputStream(path);
// 使用响应中的字节流 将图片写回浏览器
ServletOutputStream os = response.getOutputStream();
byte[] bs = new byte[1024];
int len = 0;
while ((len = fis.read(bs)) != -1) {
// 将图片写回浏览器
os.write(bs, 0, len);
}
// 关流
// 自己创建的自己关(获取系统的不用管)
fis.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
刷新设置
// 3秒后刷新页面(刷新头)
// 站内响应
response.setHeader("refresh", "3;url=/practice/kll");
// 站外响应
//response.setHeader("refresh", "3;url=http://www.baidu.com");
// 每隔一秒 刷新界面
// response.setIntHeader("refresh", 1);
// 回写数据测试
PrintWriter writer = response.getWriter();
writer.write(Math.random() + " ");
通过响应response 请求重定向
可以进行站内重定向 相对于8080后的斜杠(需要带上工程名) ;
也可以进行站外重定向
以下是从Kll站内重定向到Kll1的代码:
Kll1代码不变,和上面的一样.
package kll.com;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Kll extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("请求转发到kll1");
System.out.println("可以吗?");
// 可以进行站内重定向 相对于8080后的斜杠(需要带上工程名)
response.setHeader("location", "/practice/kll1");
// 也可以进行站外重定向
// response.setHeader("location", "http://www.baidu.com");
// 添加重定向的状态码
response.setStatus(302);
System.out.println("转发结束!");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
运行结果图:
request请求
获取请求的方式(get/post)
// GET
String method = request.getMethod();
System.out.println(method);
获取用户请求的URL(统一资源定位符)
// http://localhost:8080/practice/kll
StringBuffer url = request.getRequestURL();
System.out.println(url);
获取用户请求的URI(统一资源标识符)
// /practice/kll
String uri = request.getRequestURI();
System.out.println(uri);
获取相对路径
// /practice (工程名)
String path = request.getContextPath();
System.out.println(path);
获取用户请求的参数
// 参数是:传入的相当于key
// http://localhost:8080/practice/kll?username=konglinglei&password=123456
String username = request.getParameter("username");
String password = request.getParameter("password");
System.out.println(username);
System.out.println(password);