日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

尚硅谷最新版JavaWeb全套教程,java web零基础入门完整版(二)

發(fā)布時間:2025/3/19 java 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 尚硅谷最新版JavaWeb全套教程,java web零基础入门完整版(二) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

書城項目

JavaEE三層架構(gòu)介紹

搭建書城項目環(huán)境


IDEA工具Debug的使用






JSP

什么是jsp


jsp頁面的本質(zhì)






jsp的page指令


  • 雖然 /瀏覽器解析的時候是端口號,但是jsp本質(zhì)最終還是被翻譯成servlet程序,所以還是被 服務(wù)器 解析,那么 /服務(wù)器解析得到的還是工程路徑

聲明腳本

<%@ page import="java.util.Map" %> <%@ page import="java.util.HashMap" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %><html> <head><title>Title</title> </head> <body><%--1. 聲明類屬性--%><%!private Integer id;private String name;private static Map<String, Object> map;%><%--2.聲明static靜態(tài)代碼塊--%><%!static {map = new HashMap<String, Object>();map.put("key1", "value1");map.put("key2", "value2");map.put("key3", "value3");}%><%--3.聲明類方法--%><%!public int abc() {return 12;}%><%--4.生成內(nèi)部類--%><%!public static class A {private Integer id = 12;private String abc = "abc";}%> </body> </html>
  • jsp也會自動導(dǎo)包
  • 在聲明了類的屬性后,進(jìn)入a.java即翻譯后的程序后,發(fā)現(xiàn)我們定義的屬性已經(jīng)在源碼中啦



表達(dá)式腳本

<%@ page import="java.util.Map" %> <%@ page import="java.util.HashMap" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %><html> <head><title>Title</title> </head> <body><%--1. 聲明類屬性--%><%!private Integer id;private String name;private static Map<String, Object> map;%><%--2.聲明static靜態(tài)代碼塊--%><%!static {map = new HashMap<String, Object>();map.put("key1", "value1");map.put("key2", "value2");map.put("key3", "value3");}%><%--3.聲明類方法--%><%!public int abc() {return 12;}%><%--4.生成內(nèi)部類--%><%!public static class A {private Integer id = 12;private String abc = "abc";}%><%--表達(dá)式腳本可以在頁面輸出 整型、浮點型、字符串、對象--%><%=12 %> <br><%=12.12 %> <br><%="這是一個字符串" %> <br><%=map %> <br></body> </html>

  • 翻譯后的程序中,在 _jspService 這個方法中 :
<%--表達(dá)式腳本可以在頁面輸出 整型、浮點型、字符串、對象--%><%=12 %> <br><%=12.12 %> <br><%="這是一個字符串" %> <br><%=map %> <br><%=request.getParameter("username")%> </body> </html>

代碼腳本

  • _jspService方法中的現(xiàn)有對象都可以直接使用 :

  • 由多個代碼腳本塊組合完成一個完整的java語句 :

  • 代碼腳本可以和表達(dá)式腳本一起使用,在jsp頁面輸出數(shù)據(jù) :

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html> <head><title>Title</title> </head> <body><table border="1" cellspacing="0"><%for (int j = 0; j < 10; j ++ ) {%><tr><td><%=j + 1%></td></tr><%}%></table> </body> </html>



jsp中的三種注釋

1.html注釋
<!-- 這是html注釋 -->
html注釋會被翻譯到j(luò)ava源代碼中。在_jspService方法里,以out.write輸出到客戶端

  • jsp注釋是jsp中真正的注釋

jsp中的九大內(nèi)置對象

jsp中的內(nèi)置對象,是指tomcat在翻譯jsp頁面成為servlet源代碼后,內(nèi)部提供的九大對象,叫內(nèi)置對象

public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)throws java.io.IOException, javax.servlet.ServletException {final java.lang.String _jspx_method = request.getMethod();if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method) && !javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSP 只允許 GET、POST 或 HEAD。Jasper 還允許 OPTIONS");return;}final javax.servlet.jsp.PageContext pageContext;javax.servlet.http.HttpSession session = null;final javax.servlet.ServletContext application;final javax.servlet.ServletConfig config;javax.servlet.jsp.JspWriter out = null;final java.lang.Object page = this;

request : 請求對象
response :響應(yīng)對象
pageContext :jsp的上下文對象
session :會話對象
application :ServletContext對象(Servlet上下文對象)
config :ServletConfig對象
out :jsp輸出流對象
page :指向當(dāng)前jsp的對象
第九個對象需要isErrorPage為true才會出現(xiàn),也就是 exception :異常對象

