文章目录
Servlet基础代码
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; /** * Servlet的实现 * 1、新建包,并新建普通class类 * 2、继承HttpServle 类(实现Servlet规范) * 3、重写HttpServlet 类中的service()方法(service方法在servlet类接受请求,自动调用。作用:响应结果) * 4、在Servlet上添加注解@WebServlet("/"),设置 Servlet访问路径(资源的对外访问路径:浏览器通过访问该路径,从而访问到Servlet类) * * * */ @WebServlet("/ser") public class Servlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("hello"); resp.getWriter().write("hello"); } }
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; /** * Servlet的生命周期 * 1、初始化 * init() * 系统方法,服务器自动调用,只执行一次。 * 当第一次访问Servlet时触发 * 2、就绪/调用 * servlet() * 系统方法,服务器自动调用,可以执行多次。 * 当有请求访问servlet时,servlet就会被调用 * 3、销毁 * destory() * 系统方法,服务器自动调用,只执行一次。 * 当服务器关闭时触发。 * */ @WebServlet("/ser01") public class Servlet01 extends HttpServlet { /** * init() * 系统方法,服务器自动调用,只执行一次。 * 当第一次访问Servlet时触发 * * * @throws ServletException */ @Override public void init() throws ServletException { System.out.println("init...."); } /** * servlet() * 系统方法,服务器自动调用,可以执行多次。 * 当有请求访问servlet时,servlet就会被调用 * @param req * @param resp * @throws ServletException * @throws IOException */ @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("service...."); } /** * destory() * 系统方法,服务器自动调用,只执行一次。 * 当服务器关闭时触发。 */ @Override public void destroy() { System.out.println("destroy...."); } }
获取请求参数
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; @WebServlet("/ser02") public class Servlet02 extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // http://localhost/servlet01_war_exploded/ser02?name=zs&pwd=123&hobby=sing&hobby=dance String queryString = req.getQueryString(); System.out.println("获取请求行中的参数部分"+ queryString); /** * * 获取请求参数 * 1.通过参数名获取参数,返回字符串 * * 参数名: * 1.表单元素的name属性 * 2.Ajax的data属性的值的键 * 3.超链接参数的键 * 2.通过参数名获取所有参数,返回数组 * * */ //获取用户名 String name = req.getParameter("name"); String pwd = req.getParameter("pwd"); System.out.println("姓名:"+name+",密码:"+pwd); //通过参数名获取所有参数,返回数组 String[] arr = req.getParameterValues("hobby"); for (int i = 0; i < arr.length ;i++){ System.out.println(arr[i]); } } }
请求乱码处理
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="ser03" method="post"> 姓名:<input type="text" name="names"> <button>登录</button> </form> </body> </html>
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; /** * 请求乱码处理 * req.setCharacterEncoding("UTF-8"); */ @WebServlet("/ser03") public class Servlet03 extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //在参数获取前,设置编码 req.setCharacterEncoding("UTF-8"); System.out.println(req.getParameter("names")); } }
请求转发
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="ser04" method="post"> 姓名:<input type="text" name="names"> 密码:<input type="password" name="paw"> <button>登录</button> </form> </body> </html>
<%@ page import="java.util.List" %> <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html> <html> <head> <title>JSP - Hello World</title> </head> <body> <%-- 数据共享 Servlet04--%> <% String name = request.getParameter("names"); String paw = request.getParameter("paw"); out.print("名字:"+name+",密码:"+paw); %> <br> <% //获取request作用域 Servlet05 String names = (String) request.getAttribute("username"); //在浏览器中打印 out.print(names); //获取集合 // List<String> list = (List<String>) request.getAttribute("list"); // out.print(list.get(0)); %> <br/> <a href="hello-servlet">Hello Servlet</a> </body> </html>
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; /** * * 请求转发 * 服务端的一种跳转方式 * 格式: * req.getRequestDispatcher("路径").forward(req,resp); * 特点: * 1.地址栏不发生改变 * 2.服务端行为 * 3.请求转发只有一次请求 * 4.request对象可以共享 * */ @WebServlet("/ser04") public class Servlet04 extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); System.out.println("Servlet...."); String name = req.getParameter("names"); String paw = req.getParameter("paw"); System.out.println("名字:"+name+",密码:"+paw); //请求转发跳转到首页 req.getRequestDispatcher("index.jsp").forward(req,resp); } }
request作用域
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="ser05" method="post"> 姓名:<input type="text" name="names"> 密码:<input type="password" name="paw"> <button>登录</button> </form> </body> </html>
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; import java.util.ArrayList; import java.util.List; /** * request作用域 * 设置作用域 * request.setAttribute("参数名",参数值); * 获取作用域 * request.getAttribute("参数名"); * 移除作用域 * request.removeAttribute("参数名"); * * */ @WebServlet("/ser05") public class Servlet05 extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); System.out.println("Servlet05...."); String name = "李祥"; String paw = "asd.123"; List<String> list = new ArrayList<>(); list.add("ada"); list.add("aaa"); //设置到request作用域中 req.setAttribute("username",name); req.setAttribute("password",paw); req.setAttribute("list",list); //请求转发跳转到首页 req.getRequestDispatcher("index.jsp").forward(req,resp); } }