浅学 jsp
一、jsp
1. jsp介绍
2. JSP执行流程
3. JSP的本质还是Servlet
4. JSP语法
4.1 注释
<%–注释格式–%>
4.2 代码块
<% System.out.println(“ssss”);%>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<title>JSP</title>
</head>
<body>
<h1> 第一个jsp 实例ssss </h1>
<%--s--%>
<%--打印到控制台 --%>
<% System.out.println("ssss");%>
<%--输出到页面 out是JSPWriter对象输出到页面上--%>
<% out.println("输出到页面<br>");
String str="你好";
out.println(str);
%>
</body>
</html>
4.3JSP表达式
<%–jsp表达式 相当于 out.println()–%>
<%=“hello” %>
4.4JSP声明
<%! String string=“123”; %>
<%=string%>
5. JSP指令
page 指令
错误信息跳转,出现1/0 跳转到error.jsp,只是访问test.jsp会自动跳转到error.jsp
include 包含指令包含 其他页面
被包含页面,然后访问test.jsp页面会使用包含页面元素
taglib 引入外部标签库
6. JSP细节 九大隐式对象
6.1 pageContext 页面域对象
7. Servlet四大域对象小结
8. MVC模型
9. 登录&添加学生&查看学生列表案例
首页
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>学生管理系统首页</title>
</head>
<body>
<% Object username = session.getAttribute("username");
if (username == null) {
%>
<a href="/stu/login.jsp">请登录</a>
<%} else {%>
<a href="/stu/addStudent.jsp">添加学生</a>
<a href="/stu/listStudentServlet">查看学生</a>
<%}%>
</body>
</html>
登录页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>学生登录</title>
</head>
<body>
<form action="/stu/loginServlet" method="get" autocomplete="off">
用户名: <input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<button type="submit">登录</button>
</form>
</body>
</html>
登录请求处理
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取用户名和密码
String username = req.getParameter("username");
String password = req.getParameter("password");
//判断用户名
if (username==null||"".equals(username)){
//用户名为空 重定向到登录界面
resp.sendRedirect("/stu/login.jsp");
return;
}
//不为空 存入会话域
HttpSession session = req.getSession();
session.setAttribute("username",username);
//重定向到首页jsp
resp.sendRedirect("/stu/index.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
}
添加学生页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加学生</title>
</head>
<body>
<form action="/stu/addServlet" method="get" autocomplete="off">
姓名: <input type="text" name="username"><br>
年龄:<input type="number" name="age"><br>
分数:<input type="number" name="score"><br>
<button type="submit">保存</button>
</form>
</body>
</html>
添加学生请求处理
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
@WebServlet("/addServlet")
public class addStudent extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1. 获取表单数据
String username = req.getParameter("username");
String age = req.getParameter("age");
String score = req.getParameter("score");
//2. 创建学生对象并赋值
Student student = new Student(username, Integer.parseInt(age), Integer.parseInt(score));
//3.将学生对象的数据保存到d:\\stu.txt
BufferedWriter bw=new BufferedWriter(new FileWriter("d:\\stu.txt",true));
bw.write(student.getUsername()+","+student.getAge()+","+student.getScore());
bw.newLine();
bw.close();
//4.通过定时刷新功能响应给浏览器
resp.setContentType("text/html;charset=UTF-8");
resp.getWriter().write("添加成功。2秒后自动跳转到首页");
resp.setHeader("Refresh","2;URL=/stu/index.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
}
查看学生列表页面
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.example.jsp.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>查看学生</title>
</head>
<body>
<table width="600px" border="1px">
<tr>
<th>姓名</th>
<th>年龄</th>
<th>成绩</th>
</tr>
<% ArrayList<Student> students= (ArrayList<Student>) session.getAttribute("stus");
for (Student stu:students ){
%>
<tr>
<td><%=stu.getUsername()%></td>
<td><%=stu.getAge()%></td>
<td><%=stu.getScore()%></td>
</tr>
<%}%>
</table>
</body>
</html>
列表请求处理
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
@WebServlet("/listStudentServlet")
public class listStudentServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1. 创建字符输入流对象 关联读取文件
BufferedReader br=new BufferedReader(new FileReader("d:\\stu.txt"));
//2. 创建集合对象 用于保存Student对象
ArrayList<Student > list=new ArrayList<>();
//3.循环读取文件中的数据 将数据封装到Student 对象中 再把多个学生对象添加到集合中
String line;
while ((line=br.readLine())!=null){
// 张三 18 100
Student stu=new Student();
String[] arr = line.split(",");
stu.setUsername(arr[0]);
stu.setAge(Integer.parseInt(arr[1]));
stu.setScore(Integer.parseInt(arr[2]));
list.add(stu);
}
//4.遍历集合 将数据响应到浏览器
//获取输出流对象
resp.setContentType("text/html;charset=UTF-8");
// PrintWriter pw = resp.getWriter();
// for (Student s:list){
// pw.write(s.getUsername()+","+s.getAge()+","+s.getScore());
// pw.write("<br>");
// }
req.getSession().setAttribute("stus",list);
//5.重定向
resp.sendRedirect("/stu/list.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doGet(req, resp);
}
}
实体类
public class Student {
private String username;
private int age;
private int score;
@Override
public String toString() {
return "Student{" +
"username='" + username + '\'' +
", age=" + age +
", score=" + score +
'}';
}
public Student() {
}
public Student(String username, int age, int score) {
this.username = username;
this.age = age;
this.score = score;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}