jsp四大域?qū)ο蟮难菔?/h2>

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body><h1>scope.jsp頁面</h1><%// 往四個域中都分別存了數(shù)據(jù)pageContext.setAttribute("key", "pageContext");request.setAttribute("key", "request");session.setAttribute("key", "session");application.setAttribute("key", "application");%>pageContext域是否有值 :<%=pageContext.getAttribute("key")%> <br>request域是否有值 :<%=request.getAttribute("key")%> <br>session域是否有值 :<%=session.getAttribute("key")%> <br>application域是否有值 :<%=application.getAttribute("key")%> <br><%request.getRequestDispatcher("/scope2.jsp").forward(request, response);%> </body> </html> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body><h1>scope2.jsp頁面</h1>pageContext域是否有值 :<%=pageContext.getAttribute("key")%> <br>request域是否有值 :<%=request.getAttribute("key")%> <br>session域是否有值 :<%=session.getAttribute("key")%> <br>application域是否有值 :<%=application.getAttribute("key")%> <br> </body> </html>


  • 整個請求轉(zhuǎn)發(fā)還是 一次請求
  • 關(guān)閉瀏覽器以后再打開scope2.jsp頁面(沒有關(guān)閉服務(wù)器) :
  • 重新部署tomcat,再訪問scope2 :
  • 數(shù)據(jù)存儲在域?qū)ο笾行枰紦?jù)空間,如果使用范圍大的,可能已經(jīng)被用過了,但是并沒有釋放空間,所以優(yōu)先使用范圍小的,使用完自動銷毀

out和response.getWriter輸出的區(qū)別


<%out.write("out 1");out.flush();out.write("out 2");response.getWriter().write("res 1");response.getWriter().write("res 2");%>

常用標(biāo)簽之 靜態(tài)包含

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>main.jsp</title> </head> <body>頭部信息 <br>主題內(nèi)容 <br><%--<%@include file=""%> 就是靜態(tài)包含file屬性制定你要包含的jsp頁面的路徑地址中第一個斜杠 / 表示 http://ip:port/工程路徑/ 映射到代碼的web目錄靜態(tài)包含的特點 :1、靜態(tài)包含不會翻譯被包含的jsp頁面2、靜態(tài)包含其實是被被包含的jsp頁面的代碼拷貝到包含的位置執(zhí)行輸出--%><%@include file="/include/footer.jsp"%> </body> </html> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>footer.jsp</title> </head> <body>頁腳信息 <br> </body> </html>

常用標(biāo)簽之 動態(tài)包含


動態(tài)包含的底層原理 :

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>main.jsp</title> </head> <body>頭部信息 <br>主題內(nèi)容 <br><%--<jsp:include page=""></jsp:include> 這是動態(tài)包含動態(tài)包含也可以像靜態(tài)包含一樣把被包含的內(nèi)容輸出到包含位置動態(tài)包含的特點 :1、動態(tài)包含會把包含的jsp頁面也翻譯成為java代碼2、動態(tài)包含底層代碼使用如下去調(diào)用被包含的jsp頁面執(zhí)行輸出JspRuntimeLibrary.include(request, response, "/include/footer.jsp", out, false);3、動態(tài)包含,還可以傳遞參數(shù)--%><jsp:include page="/include/footer.jsp"><jsp:param name="username" value="bbj"/><jsp:param name="password" value="root"/></jsp:include> </body> </html> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>footer.jsp</title> </head> <body>頁腳信息 <br><%=request.getParameter("password")%> </body> </html>
  • 一般用jsp只是輸出頁面數(shù)據(jù),不會有太復(fù)雜代碼,因此一般使用的是 靜態(tài)包含

常用標(biāo)簽之 請求轉(zhuǎn)發(fā)

練習(xí)一 :在jsp頁面中輸出九九乘法口訣表

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title> </head> <body><table><% for (int i = 1; i <= 9; i ++ ) { %><tr><% for (int j = 1; j <= i; j ++ ) { %><td><%= j + "x" + i + "=" + (i * j)%></td><% } %></tr><% } %></table> </body> </html>

練習(xí)二 :遍歷輸出十個學(xué)生信息到表格中

