Servlet概述
- Servlet是Server Applet的简称,是服务器端的程序
- 可交互式的处理客户端发送到服务器端的请求,并完成响应操作
- 动态网页技术
- JavaWeb程序开发的基础,JavaEE规范的一个组成部分
作用
- 接收客户端请求,完成操作
- 动态生成网页
- 将包含操作结果的动态网页响应给客户端
开发步骤
- 将Servlet相关jar包配置到classpath中
部署
- 将生成的.class文件放在WEB-INF/classes文件中
配置
- 编写项目配置文件web.xml
HTTP协议
概念
- 超文本传输协议
- 是互联网上应用最广泛的一种网络协议
- 是一个基于请求与相应模式的、无状态的、应用层的协议,运行于TCP协议基础之上
特点
- 支持客户端/服务器模式
- 简单快速
- 灵活
- 无连接
- 无状态
通信流程
- 客户端与服务器端建立连接
- 客户端向服务器端发送请求
- 服务器接受请求,并根据请求返回相应信息
- 客户端与服务器端断开连接
HTTP请求报文
- 当浏览器想Web服务器发出请求的时候,就会想服务器发送一个请求报文
HTTP响应报文
- 当Web服务器收到浏览器的请求后,服务器对浏览器做出的响应,就是响应报文
常见状态码
- 200:客户端请求成功
- 302:临时重定向
- 403:服务器收到请求,但是拒绝提供服务
- 404:请求的资源不存在
- 500:服务器端错误,导致无法完成客户端的请求
Servlet的使用
一、先创建一个JavaWeb项目
File –> new –> Project –>Java Enterprise
二、为此Web项目配置一个Tomcat服务器
在右上角找到配置
点击加号,找到Tomcat
Server中为其修改一个名字即可,然后找到Deployment,在里面将此项目添加进去,这样启动服务器的时候就会访问此项目了
三、创建Servlet
在src目录下创建包结构以及一个Servlet类
然后继承HttpServlet方法,并重写其中的两个方法
一个是doPost方法,一个是doGet方法,在这里只用一个就行,为其编写一个向页面输出一条信息的语句
- get请求,不安全,提交的数据会被拼接到url之后
- post请求,相对get更安全,把提交的数据放在HTTP包的Body中
package com.robot.servlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 张宝旭
*/
public class MyServlet1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.getWriter().write("我的第一个Servlet");
}
}
配置WEB-INF目录下的web.xml文件
- url-pattern:浏览器中访问Servlet的名称
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>MyServlet1</servlet-name>
<servlet-class>com.robot.servlet.MyServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet1</servlet-name>
<url-pattern>/myservlet1</url-pattern>
</servlet-mapping>
</web-app>
配置好后就可以启动服务器了
然后再浏览器中访问http://localhost:8080/MyWebProject8_5_war_exploded/,8080后面的名称是自己配置Tomcat时配置的,会默认生成,启动后会自动弹出页面,因为在web目录下默认有index.jsp页面,所以默认直接访问index.jsp页面,会看到 $ END$ 这个信息
然后访问Servlet,在其url后面追加在web.xml中servlet-mapping标签下的url-pattern中的内容,就可以访问这个Servlet了
因为前面有中文,所以会乱码,后续会解决
使用注解方式配置
Servlet3.0以后支持注解配置,推荐使用
直接在Servlet类上面添加一条注解,name可以不写,value就是浏览器中访问的路径
@WebServlet(name = "MyServlet1", value = "/myservlet1")
完整代码
package com.robot.servlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 张宝旭
*/
@WebServlet(name = "MyServlet1", value = "/myservlet1")
public class MyServlet1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.getWriter().write("我的第一个Servlet");
}
}
使用注解和使用web.xml的方式可以同时使用,但是它们两个的作用是一样的
假如表单中有复选框,那么获取的参数name都是一样的,改如何获取所有的值呢?
当然是使用数组的形式,在注解中添加一条内容
@WebServlet(name = "MyServlet1", value = "/myservlet1", initParams = {@WebInitParam(name = "", value = "")})
解决乱码
客户端以浏览器设置的编码将表单数据传输到服务器端,服务器daunt默认是ISO-8859-1编码接收
所以我们需要设置一个统一的编码,为请求和响应都设置utf-8编码
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
request
在Servlet中用来处理客户端请求需要用request对象,包含了客户端请求的所有内容
常用方法
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// 设置编码
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
// 获取参数
ServletConfig servletConfig = getServletConfig();
String initParameter = servletConfig.getInitParameter("username");
System.out.println(initParameter);
// 获取请求头
String accept = req.getHeader("Accept");
System.out.println("请求头: " + accept);
// 获取请求方式
String method = req.getMethod();
System.out.println("请求方式: " + method);
// 获取URL
StringBuffer requestURL = req.getRequestURL();
System.out.println("URL: " + requestURL);
// 获取IP地址
String remoteAddr = req.getRemoteAddr();
System.out.println("IP地址: " + remoteAddr);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
respone
用于响应客户端请求并向客户端输出信息,包含了响应目标的所有内容
常用方法
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
// 设置编码
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
// 服务器响应信息
// 设置响应类型
resp.setContentType("text/html;charset=utf-8");
// 创建字符流
PrintWriter writer = resp.getWriter();
// 输出到页面中
writer.write("<h1>响应</h1>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
注意:在有服务器响应的地方才可以设置响应类型,否则不要设置,容易导致其它地方输出乱码
转发与重定向
在实际业务中,请求和响应肯定需要在不同的Servlet中处理,来实现业务逻辑与显示结果分离,所以就需要转发与重定向
转发
转发的作用在服务器端,将请求发送给服务器上的其它资源,然后共同完成一次请求处理
在转发的时候,url不会改变,所以只是一次请求,不会有第二次请求
比如:浏览器向A发送一个请求,但是A能力有限,不能完成,所以A就将这个请求转发给了B,然后B完成后又将结果返回给了A,由A将结果再返回给浏览器,此过程只有一次请求,浏览器不知道B的存在,只向A发送了一个请求,所以url也不会变
使用
request.getRequestDispatcher("success.html").forward(request, response);
作用域
拥有存储数据的空间,作用范围是一次请求有效
存储数据
request.setAttribute("name", "zbx");
获取数据
request.getAttribute("name");
转发的特点
- 转发是服务器的行为
- 转发是浏览器只进行一次请求
- 转发浏览器地址不变
- 转发两次跳转之间传输的信息不会丢失,所以可以通过request进行数据的传递
- 转发只能将请求转发给同一个Web应用中的组件
重定向
重定向作用在客户端,客户请求服务器,服务器响应给客户端一个新的请求地址,客户端重新发送新的请求
使用
response.sendRedirect("failure.html");
重定向的特点
- 重定向是客户端行为
- 浏览器至少进行两次访问请求
- 浏览器地址改变
- 两次跳转之间传输的数据会丢失
- 可以指向任何的资源
总结:当需要数据传递的时候,选择forward转发
实例
在web目录下创建一个登陆界面,form表单填写账号和密码,然后提交
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<form action="login" method="post">
<table>
<tr>
<td>账号</td>
<td><input type="text" name="username"> </td>
</tr>
<tr>
<td>密码</td>
<td><input type="text" name="password"> </td>
</tr>
<tr aria-colspan="2">
<td><button type="submit" name="submit">提交</button> </td>
</tr>
</table>
</form>
</body>
</html>
再创建一个成功页面,用于登陆成功跳转的页面
success.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录成功</title>
</head>
<body>
<h1>登录成功</h1>
</body>
</html>
再创建一个失败的页面,用于登陆失败跳转的页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录失败</title>
</head>
<body>
<h1>登录失败</h1>
</body>
</html>
创建Servlet,实现登陆成功就转发到成功页面,登陆失败就重定向到失败页面
LoginServlet.java
package com.robot.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author 张宝旭
*/
@WebServlet(name = "LoginServlet", value = "/login")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
// 获取用户名和密码
String username = request.getParameter("username");
String password = request.getParameter("password");
// 先固定设置好正确的账号和密码,然后进行匹配
if (username.equals("admin") && password.equals("123456")) {
// 转发
request.getRequestDispatcher("success.html").forward(request, response);
} else {
// 重定向
response.sendRedirect("failure.html");
}
}
}
启动服务器,在浏览器中访问http://localhost:8080/MyWebProject8_5_war_exploded/login.html,然后输入不同账号和密码进行测试
当登陆成功时,结果如下,转发到成功页面,而后面的地址也没有改变,登陆时访问的就是Servlet,所以地址还是Servlet配置的地址login,没有改变
当登陆失败时,结果如下,重定向到失败页面,而后面的地址发生了改变,变成了失败页面的地址failure.html
Cookie
存储在客户端的数据,当web服务器在HTTP响应头中附带传送给浏览器的一小段数据,当浏览器保存了Cookie,之后每次访问该服务器时,会通过请求头回传该Cookie数据,Cookie主要由name和value组成
创建Cookie
Cookie cookie = new Cookie("username", "robot");
设置Cookie的路径
cookie.setPath(getServletContext().getContextPath());
设置Cookie的生命周期
- 当>0时有效期为多少秒;当=0时有效期为浏览器关闭;默认-1为内存存储
cookie.setMaxAge(60 * 60 * 24);
响应给客户端
response.addCookie(cookie);
修改Cookie
- 只要保证Cookie的名称和路径一致既可修改
Cookie cookie = new Cookie("username", "robot");
cookie.setPath(getServletContext().getContextPath());
cookie.setMaxAge(60 * 60);
获取Cookie
Cookie[] cookies = request.getCookies();
Session
用于记录用户的状态,存储在服务器端
获取Session
HttpSession session = request.getSession();
保存数据
- 以键值对的形式存储
session.setAttribute("validateCode", "1314");
获取数据
Object validate2 = session.getAttribute("validateCode");
移除数据
session.removeAttribute("user");
清除Session
session.invalidate();
Session的生命周期
- 在浏览器打开的过程中都有效,一旦浏览器关闭,则Session失效,或者Session超时,则失效,或者手动销毁,则失效
浏览器发生请求,不会创建Session,当浏览器端调用getSession()时,Session在服务器端被创建,然后将Sessionid存储到一个Cookie里,返回给浏览器,所以浏览器关闭,Session不会销毁,而是存储Sessionid的那个Cookie随浏览器关闭而销毁了,没有了Sessionid,也就找不到Session了,所以服务器端的Session处于遗忘状态,等待生命周期结束,自动销毁。
ServletContext
全局对象,当Web服务器启动的时候,会为每个Web应用程序创建一块共享的存储区域,在服务器启动时创建,服务器关闭时销毁
获取ServletContext的三种方法
ServletContext servletContext1 = getServletContext();
ServletContext servletContext2 = request.getServletContext();
ServletContext servletContext3 = request.getSession().getServletContext();
获取项目真实路径
- 打印结果为:C:\WebProject\MyWebProject8.5\out\artifacts\MyWebProject8_5_war_exploded\img\robot.jpg
String realPath = servletContext.getRealPath("/img/robot.jpg");
System.out.println(realPath);
获取应该上下文路径
- 打印结果为:/MyWebProject8_5_war_exploded(Tomcat配置的访问路径)
String contextPath = servletContext.getContextPath();
System.out.println(contextPath);
存储数据
- 比如我们可以用它来统计访问网站的次数
servletContext.setAttribute("count", 1);
获取数据
servletContext.getAttribute("count");
移除数据
servletContext.removeAttribute("count");
Project\MyWebProject8.5\out\artifacts\MyWebProject8_5_war_exploded\img\robot.jpg
String realPath = servletContext.getRealPath("/img/robot.jpg");
System.out.println(realPath);
获取应该上下文路径
- 打印结果为:/MyWebProject8_5_war_exploded(Tomcat配置的访问路径)
String contextPath = servletContext.getContextPath();
System.out.println(contextPath);
存储数据
- 比如我们可以用它来统计访问网站的次数
servletContext.setAttribute("count", 1);
获取数据
servletContext.getAttribute("count");
移除数据
servletContext.removeAttribute("count");