什么是jsp


1、什么是jsp呢?

實際上jsp就是一個servlet

在html頁面中嵌入我們的java腳本

jsp的執行過程

1、翻譯階段

jsp在進行請求的時候會先被翻譯成java文件

2、編譯階段

在將java文件編譯成.class文件

3、執行

將處理之后的結果推送到我們的頁面進行展示

如果我們jsp頁面第二次訪問直接加載編譯好的.class文件,如果說第二次加載整整個文件,它修改了會再次進行翻譯,在編譯 在執行

2、jsp頁面元素組成

1、page指令

作用:設置內部的多個屬性定義整個頁面的屬性

設置的屬性在整個頁面都可以使用

2、小腳本和表達式

小腳本: <% 小腳本 ---java代碼 %>

表達式: <%= %> 你可以堪稱是輸出語句

3、靜態內容

HTML靜態文本

4、指令:

以“<%@ ” 開始,以“%> ” 結束

5、聲明 --方法的聲明

​ <%! 方法 %>

6、注釋

<%-- 客戶端不能查看到 --%>

2、jsp中使用jdbc訪問數據庫

需求: 查詢學生表信息展示到我們的jsp頁面 jsp+jdbc 采用三層架構方式

采用的是三層架構的格式

1、將我們用的三層架構的包打出來

1632710418952

2、編寫我們的basedao

z直接將我們之前寫好的basedao拿過來使用

3、編寫我們的實體對象(student)

public class StudentEntity {
	private int stuId;
	private String stuName;
	private String gender;
	private int age;
	private Date birthday;
	private int  studentNO;
	private int ClassID;
	private String city;
	private String email;
	public int getStuId() {
		return stuId;
	}
	public void setStuId(int stuId) {
		this.stuId = stuId;
	}
	public String getStuName() {
		return stuName;
	}
	public void setStuName(String stuName) {
		this.stuName = stuName;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public int getStudentNO() {
		return studentNO;
	}
	public void setStudentNO(int studentNO) {
		this.studentNO = studentNO;
	}
	public int getClassID() {
		return ClassID;
	}
	public void setClassID(int classID) {
		ClassID = classID;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
}

4、編寫dao層接口

//查詢學生表中所有信息
	public List<StudentEntity> getStuList(String sql);

5、編寫dao接口實現

PreparedStatement pst = null;
	ResultSet rs = null;
	@Override
	public List<StudentEntity> getStuList(String sql) {
		//聲明一個最終返回的集合
		List<StudentEntity> stulist = new ArrayList<StudentEntity>();
		//連接數據庫
		Connection  connection=BaseDao.getConnection();
		//執行sql--先產生一個PreparedStatement
		try {
			pst = connection.prepareStatement(sql);
			rs = pst.executeQuery(); //拿到結果集
			while(rs.next()) {
				//聲明一個學生對象
				StudentEntity student = new StudentEntity();
				student.setStuId(rs.getInt("stuId"));
				student.setStuName(rs.getString("stuName"));
				student.setAge(rs.getInt("age"));
				student.setCity(rs.getString("city"));
				stulist.add(student);//將對象添加到集合中
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return stulist;
	}

6、編寫service接口

//查詢student表所有信息
	public List<StudentEntity> findAll();

7、編寫service接口實現

	//實例化dao層對象
	StudentDaoImpl StudentDao = new StudentDaoImpl();
	@Override
	public List<StudentEntity> findAll() {
		//編寫查詢sql
		 String sql = "select * from studentinfo";
		 //調用dao層中的查詢方法
		 List<StudentEntity> userlist =StudentDao.getStuList(sql);
		 return userlist;
	}

8、編寫jsp---stuinf.jsp

將我們學生信息展示到頁面中

<%@page import="java.util.List"%>
<%@page import="com.dzqc.testStu.entity.StudentEntity"%>
<%@page import="com.dzqc.testStu.service.impl.StudentServiceImpl"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<table border="2" align = "center">
		<caption>學生信息列表</caption>
		<tr>
			<td>學號</td>
			<td>姓名</td>
			<td>年齡</td>
			<td>城市</td>
		</tr>
		<!-- 讀取業務層數據 -->
		<%
		//實例化業務層對象
		StudentServiceImpl studentServiceImpl = new StudentServiceImpl();
		//調用查詢學生多有的信息方法
	//	List<StudentEntity> stulist =studentServiceImpl.findAll();
		List<StudentEntity> stulist = studentServiceImpl.findAll();
		for(int i = 0 ; i<stulist.size();i++){
			StudentEntity stu =stulist.get(i);
		%>
			<tr>	
				<td><%=stu.getStuId() %></td>
				<td><%=stu.getStuName() %></td>
				<td><%=stu.getAge() %></td>
				<td><%=stu.getCity() %></td>
			</tr>
		<%}%>
	</table>
</body>
</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM