「这是我参与2022首次更文挑战的第39天,活动详情查看:2022首次更文挑战」
jsp练习
练习1
打印九九乘法表
<html>
<head>
</head>
<body >
<h1 >九九乘法表</h1>
<%
for(int i=1;i<10;i++){
for(int j=1;j<=i;j++){
%>
<%=j+"*"+i+"="+(i*j)%>
<%
}
%>
<br/>
<%
}
%>
</body>
</html>
练习2
存储学生信息并打印
pojo包下的student类
package pojo;
public class Student {
private String name;
private int id;
private int age;
public Student(String name, int id, int age) {
this.name = name;
this.id = id;
this.age = age;
}
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", id=" + id +
", age=" + age +
'}';
}
}
text1.jsp下
<%@ page import="java.util.List" %>
<%@ page import="pojo.Student" %>
<%@ page import="java.util.ArrayList" %>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<html>
<head>
<meta charset="utf-8"/>
<title>Insert title here</title>
<%-- 设置样式<style></style>--%>
<style>
table{
border: 1px black solid;
width: 300px;
}
td,tr{
border: 1px black solid;
width: 300px;
}
</style>
</head>
<body>
<%
List<Student> list=new ArrayList<>();
for (int i=1;i<=10;i++){
list.add(new Student("name"+i,i,10+i));
}
%>
<table>
<%for (Student student:list){%>
<%-- tr是一行,td为一列--%>
<tr>
<td><%=student.getName()%></td>
<td><%=student.getId()%></td>
<td><%=student.getAge()%></td>
</tr>
<% } %>
</table>
</body>
</html>
请求转发使用说明
流程图:
SearchStudentServlet类下
package com.Servlet;
import pojo.Student;
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 {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取请求参数
//发sql语句查询学生信息
//使用for循环生成查询到的数据做模拟
List<Student> list=new ArrayList<>();
for (int i=1;i<=10;i++){
list.add(new Student("name"+i,i,10+i));
}
//保存查询到的数据到Request域中
req.setAttribute("stuList", list);
//请求转发到之外的showStudent.jsp中
req.getRequestDispatcher("/showStudent.jsp").forward(req, resp);
}
}
web.xml下:
<servlet>
<servlet-name>SearchStudentServlet</servlet-name>
<servlet-class>com.Servlet.SearchStudentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SearchStudentServlet</servlet-name>
<url-pattern>/searchStudentServlet</url-pattern>
</servlet-mapping>
showStudent.jsp下
<%@ page import="java.util.List" %>
<%@ page import="pojo.Student" %>
<%@ page import="java.util.ArrayList" %>
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<html>
<head>
<meta charset="utf-8"/>
<title>Insert title here</title>
<%-- 设置样式<style></style>--%>
<style>
table{
border: 1px black solid;
width: 300px;
}
td,tr{
border: 1px black solid;
width: 300px;
}
</style>
</head>
<body>
<%
List<Student> list= (List<Student>) request.getAttribute("stuList");
%>
<table>
<%for (Student student:list){%>
<%-- tr是一行,td为一列--%>
<tr>
<td><%=student.getName()%></td>
<td><%=student.getId()%></td>
<td><%=student.getAge()%></td>
</tr>
<% } %>
</table>
</body>
</html>
运行结果:
Listener监听器
1、Listener监听器他是JavaWeb的三大组件之一。javaweb的三大组件分别是servlet程序、filter过滤器、Listenter监听器。
2、Listenter他是javaEE的规范,规范就是接口
3、监听器的作用是,监听某种事务的变化,然后通过回调函数,反馈给客户或程序去做一些相应的处理。
ServletContextListenter监听器
ServletContextListener他可以监听ServletContext对象的创建和销毁。
ServletContext对象在web工程启动的时候,在web工程停止的时候销毁。
ServletContextListener监听器监听ServletContext对象的步骤
1、编写一个类去实现ServletContextListener
2、实现器两个回调方法
3、到web.xml中去配置监听器
创建类和实线两个方法
package com.Listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class MyServletContextListenerImpl implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("Servlet对象被创建了");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("Servlet对象被销毁了");
}
}
web.xml中配置
<listener>
<listener-class>com.Listener.MyServletContextListenerImpl</listener-class>
</listener>