位置: IT常识 - 正文
推荐整理分享2022-8-30 servlet,希望有所帮助,仅作参考,欢迎阅读内容。
文章相关热门搜索词:,内容如对您有帮助,希望把文章链接给更多的朋友!
System.out.println("请求的方法:" + request.getMethod());System.out.println("请求的根路径:" + request.getContextPath());System.out.println("请求的URI:" + request.getRequestURI());System.out.println("请求的URL:" + request.getRequestURL());System.out.println("请求的servletPath:" + request.getServletPath());
@WebServlet("/demo.do")public class Demo01Servlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 根据key获取对应的value request.getParameter("");// 获取一个key对应的多个值,返回值为String类型的数组 例如:复选框 String[] hobbies = request.getParameterValues("hobby");// System.out.println(Arrays.toString(hobbies)); // 获取请求中传过来的参数的key值,根据key值获取对应的value值 Enumeration<String> parameterNames = request.getParameterNames(); while(parameterNames.hasMoreElements()){ System.out.println(parameterNames.nextElement()); } // 可以获取到请求中的所有的键值对,返回值是一个map集合 // 这个map集合的key是String,value是String[] // username=admin,password=123456,hobby=a,b,c,gender=man Map<String, String[]> map = request.getParameterMap(); for (Map.Entry<String, String[]> name : map.entrySet()) { String key = name.getKey(); String[] value = name.getValue(); System.out.println(key + "--->" + Arrays.toString(value)); }}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response);}}
转发 request.getRequestDispatcher("demo03.do").forward(request,response);@WebServlet("/demo02.do")public class Demo02Servlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/** 请求转发---由多个servlet来处理同一个请求** 请求转发是在服务器上完成的,跟客户端是否无关!!!***/String name = request.getParameter("name");System.out.println("Demo02Servlet,name:" + name);// getRequestDispatcher参数:转发的servlet的地址// forward参数:request,response,把当前servlet用的request和response传给下一个request.getRequestDispatcher("demo03.do").forward(request,response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}@WebServlet("/demo04.do")public class Demo04Servlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /* * 作用域:起作用的区域 * * 往request作用域中添加了一个属性name,值是zhangsan * * 请求转发的时候是同一个request * */ request.setAttribute("name","zhangsan"); // name怎么取出来? System.out.println("demo04.do---name:" + request.getAttribute("name")); request.getRequestDispatcher("demo05.do").forward(request,response);// 根据指定的key,删除对应的value// request.removeAttribute("name");// 获取request作用域中的所有的key// Enumeration<String> attributeNames = request.getAttributeNames();}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}} request.getRequestDispatcher("demo03.do").forward(request,response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response);}}重定向 response.sendRedirect("success.html");@WebServlet("/demo06.do")public class Demo06Servlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter() response.setCharacterEncoding("utf-8"); response.addHeader("content-type","text/html;charset=utf-8"); response.sendRedirect("success.html");}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}}请求转发和重定向的区别: 1.重定向是两次请求,请求转发是一个请求 2.重定向是客户端行为,请求转发是服务器行为 3.重定向浏览器地址会发生改变,转发不会最常见的客户端传参的方式两种,get,post * 浏览器地址栏直接输入:一定是get请求 * 超级链接:一定是get请求 * 表单:可以是get,也可以是post,取决于method * * GET请求和POST请求的区别: * GET:请求的参数会在浏览器的地址栏中显示,不安全;请求的长度限制在1K之内。 * 没有请求体,无法通过request去设置一些参数。 * POST:请求的参数不会在浏览器的地址栏中显示,相对安全。 * 把请求的参数封装成几个数据报文(请求体)。请求参数的长度没有限制。上一篇:前端框架小实验-在umi框架中以worker线程方式使用SQL.js的wasm(简单易学的前端框架)
友情链接: 武汉网站建设