目錄
- Servlet
- Servlet的三大職責
- 跳轉:請求轉發和重定向
- 請求轉發
- 重定向
- 匯總
- 請求轉發與重定向的區別
- 用請求轉發和重定向完善登錄
- JSP
- 第一個JSP
- 概述
- 注釋
- 設置創建JSP文件默認字符編碼集
- JSP的java代碼書寫
- JSP的原理
- 三大指令
- 九大內置對象
- 改造動態web工程進行示例
- 內置對象名稱來源?
- 名單列表
- 四大作用域
- 概述
- 案例測試
- 登錄完善
Servlet
Servlet的三大職責
1.接受參數 --> req.getParameter (非必須)
2.處理業務 --> 拿到數據后去做一些事情(非必須)
3.跳轉(必須)–> 操作完的一個結果 兩句代碼
跳轉:請求轉發和重定向
請求轉發
案例演示:動態web項目
AServlet
@WebServlet("/go/a")
public class AServlet extends HttpServlet{@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("AServlet");String name = req.getParameter("name");System.out.println("A-name: "+name);req.setAttribute("password", "123456");// 請求轉發req.getRequestDispatcher("/go/b").forward(req, resp);}
}
BServlet
@WebServlet("/go/b")
public class BServlet extends HttpServlet{@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("BServlet");String name = req.getParameter("name");System.out.println("B-name: "+name);String password = (String) req.getAttribute("password");System.out.println("B-password: "+password);}
}
瀏覽器訪問:http://localhost/go/a?name=zhangsan
控制臺:
AServlet
A-name: zhangsan
BServlet
B-name: zhangsan
B-password: 123456
req.getRequestDispatcher(“路徑”).forward(request, response); ,請求里的東西,forward可以理解為攜帶
帶值跳轉,可以訪問WEB-INF中資源,地址欄不改變
發送一次請求,最后一個response起作用,不可以跨域[跨網站]訪問
重定向
案例演示:動態web項目
CServlet
@WebServlet("/go/c")
public class CServlet extends HttpServlet{@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("CServlet");String name = req.getParameter("name");System.out.println("C-name: "+name);resp.sendRedirect("/go/d");}
}
DServlet
@WebServlet("/go/d")
public class DServlet extends HttpServlet{@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("DServlet");String name = req.getParameter("name");System.out.println("D-name: "+name);}
}
瀏覽器訪問:http://localhost/go/c?name=zhangsan
控制臺:
CServlet
C-name: zhangsan
DServlet
D-name: null
resp.sendRedirect(“路徑”) ,響應里的東西,可以有避免重復扣款和訪問外部網站之類的作用
無法帶值,不能訪問WEB-INF下內容,地址欄改變
兩次請求,起作用的依然是最后一個,可以跨域訪問
匯總
請求轉發與重定向的區別
請求轉發的特點:可以攜帶參數,只用一次請求,可以訪問WEB-INF
重定向的特點:可以避免重復扣款場景風險,可以訪問外部網站,不能訪問WEB-INF
請求轉發過程:瀏覽器 - 內部代碼 - WEB-INF
重定向過程:瀏覽器 - 內部代碼 - 瀏覽器 - URL
動態web項目示例:
EServlet
@WebServlet("/go/e")
public class EServlet extends HttpServlet{@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("EServlet");System.out.println("E----扣款1000");// req.getRequestDispatcher("/go/f").forward(req, resp);
// resp.sendRedirect("/go/f");
// resp.sendRedirect("https://www.fu365.com/");// req.getRequestDispatcher("/WEB-INF/haha.html").forward(req, resp);
// resp.sendRedirect("/WEB-INF/haha.html");}
}
FServlet
@WebServlet("/go/f")
public class FServlet extends HttpServlet{@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {System.out.println("FServlet");}
}
WEB-INF下新建haha.html
瀏覽器訪問:http://localhost/go/e
用請求轉發和重定向完善登錄
webapp下WEB-INF外新建login.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body><form action="/loginTest" method="post">賬號:<input type="text" name="name"><br>密碼:<input type="password" name="password"><input type="submit" value="post"></form>
</body>
</html>
loginTest
@WebServlet("/loginTest")
public class LoginServletTest extends HttpServlet{@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("UTF-8");resp.setContentType("text/html;charset=utf-8");String name = req.getParameter("name");String password = req.getParameter("password");if ( name.equals("zhangsan") && password.equals("123456") ) {resp.sendRedirect("main.html"); //這里在WEB-INF外重定向可以訪問} else {req.setAttribute("msg", "登錄失敗");// 需要訪問另外一個servlet,把參數傳進頁面打印出來req.getRequestDispatcher("/AAAServlet").forward(req, resp);}}
}
main.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body><h1>登錄成功</h1>
</body>
</html>
AAAServlet
@WebServlet("/AAAServlet")
public class AAAServlet extends HttpServlet{@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Object attribute = req.getAttribute("msg");System.out.println(attribute);PrintWriter writer = resp.getWriter();writer.print("<!DOCTYPE html>");writer.print("<html>");writer.print("<head>");writer.print("<meta charset=\"UTF-8\">");writer.print("<title>Insert title here</title>");writer.print("</head>");writer.print("<body>");writer.print(attribute);writer.print(" <form action=\"/loginTest\" method=\"post\">");writer.print(" 賬號:<input type=\"text\" name=\"username\"><br>");writer.print(" 密碼:<input type=\"password\" name=\"password\"><br>");writer.print(" <input type=\"submit\" value=\"post\">");writer.print(" </form>");writer.print("</body>");writer.print("</html>");}
}
JSP
第一個JSP
概述
servlet:是用來寫java代碼的,也可以用來做頁面展示(把html代碼一行一行打印出去),但是不擅長做頁面展示。
html:用來做頁面展示,靜態網頁,沒辦法拿到Java代碼,不能展示數據。
jsp:看起來像html,但是它里面可以寫java代碼(動態網頁)。
注釋
webapp下新建_01hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><!-- 不安全的注釋,能在控制臺看到 --><%-- 安全的注釋,不能再控制臺看到 --%><h1>我是第一個JSP</h1>
</body>
</html>
設置創建JSP文件默認字符編碼集
JSP文件內右鍵 - Preferences - utf-8
JSP的java代碼書寫
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head><body><!-- jsp寫java代碼的第一種方式,打印到后端控制臺 --><%for(int i = 0;i<5;i++){System.out.println("i: "+i);}int b =520;%><!-- jsp寫java代碼的第二種方式,顯示在頁面上 --><%=b %><!-- jsp寫java代碼的第三種方式,涉及JSP的底層原理 --><%!String ss ="abc";// System.out.println("abc: "+ss);%></body>
</html>
JSP的原理
jsp需要tomcat運行才能正常展示內容
訪問JSP - tomcat的web.xml - 兩個servlet類(把JSP轉化為servlet/java文件,把html代碼打印出去)
tips:tomcat的web.xml是全局的,項目中的web.xml是局部的
三大指令
1.page :當前頁面的一些配置,jsp生成java文件時會引用這些配置
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
2.taglib:不講 (下一節來說 )
3.include:引用一個文件,常用于導航欄
_03include.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@include file="head.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><div>螺旋丸</div>
</body>
</html>
_04include.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><div>千年殺</div><%@include file="head.jsp" %>
</body>
</html>
head.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><div style="background-color:red;text-align:center;font-size:50px">火影忍者</div>
九大內置對象
改造動態web工程進行示例
改造LoginServletTest,登錄失敗后跳轉到login.jsp
@WebServlet("/loginTest")
public class LoginServletTest extends HttpServlet{@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {req.setCharacterEncoding("UTF-8");resp.setContentType("text/html;charset=utf-8");String name = req.getParameter("name");String password = req.getParameter("password");if ( name.equals("zhangsan") && password.equals("123456") ) {resp.sendRedirect("main.html"); //這里在WEB-INF外重定向可以訪問} else {req.setAttribute("msg", "登錄失敗");// 需要訪問另外一個servlet,把參數傳進頁面打印出來
// req.getRequestDispatcher("/AAAServlet").forward(req, resp);req.getRequestDispatcher("login.jsp").forward(req, resp);}}
}
webapp下新增login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><%=request.getAttribute("msg") %><form action="/loginTest" method="post">賬號:<input type="text" name="name"><br>密碼:<input type="password" name="password"><input type="submit" value="post"></form>
</body>
</html>
內置對象名稱來源?
來自tomcat根據jsp生成的java文件,在那里面定義了
名單列表
HttpServletRequest request 請求對象
HttpServletResponse response 響應對象
ServletConfig config 配置對象
ServletContext application
Throwable exception 異常( 你當前頁面是錯誤頁時才有 isErrorPage="true" )
JspWriter out 輸出流對象
Object page 相當于this 是當前頁的意思
PageContext pageContext 沒好大用處
HttpSession session 會話對象(重要)
四大作用域
概述
HttpServletRequest request 一次請求
HttpSession session 一次會話 同一個瀏覽器訪問tomcat就是一次會話
PageContext pageContext 當前頁面 作用不大
ServletContext application 整個會話tomcat沒有關閉就不會消失,在不同的瀏覽器都能拿到
案例測試
webapp下新增_05page.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><%pageContext.setAttribute("iampageContext","我是當前頁對象");request.setAttribute("iamrequest", "我是請求對象");session.setAttribute("iamsession", "我是會話對象");application.setAttribute("iamapplication", "我是應用對象");%><%=pageContext.getAttribute("iampageContext")%><%=request.getAttribute("iamrequest")%><%=session.getAttribute("iamsession")%> <%=application.getAttribute("iamapplication")%>
</body>
</html>
webapp下新增_06page.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><%=pageContext.getAttribute("iampageContext")%><%=request.getAttribute("iamrequest")%><%=session.getAttribute("iamsession")%> <%=application.getAttribute("iamapplication")%>
</body>
</html>
啟動tomcat,瀏覽器訪問http://localhost/_05page.jsp,我是當前頁對象 我是請求對象 我是會話對象 我是應用對象
瀏覽器訪問http://localhost/_06page.jsp,null null 我是會話對象 我是應用對象
換一個瀏覽器訪問http://localhost/_06page.jsp,null null null 我是應用對象
重啟原瀏覽器訪問http://localhost/_06page.jsp,null null null 我是應用對象
重啟tomcat訪問http://localhost/_06page.jsp,null null null null
修改_05page.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><%pageContext.setAttribute("iampageContext","我是當前頁對象");request.setAttribute("iamrequest", "我是請求對象");session.setAttribute("iamsession", "我是會話對象");application.setAttribute("iamapplication", "我是應用對象");%><%request.getRequestDispatcher("_06page.jsp").forward(request, response);%>
</body>
</html>
啟動tomcat,瀏覽器訪問http://localhost/_05page.jsp,null 我是請求對象 我是會話對象 我是應用對象
修改_05page.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><%pageContext.setAttribute("iampageContext","我是當前頁對象");request.setAttribute("iamrequest", "我是請求對象");session.setAttribute("iamsession", "我是會話對象");application.setAttribute("iamapplication", "我是應用對象");%><%//request.getRequestDispatcher("_06page.jsp").forward(request, response);response.sendRedirect("_06page.jsp");%>
</body>
</html>
啟動tomcat,瀏覽器訪問http://localhost/_05page.jsp,null null 我是會話對象 我是應用對象
修改_06page.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><%=pageContext.getAttribute("iampageContext")%><%=request.getAttribute("iamrequest")%><%=session.getAttribute("iamsession")%> <%=application.getAttribute("iamapplication")%><%request.getRequestDispatcher("_05page.jsp").forward(request, response);%>
</body>
</html>
啟動tomcat,瀏覽器訪問http://localhost/_05page.jsp,報錯:該網頁無法正常運作,localhost將您重定向的次數過多。
登錄完善
改造login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8" isErrorPage="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body><%if(request.getAttribute("msg")!=null){ %><%=request.getAttribute("msg") %><%} %><form action="/loginTest" method="post">賬號:<input type="text" name="name"><br>密碼:<input type="password" name="password"><input type="submit" value="post"></form>
</body>
</html>
LoginServletTest設置數據到session作用域
@WebServlet("/loginTest")
public class LoginServletTest extends HttpServlet{@Overrideprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {ServletContext servletContext = req.getServletContext();HttpSession session = req.getSession();req.setCharacterEncoding("UTF-8");resp.setContentType("text/html;charset=utf-8");String name = req.getParameter("name");String password = req.getParameter("password");if ( name.equals("zhangsan") && password.equals("123456") ) {session.setAttribute("name", "zhangsan");resp.sendRedirect("main.jsp"); //這里在WEB-INF外重定向可以訪問} else {req.setAttribute("msg", "登錄失敗");// 需要訪問另外一個servlet,把參數傳進頁面打印出來
// req.getRequestDispatcher("/AAAServlet").forward(req, resp);req.getRequestDispatcher("login.jsp").forward(req, resp);}}
}
新建main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
恭喜你登錄成功<%=session.getAttribute("name")%>
</body>
</html>