Servlet
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
继承servlet后 重写的方法后关注的生命周期,一般继承Httpservlet主要写service方法。
1.servlet继承体系
2.ServletConfig类 3个作用
在init方法中使用:
获取web.xml中的对应的参数
注意:如下:只在单独的helloServlet2 中可以获取,因为web.xml中就写在了单独的某个Servlet。
也可以在其他方法中使用
3.ServletContext类4个作用
ServletContext只能获取context-prarm, 上一节提到的init-param是ServletConfig才能获取的。
idea发布的 对应工程的web目录 就是把源码 web目录下的内容+字节码部署到这个路径下了
这个路径是编译完后的字节文件路径,是下面out文件的输出路径
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = getServletContext();
servletContext.setAttribute("key1","value1");
}
context对象的作用就是 每一个Servlet是共用的,相互之间都可以拿到配置的信息。
4.HttpServletRequest
每次请求进来就会创建一个
注意:URL = 协议 + 主机 + 端口号 + URI
@WebServlet(name = "RequestAPIServlet", value = "/RequestAPIServlet")
public class RequestAPIServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(request.getRequestURI());
System.out.println(request.getRequestURL());
System.out.println("客户端IP地址:"+request.getRemoteHost());
String requestHeader = request.getHeader("User-Agent");
System.out.println(requestHeader);
System.out.println(request.getMethod());
///servlet_demo01/RequestAPIServlet
//http://localhost:8080/servlet_demo01/RequestAPIServlet
//客户端IP地址:127.0.0.1
//Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0
//GET
}
参数处理:
乱码问题:
注意:设置字符集必须要放在最前面。先获取参数,后设置,无效。
5.请求转发
-
浏览器地址没有变
-
他们是一次请求
-
他们共享request域中的数据
-
可以转发web-inf目录下(web-inf目录是不可以浏览器地址直接访问的)
-
不可访问工程以外的地址
public class Servlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取请求的参数(办事的材料)查看
String username = req.getParameter("username");
System.out.println("在Servlet1(柜台1)中查看参数(材料):" + username);
// 给材料 盖一个章,并传递到Servlet2(柜台 2)去查看
req.setAttribute("key1","柜台1的章");
// 问路:Servlet2(柜台 2)怎么走
/**
* 请求转发必须要以斜杠打头,/ 斜杠表示地址为:http://ip:port/工程名/ , 映射到IDEA代码的web目录<br/>
*
*/
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/servlet2");
// RequestDispatcher requestDispatcher = req.getRequestDispatcher("http://www.baidu.com");
// 走向Sevlet2(柜台 2)
requestDispatcher.forward(req,resp);
}
}
请求转发必须要以斜杠打头,/ 斜杠表示地址为:http://ip:port/工程名/ , 映射到IDEA代码的web目录
请求转发必须要以斜杠打头,/ 斜杠表示地址为:http://ip:port/工程名/ , 映射到IDEA代码的web目录
请求转发必须要以斜杠打头,/ 斜杠表示地址为:http://ip:port/工程名/ , 映射到IDEA代码的web目录
public class Servlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取请求的参数(办事的材料)查看
String username = req.getParameter("username");
System.out.println("在Servlet2(柜台2)中查看参数(材料):" + username);
// 查看 柜台1 是否有盖章
Object key1 = req.getAttribute("key1");
System.out.println("柜台1是否有章:" + key1);
// 处理自己的业务
System.out.println("Servlet2 处理自己的业务 ");
}
}
6.base标签的作用
解决的问题就是
正常使用的a标签跳转,跳转后地址栏改变,然后点击回到上一页面,正常运行。
如果是请求转发过来的,点击回到上一页,因为地址栏没有改动到当前页面,所以会发生错误。
页面有base标签时,点击回到上一页,先参考设置的地址,作为当前地址,而不是地址栏的地址。
7.Web中 / 的意义
8.HttpServletResponse
不能同时使用,控制台会报错。
解决乱码:
设置响应头的内容都需要在获取流对象之前才可以
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// System.out.println(response.getCharacterEncoding());//ISO-8859-1
response.setCharacterEncoding("utf-8");
//浏览器使用的GBK
//让浏览器也使用utf-8
response.setHeader("Content-Type","text/html;charset = utf-8");
//resp.setContentType("text/html;charset=utf-8") 用这一句也能解决 他相当于上面两句
PrintWriter writer = response.getWriter();
writer.write("response");
writer.write("数据");
}
9.请求重定向
这里的req.setAttribute和getServletContext().setAttribute 不一样,重定向 Context()的Attribute是可以获取到的
推荐使用的方法: