JavaWeb的准备工作
一、IDEA软件的安装和tomact的安装下载
安装
IDEA直接next(x64) + 激活码
注意:
1. 安装目录可以随意指定~
2. 之前安装了jdk和进行了path的配置,最好为1.8版本~
tomcat 在项目中导入路径
开始学习
Servlet
(1)sevlet的创建和使用
(2)servlet的服务器设置和webServlet注释
(3)Servlet的工作流
(4)Servlet的实现方式
(5)Servlet的生命周期
获取参数和路径
@WebServlet("/ser1")
public class Servlet2 extends HttpServlet {
@Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("启动Servlet...."); /** * 获取 */ String url = request.getRequestURI() + ""; System.out.println("完整路径: " + url); String uri = request.getRequestURI(); System.out.println("部分路径: " + uri); String queryString = request.getQueryString(); System.out.println("全部参数:" + queryString); String method = request.getMethod(); System.out.println("请求方式:" + method); String prototol = request.getProtocol(); System.out.println("协议版本:" + prototol); String webapp = request.getContextPath(); System.out.println("站点名:" + webapp); /** * 获取指定参数 * 重点! * 案例链接: * http://localhost:8080/annran/ser1?uname=admin&upwd=123456&hobbys=pingpon&hobbys=java * 预计输出: * uname: admin, upwd: 123456 * 爱好:pingpon * 爱好:java */ String uname = request.getParameter("uname"); String upwd = request.getParameter("upwd"); System.out.println("uname: " + uname + ", upwd: " + upwd); String[] hobbys = request.getParameterValues("hobbys"); //注意:判断是否为空! if (hobbys != null && hobbys.length > 0) { //foreach输出 for (String hobby: hobbys) { System.out.println("爱好:" + hobby); } } } @Override public void destroy() { System.out.println("销毁Servlet...."); } @Override public void init() throws ServletException { System.out.println("初始化Servlet...."); } }
请求乱码的问题
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>登录</title> </head> <body> <form method="post" action="/annran/ser2"> 姓名:<input type="text" name="name"><br> 密码:<input type="password" name="pwd"><br> <button>登录</button> </form> </body> </html>
@WebServlet("/ser2") public class Servlet1 extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * 请求乱码的问题 * tomcat8以上的版本 预计输出 * get请求 不会乱码 姓名:张三,密码:张三 * 即便乱码,使用 new String(request.getParameter(name).getBytes("ISO-8859-1"),"UTF-8"); * post请求-表单 会乱码,设置服务器解析编码的格式 * post请求无论是什么版本的服务器都会乱码 */ //设置请求的编码格式 request.setCharacterEncoding("UTF-8"); String name = request.getParameter("name"); String pwd = request.getParameter("pwd"); System.out.println("姓名:" + name + ",密码:" + pwd); } }
请求转发(后台到前台-服务端到客户端)
/** * 实现请求转发 * 可以请求从服务端跳转到客户端: * request.getRequestDispatcher("/ser4").forward(request,response); * 特点: * 1、服务端行为 * 2、地址栏不变 * 3、从始至终只有一个请求 * 4、数据可以共享 */ @WebServlet("/ser3") public class Servlet3 extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //接受客户端参数 String uname = request.getParameter("uname"); System.out.println("Servlet3 uname: " + uname); //请求跳转到Servlet4 //request.getRequestDispatcher("/ser4").forward(request,response); //请求转发到一个jsp页面 //request.getRequestDispatcher("login.jsp").forward(request,response); //请求转发到一个html页面 request.getRequestDispatcher("login.html").forward(request,response); } }
/** * 实现请求转发 * 可以请求从服务端跳转到客户端 * 服务端行为 */ @WebServlet("/ser4") public class Servlet4 extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //接受客户端参数 String uname = request.getParameter("uname"); System.out.println("Servlet4 uname: " + uname); } }
request作用域
/** * request作用域 * 在一个请求中传输数据,只在跳转的时候有效 */ @WebServlet("/ser5") public class Servlet5 extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("Servlet5"); //设置域对象 name object request.setAttribute("name","zhangsan"); request.setAttribute("age","18"); List<String> list = new ArrayList<>(); list.add("aaa"); list.add("bbb"); request.setAttribute("list",list); //请求转发 request.getRequestDispatcher("/ser6").forward(request,response);
//这里如果直接跳转到jsp页面,获取参数的方法也是一样的,只是在jsp中写入java代码,使用<%java代码%> } }
/** * request作用域 * 在一个请求中传输数据,只在跳转的时候有效 */ @WebServlet("/ser6") public class Servlet6 extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("Servlet6"); //获取阈值 String name = (String) request.getAttribute("name"); System.out.println("name: " + name); String age = (String) request.getAttribute("age"); System.out.println("age: " + age); List<String> list = (List<String>) request.getAttribute("list"); System.out.println(list.get(0)); } }
响应数据的两种方式:
(1)字符流
(2)字节流
//字符输出流 PrintWriter writer = response.getWriter(); writer.write("hello"); writer.write("<p>hello</p>"); //字节输出流 ServletOutputStream out = response.getOutputStream(); out.write("hello".getBytes()); out.write("<p>hello</p>".getBytes());
响应乱码的问题:
//第一种 //告知服务端 response.setCharacterEncoding("UTF-8"); //告知客户端 response.setHeader("content-type","text/html;charset=UTF-8"); //第二种 //常用,同时设置服务端和客户端 response.setContentType("text/html;charset=UTF-8");
重定向(客户端行为):
@WebServlet("/ser01") public class Servlet01 extends HttpServlet { /** * 重定向: * 1、存在两次请求 * 2、地址栏会发生改变 * 3、数据无法共享 */ @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("Servlet01..."); //接受参数 String uname = request.getParameter("uname"); System.out.println("Servlte01 uname: " + uname); //重定向 response.sendRedirect("ser02"); } }
@WebServlet("/ser02") public class Servlet02 extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("Servlet02..."); //接受参数 String uname = request.getParameter("uname"); System.out.println("Servlte02 uname: " + uname); } }