1.创建一个servlet
继承HttpServlet类重写doget,dopost方法
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("hello ");
}
}
在web.xml中配置
2. servlet的生命周期
index.html
HelloServlet
com.servlet.HelloServlet
HelloServlet
/hello
实例化—>初始化init—>服务service—>销毁destroy—>不可用
什么时候创建servlet :当第一个用户第一次访问servlet时由tomcat容器创建servlet对象(单例模式)
Servlet生命生命周期中的三个方法:
init()servlet实例化后自动调用,起到构造方法参数初始化的作用
service()分析客户端的访问方式get/post/delete/put…
destroy();销毁实例
3. 在servlet中获取初始化参数
从web.xml中读取
1.获取局部参数
xml文件中
(写在servlet标签里面)
charset数据名
UTF-8值
java程序中
public class HelloServlet extends HttpServlet {
private String charset;
@Override
public void init(ServletConfig config) throws ServletException {
String tem=config.getInitParameter("charset");
this.charset=(tem==null)?"GBK":tem;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println(charset);
}
}
重写init(config)方法
String tem= config.getInitParameter(“charset”);数据名获取(如果没有这项数据会出现异常)
charset=(tem!=null)?tem:charset;判断取到的值是否为空
2.全局servlet数据
xml文件中
(写在servlet标签外面)
charset数据名
gbk值
java程序中
重写init(config)方法
ServletContext con=(ServletContext)config.getServletContext();先拿到ServletContext()对象
String tem=con.getInitParameter(“charset”);通过servletcontext对象拿到数据
charset=(tem!=null)?tem:charset;
另一种初始化数据的方法
专门写一个servlet
只有init()方法
xml文件中
写单个servlet数据
删掉标签
(服务器一启动就创建sevlet对象并调用init方法)
优先级1-10 (优先级越高越早创建,1最高,<0表示该servlet被选择时才加载)
在servlet中如果重写了两个init方法一个有参一个无参,在服务器创建servlet对象时只会调用有参的
4.在servlet中获取请求参数
从客户端获取数据
通过HttpServletRequst 的对象request
request.getParmeter(“userName”)填表单元素的name返回字符串
request.getParmeterValues(“check”)填多选框的name返回字符串数组
request.getParmeterNames()获取所有的表单name
5.解决请求参数中文乱码问题
调用request对象获取数据之前(只对post方法提交有效)
request.setCharacterEncoding(“UTF-8”);
地址栏提交汉字处理(get方法提交处理)
String name=request.getParameter(“userName”);
String userName=new String(name.getBytes(“ISO8859-1”),“UTF-8”);
把name以ios8859-1编码打散成字节数组,再以UTF-8格式重新生成字符串
6.向客户端传递数据
向客户端传递数据
通过HttpServletResponse 的对象response
通过response.getWriter()方法得到PrintWriter对象out
调用out.print("")方法
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter out= resp.getWriter();
out.write(" test 世界,你好
");
out.flush();
out.close();
}
7.解决传递数据中文乱码问题
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out= resp.getWriter();
out.write(" test 世界,你好
");
out.flush();
out.close();
}
8.servlet之间的通信
servlet之间的通信有三种方式,也是servlet与jsp的通信方式
(1)response.sendRedirect(“B”);跳转到B页面,没有请求关系
(2)request.getRequestDispatcher(“B”).forward(request, response);跳转到Bservlet有请求关系
(3)request.getRequestDispatcher(“B”).include(request, response);将Bservlet包含到Aservlet,有请求关系
9.解决表单重复提交问题(令牌机制)
以jsp页面为例
原理是在进入一个表单页面之前,给用户分配一个随机令牌,session中也保存一份,在页面提交时,会将用户的令牌与session中的进行对比,对比之后销毁session中的令牌
令牌工具类
public abstract class TokenUtil {
public static void getToken(HttpServletRequest request){
UUID uuid= UUID.randomUUID();
String token= uuid.toString();
request.setAttribute("token",token);
HttpSession session=request.getSession();
session.setAttribute("token",token);
}
public static boolean checkToken(HttpServletRequest request){
String reqToken =request.getParameter("token");
if (reqToken==null||reqToken.equals("")){
return false;
}
Object tokenObj= request.getSession().getAttribute("token");
if(tokenObj==null){
return false;
}
request.getSession().removeAttribute("token");
String token= (String) tokenObj;
if(reqToken.equals(token)){
return true;
}else {
return false;
}
}
}
负责页面跳转的servlet
public class IndexServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
TokenUtil.getToken(req);
req.getRequestDispatcher("/index.jsp").forward(req,resp);
}
}
负责接收数据的servlet
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
boolean bol= TokenUtil.checkToken(req);
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out= resp.getWriter();
if (!bol){
out.write("请勿重复提交");
}else {
out.write("提交成功");
}
out.flush();
out.close();
}
}
jsp表单页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title