一、代码
1.代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table>
<%--方法区可以将java代码截为两段--%>
<%
for (int i = 1; i < 9; i++) {
%>
<tr>
<%
for (int j = 1; j <= i; j++) {
%>
<%
}
%>
</tr>
<%
}
%>
</table>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: admin
Date: 2021/1/26
Time: 10:48
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--方法区--%>
<%
//方法中可以写哪些?1.变量的操作 2.程序结构 3.调用其他方法
for (int i = 0;i < 10; i++) {
System.out.println("hello jsp兄弟");
}
show();
%>
<%--类区--%>
<%!
//类中可以写哪些?1.属性的操作 2.代码块 3.声明/定义方法
public void show(){
System.out.println("大家好,我是百里半的优秀成员!");
}
%>
<%--输出表达式 response.getWriter().wrte()--%>
<%="<h1>hello<h1>"%>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--方法区可以将java代码截为两段--%>
<%
//循环输出10次 <h3>hello</h3>
for (int i = 0; i < 10; i++) {
%>
<h3>hello</h3>
<%
}
%>
</body>
</html>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
class Emp{
private int id;
private String name;
private int age;
public Emp(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
%>
<%
ArrayList<Emp> arr = new ArrayList();
arr.add(new Emp(1,"秦始皇",88));
arr.add(new Emp(2,"刘邦",68));
arr.add(new Emp(3,"刘备",48));
arr.add(new Emp(4,"杨广",28));
arr.add(new Emp(5,"李世明",18));
for (int i = 0; i < arr.size(); i++) {
%>
<table border="1" cellspacing="0" cellpadding="15">
<tr>
<td><%=arr.get(i).getId()%></td>
<td><%=arr.get(i).getName()%></td>
<td><%=arr.get(i).getAge()%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" import="java.util.ArrayList" errorPage="error.jsp" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--如果不忽略EL.页面显示3--%>
${1+2}
<%
//int i=6/0;
new ArrayList<>();
%>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.ArrayList" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="15">
<tr>
<td>1</td>
<td>员工</td>
<td>工资</td>
</tr>
<%@ include file="c.jsp"%> <%--b页面中引入c--%>
</table>
<%=a%>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" %>
<%
int a=9;
%>
<%!
class Emp{
private int id;
private String name;
private int age;
public Emp(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Emp{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
%>
<%
ArrayList<Emp> arr = new ArrayList();
arr.add(new Emp(1,"秦始皇",88));
arr.add(new Emp(2,"刘邦",68));
arr.add(new Emp(3,"刘备",48));
arr.add(new Emp(4,"杨广",28));
arr.add(new Emp(5,"李世明",18));
for (int i = 0; i < arr.size(); i++) {
%>
<tr>
<td><%=arr.get(i).getId()%></td>
<td><%=arr.get(i).getName()%></td>
<td><%=arr.get(i).getAge()%></td>
</tr>
<%
}
%>
<%--如果承认自己是一个处理错误的页面,则可以使用exception对象,输出错误信息 否则不能使用--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
exception.getMessage();
%>
<h2>
温馨提示:您访问的页面走丢了!
</h2>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
//域对象 pageContext request session application
pageContext.setAttribute("name","小明");
request.setAttribute("name","小红");
session.setAttribute("name","小芳");
application.setAttribute("name","小李");
%>
<hr>
<%--9个内置对象中,4个是域对象--%>
<%
//pageContext是最小的域对象,同时可以获取其它8个内置对象
//pageContext 可以获取其它的8个内置对象
ServletContext app=pageContext.getServletContext();
%>
<%=app%>
<hr>
<%=pageContext.getAttribute("name")%>
<%=request.getAttribute("name")%>
<%=session.getAttribute("name")%>
<%=application.getAttribute("name")%>
<%--a页面转发到b--%>
<%
//request.getRequestDispatcher("/3.object/b.jsp").forward(request,response);
%>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
//域对象 pageContext request session application
pageContext.setAttribute("name","小明");
request.setAttribute("name","小红");
session.setAttribute("name","小芳");
application.setAttribute("name","小李");
%>
<hr>
<%--9个内置对象中,4个是域对象--%>
<%
//pageContext是最小的域对象,同时可以获取其它8个内置对象
//pageContext 可以获取其它的8个内置对象
ServletContext app=pageContext.getServletContext();
%>
<%=app%>
<hr>
<%=pageContext.getAttribute("name")%>
<%=request.getAttribute("name")%>
<%=session.getAttribute("name")%>
<%=application.getAttribute("name")%>
<%--a页面转发到b--%>
<%
//request.getRequestDispatcher("/3.object/b.jsp").forward(request,response);
%>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
//域对象 pageContext request session application
pageContext.setAttribute("name","小明");
request.setAttribute("name","小红");
session.setAttribute("name","小芳");
application.setAttribute("name","小李");
%>
<hr>
<%--9个内置对象中,4个是域对象--%>
<%
//pageContext是最小的域对象,同时可以获取其它8个内置对象
//pageContext 可以获取其它的8个内置对象
ServletContext app=pageContext.getServletContext();
%>
<%=app%>
<hr>
<%=pageContext.getAttribute("name")%>
<%=request.getAttribute("name")%>
<%=session.getAttribute("name")%>
<%=application.getAttribute("name")%>
<%--a页面转发到b--%>
<%
//request.getRequestDispatcher("/3.object/b.jsp").forward(request,response);
%>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--在b页面获取4个域中的参数--%>
<%=pageContext.getAttribute("name")%>
<%=request.getAttribute("name")%>
<%=session.getAttribute("name")%>
<%=application.getAttribute("name")%>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: admin
Date: 2021/1/26
Time: 17:05
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--c中获取4个域中的参数--%>
<%=pageContext.getAttribute("name")%><%--第一个域:仅限于一个页面内存和取--%>
<%=request.getAttribute("name")%><%--第二个域:在一个请求的范围内--%>
<%=session.getAttribute("name")%><%--第三个域:会话域,只要不关闭浏览器或服务器,就属于同一个会话--%>
<%=application.getAttribute("name")%><%--第四个域:全局域,只要不关闭服务器,域中的数据就可以在任意地方获取到--%>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: admin
Date: 2021/1/26
Time: 17:11
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
//剩下的5个内置对象:
//页面 异常 响应 输出 配置(获取最大的域对象application)
out.println(page);
out.write("<br>"); //page就是this,当前的jsp被tomcat变成servlet后的那个类的对象
out.println(this);
out.write("<br>");
out.println(application);
out.write("<br>"); //配置对象 可以获取最大的域对象
ServletContext application1 = config.getServletContext();
out.println(application1);
out.write("<br>");
%>
</body>
</html>
<%@ page import="com.user.User" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
List<User> list =(List<User>) request.getAttribute("list");
%>
<table>
<tr>
<td>编号</td>
<td>用户名</td>
<td>密码</td>
<td>电话号码</td>
</tr>
<%@include file="b.jsp"%>
</table>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
for (User user : list) {
%>
<tr>
<td><%=user.getId()%></td>
<td><%=user.getName()%></td>
<td><%=user.getPsw()%></td>
<td><%=user.getNumber()%></td>
</tr>
<%
}
%>
</body>
</html>
<%@ page import="com.c3p0.service.impl.UserServiceImpl" %>
<%@ page import="com.user.User" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%!
UserServiceImpl s = new UserServiceImpl();
List<User> list = s.run();
%>
<table border="1" cellspacing="0" cellpadding="15">
<%
for (User user : list) {
%>
<tr>
<td><%=user.getId()%></td>
<td><%=user.getName()%></td>
<td><%=user.getPsw()%></td>
<td><%=user.getNumber()%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
集合遍历:
package cn.web;
import cn.bean.Users;
import cn.service.IUsersService;
import cn.service.impl.UsersServiceImpl;
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.IOException;
import java.util.List;
@WebServlet("/users")
public class UsersServlet extends HttpServlet {
//声明service对象作为成员属性
private IUsersService service=new UsersServiceImpl();
@Override//servlet中代码编写顺序:
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.调
List<Users> users = service.findUsers();
//2.存
req.getSession().setAttribute("users",users);
// 3.转发或重定向
resp.sendRedirect("/test/users2.jsp");
}
}
package mvc;
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.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
@WebServlet( "/as")
public class AServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ArrayList list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
PrintWriter w = response.getWriter();
w.write("<html>");
w.write("<head>");
w.write("</head>");
w.write("<body>");
for (int i = 0; i < list.size(); i++) {
w.print(list.get(i));
}
w.write("</body>");
w.write("</html>");
}
}
<%@ page import="cn.service.IUsersService" %>
<%@ page import="cn.service.impl.UsersServiceImpl" %>
<%@ page import="cn.bean.Users" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table border="1" cellpadding="15" cellspacing="0" bgcolor="#7fffd4">
<%
//servlet中代码编写顺序:取 调 存 转
List<Users> users = (List<Users>)session.getAttribute("users");
for (int i = 0; i < users.size(); i++) {
%>
<tr>
<td><%=users.get(i).getId()%></td>
<td><%=users.get(i).getName()%></td>
<td><%=users.get(i).getPsw()%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
<%@ page import="cn.service.IUsersService" %>
<%@ page import="cn.service.impl.UsersServiceImpl" %>
<%@ page import="cn.bean.Users" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table border="1" cellpadding="15" cellspacing="0" bgcolor="#7fffd4">
<%
//servlet中代码编写顺序:取 调 存 转
List<Users> users = (List<Users>)session.getAttribute("users");
for (int i = 0; i < users.size(); i++) {
%>
<tr>
<td><%=users.get(i).getId()%></td>
<td><%=users.get(i).getName()%></td>
<td><%=users.get(i).getPsw()%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
总结
以上就是jsp相关的所有代码和用法。