package com.atguigu.pojo;public class Student {private Integer id;private String name;private Integer age;private String phone;// 構(gòu)造器(無參 + 全參)+ Getter+Setter + toString@Overridepublic String toString() {return "Student{" +"id=" + id +", name='" + name + '\'' +", age=" + age +", phone='" + phone + '\'' +'}';}public Student(Integer id, String name, Integer age, String phone) {this.id = id;this.name = name;this.age = age;this.phone = phone;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}public Student() {} } <%@ page import="com.atguigu.pojo.Student" %> <%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title><style>table {border: 1px pink solid;width: 600px;border-collapse: collapse;}td, th {border: 1px pink solid;}</style> </head> <body><%List<Student> studentList = new ArrayList<Student>();for (int i = 0; i < 10; i++) {int t = i + 1;studentList.add(new Student(t, "name" + t, 18 + t, "phone" + t));}%><table><tr><td>編號</td><td>姓名</td><td>年齡</td><td>電話</td><td>操作</td></tr><% for (Student student : studentList) { %><tr><td><%=student.getId() %></td><td><%=student.getName()%></td><td><%=student.getAge()%></td><td><%=student.getPhone()%></td><td>操作、修改</td></tr><% } %></table> </body> </html>

請求轉(zhuǎn)發(fā)的使用說明

請求轉(zhuǎn)發(fā)的使用 :

  • servlet程序其實是不太適合回傳html數(shù)據(jù)給后端的,比較麻煩,而是jsp頁面比較適合干這件事
  • 因此,我們把這件事交給jsp,專門用來顯示學(xué)生信息
  • 那么,我們就需要serlet和jsp共同完成這件事情,
  • 因此,需要 請求轉(zhuǎn)發(fā),剛好請求轉(zhuǎn)發(fā)共享request域數(shù)據(jù)
package com.atguigu.pojo;import javax.servlet.ServletException; 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;public class SearchStudentServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// 獲取請求的參數(shù)// 發(fā)sql語句查詢學(xué)生的信息// 使用for循環(huán)生成查詢到的數(shù)據(jù)做模擬List<Student> studentList = new ArrayList<Student>();for (int i = 0; i < 10; i++) {int t = i + 1;studentList.add(new Student(t, "name" + t, 18 + t, "phone" + t));}// 保存查詢到的結(jié)果(學(xué)生信息)到request域中req.setAttribute("stuList", studentList);// 請求轉(zhuǎn)發(fā)到 showStudent.jsp頁面req.getRequestDispatcher("/test/showStudent.jsp").forward(req, resp);} } <%@ page import="com.atguigu.pojo.Student" %> <%@ page import="java.util.List" %> <%@ page import="java.util.ArrayList" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>Title</title><style>table {border: 1px pink solid;width: 600px;border-collapse: collapse;}td, th {border: 1px pink solid;}</style> </head> <body><%List<Student> studentList = (List<Student>) request.getAttribute("stuList");%><table><tr><td>編號</td><td>姓名</td><td>年齡</td><td>電話</td><td>操作</td></tr><% for (Student student : studentList) { %><tr><td><%=student.getId() %></td><td><%=student.getName()%></td><td><%=student.getAge()%></td><td><%=student.getPhone()%></td><td>操作、修改</td></tr><% } %></table> </body> </html>
  • 注意,寫完代碼以后直接訪問showStudent.jsp會 空指針異常
  • 因為,我們直接訪問jsp頁面,沒有經(jīng)過servlet程序,還沒有數(shù)據(jù)
  • 所以,訪問的時候一定要先訪問這個servlet
  • 記得給這個servlet加上地址

什么是Listener監(jiān)聽器

ServletContextListener監(jiān)聽器



package com.atguigu.listener;import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener;public class MyServletContextListenerImpl implements ServletContextListener {@Overridepublic void contextInitialized(ServletContextEvent sce) {System.out.println("ServletContext對象被創(chuàng)建了");}@Overridepublic void contextDestroyed(ServletContextEvent sce) {System.out.println("ServletContext對象被銷毀了");} } <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"version="4.0"><servlet><servlet-name>SearchStudentServlet</servlet-name><servlet-class>com.atguigu.pojo.SearchStudentServlet</servlet-class></servlet><servlet-mapping><servlet-name>SearchStudentServlet</servlet-name><url-pattern>/searchStudentServlet</url-pattern></servlet-mapping><!-- 配置監(jiān)聽器--><listener><listener-class>com.atguigu.listener.MyServletContextListenerImpl</listener-class></listener> </web-app>

總結(jié)

以上是生活随笔為你收集整理的尚硅谷最新版JavaWeb全套教程,java web零基础入门完整版(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。