Servlet技术
1)什么是servlet
1、Servlet是JavaEE规范之一。规范就是接口
2、Servlet就javaweb三大组件之一。三大组件分别是:Servlet程序、filter过滤器、listenner监听器。
3、Servlet是运行在服务器上的一个java小程序,它可以接受客户端发送过来的请求,并响应数据给客户端
2)手动实现Servlet程序
1、编写一个类去实现Servlet接口
2、实现service方法,处理请求,并响应数据
3、到web.xml中去配置servlet程序的访问地址
编辑项目名称
新建包 com.mafei.servlet
新建class HelloServlet
最终的目录结构:
package com.mafei.servlet;
import javax.servlet.*;
import java.io.IOException;
public class HelloServlet implements Servlet {
public HelloServlet() {
System.out.println("1、构造器");
}
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("2、init 方法");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
/**
* service方法是专门用来处理请求和响应信息的
* @param servletRequest
* @param servletResponse
* @throws ServletException
* @throws IOException
*/
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("3、 Hello Servlet 被访问了。。。。。");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
System.out.println("4、destroy 销毁方法");
}
}
web.xml配置
HelloServlet
com.mafei.servlet.HelloServlet
HelloServlet
/hello
2)Servlet的生命周期:
1、执行Servlet构造器方法
2、执行init初始化方法
第一、二步,是在第一次访问的时候创建Servlet程序
3、执行service方法
第三步,每次访问都会调用。
4、执行destory销毁方法
第四步,在web工程停止的时候调用
3)工程中调用的HttpServlet 方法
新建一个HelloServlet类
package com.mafei.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HelloServlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("访问了 doGet方法");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("访问了do Post 方法");
}
}
web.xml中增加配置
HelloServlet2
com.mafei.servlet.HelloServlet2
HelloServlet2
/hello2
使用IDEA创建servlet
web.xml增加配置:
HelloServlet3
com.mafei.servlet.HelloServlet3
HelloServlet3
/hello3
Servlet config三大作用:
以HelloServlet类举例:
HelloServlet类的init方法:
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("2、init 方法");
// 1、可以获取servlet程序的别名servlet-name的值
System.out.println("HelloServlet程序的别名是:"+servletConfig.getServletName());
// 2、获取初始化参数init-param
System.out.println("初始化参数username的值是:"+servletConfig.getInitParameter("username"));
System.out.println("初始化参数url的值是:"+servletConfig.getInitParameter("url"));
//3、获取servlet content对象
System.out.println(servletConfig.getServletContext());
}
web.xml中关于HelloServlet的配置:
HelloServlet
com.mafei.servlet.HelloServlet
username
root
url
jdbc:mysql://localhost:3306/test
最终访问效果:
HelloServlet程序的别名是:HelloServlet
初始化参数username的值是:root
初始化参数url的值是:jdbc:mysql://localhost:3306/test
org.apache.catalina.core.ApplicationContextFacade@5ba9dd28
在其他类中,重写servlet 的init方法:
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
System.out.println("重写了init初始化方法,做了一些工作!");
}
ServletContent类
1、什么是ServletContext?
a)ServletContext是一个接口,它表示Servlet上下文对象
b)一个web工程,只有一个ServletContext对象实例。
c)ServletContext对象是一个域对象
d)ServletContext是在web工程部署启动的时候创建。在web工程停止的时候销毁
什么是域对象?
域对象,是开源像map一样存取数据的对象,叫域对象。
这里的域指的是存取数据的操作范围,是整个web工程
存数据 取数据 删除数据
Map put() get() remove()
域对象 setAttribute() getAttribute() removeAttribute();
2、ServletContext类的四个作用
a)获取web.xml中配置的上下文参数 context-param
b)获取当前的工程路径,格式:/工程路径
c)获取工程部术后在服务器硬盘上的绝对路径
d)像Map一样存取数据
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext context = getServletConfig().getServletContext();
// a)获取web.xml中配置的上下文参数 context-param
String username = context.getInitParameter("username");
System.out.println("context-param参数username的值是:"+username);
System.out.println("context-param参数password的值是:"+context.getInitParameter("password"));
// b)获取当前的工程路径,格式:/工程路径
System.out.println("当前工程路径:"+context.getContextPath());
// c)获取工程部术后在服务器硬盘上的绝对路径
System.out.println("工程部署的路径是:"+ context.getRealPath("/"));
System.out.println("工程的1.css绝对路径是:"+context.getRealPath("/css/1.css"));
}
在web.xml中增加跟servlet平级的配置::
输出效果:
context-param参数username的值是:root
context-param参数password的值是:123
当前工程路径:/web02
工程部署的路径是:C:\Users\tophant\IdeaProjects\web02\out\artifacts\web02_war_exploded\
工程的1.css绝对路径是:C:\Users\tophant\IdeaProjects\web02\out\artifacts\web02_war_exploded\css\1.css
context.setAttribute("key1","----");
Object key1 = context.getAttribute("key1");
System.out.println("获取到的key1, 这个key1可以在其他工程中也可以看到");
HttpServletRequest类说明:
1、HttpServletRequest类有什么作用。
每次只要有请求进入到tomcat服务器,tomcat服务器就会把请求过来的http协议信息解析好封装到request对象中。然后传递到service方法(doGet和doPost)中给我们使用。我们可以通过HttpServletRequest对象,获取到所有请求的信息
2、HttpServletRequest类常用方法:
1)getRequestURI() 获取请求的资源路径
2)getRequestURL() 获取绝对路径
3)getRemoteHost() 获取客户端的ip地址
4)getHeader() 获取请求头
5)getParameter() 获取请求的参数
6)getParameterValues() 获取请求的参数(多个值的时候使用)
7) getMethod() 获取请求的方法GET或POST
8)getAttribute(key); 获取域数据
9) getRequestDispatcher() 获取请求转发对象
Servlet 接收参数:
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置请求体的字符集为UTF-8,从而解决post请求的中文乱码问题
req.setCharacterEncoding("UTF-8");
// 获取请求参数
String username = req.getParameter("username");
String password = req.getParameter("password");
String[] hobby = req.getParameterValues("hobby");
System.out.println("用户名:"+username);
System.out.println("密码:"+password);
System.out.println("兴趣:"+ Arrays.asList(hobby));
}
web.xml
ParameterServlet
com.mafei.www.ParameterServlet
ParameterServlet
/parameterServlet
Servlet 转发请求
Servlet1的doGet方法:
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object username = req.getParameter("username");
System.out.println("Servlet1接收到的参数:"+username);
req.setAttribute("key1", username);
// 转发给servlet2
RequestDispatcher dispatcher = req.getRequestDispatcher("/servlet2");
dispatcher.forward(req, resp);
}
Servlet2的doGet方法:
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object key1 = req.getAttribute("key1");
System.out.println("获取到Servlet1的key:"+key1);
System.out.println("开始处理Servlet2的业务");
}
web.xml配置文件:
Servlet1
com.mafei.www.Servlet1
Servlet1
/servlet1
Servlet2
com.mafei.www.Servlet2
Servlet2
/servlet2
HttpServletResponse类说明:
1、作用
HttpServletResponse类和HttpServletRequest类一样。每次请求进来,tomcat服务器都会创建一个response对象传递给servlet程序去使用。HttpServletRequest表示请求过来的信息,HttpServletResponse表示所有响应的信息,
我们如果需要设置返回给客户端的信息,都可以通过HttpServletResponse对象来进行设置
2、两个输出流的说明:
字节流: getOutputStrem(); 常用于下载(传递二进制数据)
字符流 : getWriter(); 常用于回传字符串(常用)
返回字符串给客户端
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.write("response's content!!!!!");
}
请求重定向
有response1和response2,访问response1会重定向到response2页面
response1:
public class Response1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("在response1中走过了。。。");
// resp.setStatus(302);
// resp.setHeader("Location","/web03/response2");
resp.sendRedirect("/web03/response2"); //第二种方法,更方便
}
}
response2:
public class Response2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("response2中的响应信息");
resp.getWriter().write("response2's content!!!!!1");
}
}
JAVA EE三层架构