web
Servlet
- 例子
创建一个servlet类让其继承Servlet
public class HelloServlet implements Servlet {
public HelloServlet() {
System.out.println("构造器");
}
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("init");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println(" service helloServlet");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
System.out.println("destroy");
}
}
编写配置文件web.xml
<?xml version="1.0" encoding="UTF-8"?>
HelloServlet
com.hyq.servert.HelloServlet
HelloServlet
/hello
浏览器输入地址:http://localhost:8080/java_02_web_Web/hello
控制台打印的是servlet类中service方法中的结果:helloServlet
servlet声明周期,执行顺序:构造器-->init()-->service()--->destroy()
仍然用上面的servlet类,访问http://localhost:8080/java_02_web_Web/hello时
浏览器打印了如下结果:
构造器
init
service helloServlet
服务未关闭情况下,再次访问http://localhost:8080/java_02_web_Web/hello时,只会执行service的方法。在关闭服务时,执行destroy()方法
servletContext的作用
① 获取web.xml中配置的上下文参数context-param
② 获取当前的工程路径,格式:/工程路径
③ 获取工程部署后在服务器硬盘上的绝对路径
④ 像Map一样存储数据
⑤ SerletContext是一个接口,它表示Serlet上下文对象
⑥ 一个web工程,只有一个SerletContext对象实例。
⑦ ServletContext是在web工程部署启动的时候创建。在web工程停止的时候销毁。
例子:
编写一个servlet类,继承HttpServlet
public class ServletContext extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
javax.servlet.ServletContext context = getServletContext();
//获取web.xml中配置的上下文参数 context-param
String url = context.getInitParameter("url");
System.out.println(url); // www.baidu.com
// 企图获取 init-param 参数值;getInitParameter()方法只能获取 context-param 里面的值
System.out.println(context.getInitParameter("name")); //null
//获取当前工程路径 格式:/工程名称
System.out.println(context.getContextPath()); // 【/java_02_web_Web】
//获取工程部署后 在服务器硬盘上的绝对路径
//【E:\CodeMgr\ideacode\javastudy\out\artifacts\java_02_web_Web_exploded\】
System.out.println(context.getRealPath("/"));
}
}
配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
ServletContext
com.hyq.servert.ServletContext
namespace
hyq
ServletContext
/ServletContext
url
www.baidu.com
例子
- 编写
web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
ServletContext
com.hyq.servert.ServletContext
ServletContext2
com.hyq.servert.ServletContext2
ServletContext
/ServletContext
ServletContext2
/ServletContext2
- 编写
servlet类继承httpServlet类
public class ServletContext extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
javax.servlet.ServletContext context = getServletContext();
Object name = context.getAttribute("name");
System.out.println("name="+name);
context.setAttribute("name","hyq");
System.out.println("name="+context.getAttribute("name"));
}
}
- 编写
servlet2类继承httpServlet类
public class ServletContext2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletContext();
System.out.println("name="+context.getAttribute("name"));
}
}
在地址栏输入地址:http://localhost:8080/java_02_web_Web/ServletContext
访问结果:
name=null
name=hyq
服务没有停止时,在地址栏输入地址:http://localhost:8080/java_02_web_Web/ServletContext2
访问结果:
name=hyq
再此在地址栏输入地址:http://localhost:8080/java_02_web_Web/ServletContext
访问结果:
name=hyq
name=hyq
结论:ServletContext启动后,工程中只存在一个
Get/Post请求
响应的Http协议格式
1、响应行
(1)响应的协议和版本号
(2)响应状态码
(3)响应状态描述符
2、响应头
key:value 不同的响应头,有其不同合义
空行
3、响应体: 就是回传给客户端的数据
Servlet的请求转发和重定向
编写Servlet1和Servlet2类
//Servlet1
public class Servlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取参数值
String name = req.getParameter("name");
//把参数存入request中
req.setAttribute("myName",name);
//请求转发到服务器2
// 请求转发必须以【/】打头./在服务器转发时代表的地址是 http://ip:port/工程名
RequestDispatcher dispatcher = req.getRequestDispatcher("/servlet2");
dispatcher.forward(req,resp); //转发
}
}
//Servlet2
public class Servlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("name");
System.out.println("myName=="+name);
}
}
编写web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
Servlet1
com.hyq.servert.Servlet1
Servlet1
/servlet1
Servlet2
com.hyq.servert.Servlet2
Servlet2
/servlet2
测试:在地址栏输入地址:http://localhost:8080/java_02_web/servlet1?name=hyq
结果是:myName==hyq
请求转发的结论:
① 地址栏没有发生变化
② 从A转发到B 共享Request的数据,且是一次请求
③ 可以转发到WEB-INF目录下,正常访问,不允许访问WEB-INF目录下的文件
④ 不允许访问 站外资源,如www.baidu.com 。
重定向
web 中 / 的作用
① / 如果被浏览器解析,得到的地址是 http://ip:port/;即当前站点
如:
斜杠
跳转
② / 如果被服务器解析,得到的地址是 http://ip:port/工程名称
如:
1. /ServletContext2
2. ServletContext.getRealPath("/")
3. req.getRequestDispatcher("/");
特殊情况:response.sendRediect("/"); 把斜杠发送给浏览器解析,得到的地址是:http://ip:port/
Servlet解决中文乱码
//设置服务器编码
response.setCharacterEncoding("utf-8");
//告诉浏览器 使用什么编码
response.setHeader("Content-Type","text/html;charset=UTF-8");