本例结构图
文章目录
maven项目pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Web_Jsp</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>18</maven.compiler.source>
<maven.compiler.target>18</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!--JSTL tag Lib-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
<!--tomcat plugin-->
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<!--<configuration>
<path>/xxx</path>
</configuration>-->
</plugin>
</plugins>
</build>
</project>
本例用到的Brand类
package com.diy.pojo;
/**
* 品牌实体类
*/
public class Brand {
// id 主键
private Integer id;
// 品牌名称
private String brandName;
// 企业名称
private String companyName;
// 排序字段
private Integer ordered;
// 描述信息
private String description;
// 状态:0:禁用 1:启用
private Integer status;
public Brand() {
}
public Brand(Integer id, String brandName, String companyName, String description) {
this.id = id;
this.brandName = brandName;
this.companyName = companyName;
this.description = description;
}
public Brand(Integer id, String brandName, String companyName, Integer ordered, String description, Integer status) {
this.id = id;
this.brandName = brandName;
this.companyName = companyName;
this.ordered = ordered;
this.description = description;
this.status = status;
}
public Integer getId() {
System.out.println("getId method has be invoked~~~");
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getBrandName() {
return brandName;
}
public void setBrandName(String brandName) {
this.brandName = brandName;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public Integer getOrdered() {
return ordered;
}
public void setOrdered(Integer ordered) {
this.ordered = ordered;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
@Override
public String toString() {
return "Brand{" +
"id=" + id +
", brandName='" + brandName + '\'' +
", companyName='" + companyName + '\'' +
", ordered=" + ordered +
", description='" + description + '\'' +
", status=" + status +
'}';
}
}
文章目录
jsp的写法
第一个jsp页面,直接用tomcat运行后的访问路径加 /hello.jsp 即可访问jsp资源,下同
<%--
Created by IntelliJ IDEA.
User: you
Date: 2022/07/14
Time: 15:13
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>
<h1>Hello Jsp</h1>
<%-- you can't write System.out.println here ,will response 500--%>
<%
out.println("Hello Jsp");
int i = 100;
%>
<%="here is a new hello"%>
<%=i%>
<%!
String name = "luoxiang";
void testShow(){};
%>
</body>
</html>
第二个
<%@ page import="java.util.List" %>
<%@ page import="com.diy.pojo.Brand" %>
<%@ page import="java.util.ArrayList" %><%--
Created by IntelliJ IDEA.
User: you
Date: 2022/07/14
Time: 16:58
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
// here we use collection instead of really query database
List<Brand> bs = new ArrayList<Brand>();
bs.add(new Brand(1,"Nokia","AIKON", 100, "You never forget Nokia" ,1));
bs.add(new Brand(2,"Motorola","MOTO", 110, "You ever remember Moto" ,0));
bs.add(new Brand(3,"Xiaomi","LEIJUN", 120, "Are you OK" ,1));
%>
<html>
<head>
<title>brand</title>
</head>
<body>
<input type="button" value="Add New"><br>
<hr>
<table border="1" cellspacing="0" width="800">
<tr>
<th>NO.</th>
<th>Brand Name</th>
<th>Company Name</th>
<th>Ordered</th>
<th>Description</th>
<th>Status</th>
<th>Operation</th>
</tr>
<%
for (int i = 0; i < bs.size(); i++) {
Brand br = bs.get(i);
%>
<tr align="center">
<td><%=br.getId()%></td>
<td><%=br.getBrandName()%></td>
<td><%=br.getCompanyName()%></td>
<td><%=br.getOrdered()%></td>
<td><%=br.getDescription()%></td>
<%
if (br.getStatus() == 1) {
%>
<td><%="Normal"%></td>
<%
} else {
%>
<td><%="Disable"%></td>
<%
}
%>
<%--<td><%=br.getStatus()%></td>--%>
<td><a href="#">Edit</a> <a href="#">Delete</a> </td>
</tr>
<%
}
%>
</table>
</body>
</html>
文章目录
el语句的用法
对应el语句的servlet类,运行tomcat后访问本servlet的访问路径可达el的jsp
package com.diy.web;
import com.diy.pojo.Brand;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/demo1")
public class Servlet_for_EL extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//prepare data for el_demo, here we use collection instead of database
List<Brand> bs = new ArrayList<Brand>();
bs.add(new Brand(1,"Nokia","AIKON", 100, "You never forget Nokia" ,1));
bs.add(new Brand(2,"Motorola","MOTO", 110, "You ever remember Moto" ,0));
bs.add(new Brand(3,"Xiaomi","LEIJUN", 120, "Are you OK" ,1));
//2.put this in an 域object
request.setAttribute("brands",bs);
//3.forward it to el_demo
request.getRequestDispatcher("/el_demo.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
接受servlet转发数据的el语句所在jsp页
<%--
Created by IntelliJ IDEA.
User: you
Date: 2022/07/14
Time: 23:08
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>el_demo</title>
</head>
<body>
<%--Receive data from servlet forwarding--%>
${brands}
</body>
</html>
文章目录
jstl标签用法
一,<c:if>标签
对应的servlet类,运行tomcat后访问本servlet的访问路径可达if的jsp
package com.diy.web;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
/**
* for jstl_if.jsp
*/
@WebServlet("/demo2")
public class ServletDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("status",1);
request.getRequestDispatcher("/jstl_if.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
对应使用了jstl的jsp
<%--
Created by IntelliJ IDEA.
User: you
Date: 2022/07/14
Time: 23:41
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>jstl_if</title>
</head>
<body>
<%--<c:if test="true">
<h1>c if is true</h1>
</c:if>
<c:if test="false">
<h1>c if is false</h1>
</c:if>--%>
<c:if test="${status == 1}">
Normal
</c:if>
<c:if test="${status ==0}">
Disable
</c:if>
</body>
</html>
二,<c:foreach>标签
访问本servlet
package com.diy.web;
import com.diy.pojo.Brand;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* for jstl_foreach.jsp
*/
@WebServlet("/demo3")
public class ServletDemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//prepare data for el_demo, here we use collection instead of database
List<Brand> bs = new ArrayList<Brand>();
bs.add(new Brand(1,"Nokia","AIKON", 100, "You never forget Nokia" ,1));
bs.add(new Brand(2,"Motorola","MOTO", 110, "You ever remember Moto" ,0));
bs.add(new Brand(3,"Xiaomi","LEIJUN", 120, "Are you OK" ,1));
//2.put this in an 域object
request.setAttribute("brands",bs);
//3.forward it to el_demo
request.getRequestDispatcher("/jstl_for_each.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
对应的jsp
<%--
Created by IntelliJ IDEA.
User: you
Date: 2022/07/14
Time: 23:58
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>jstl_foreach</title>
</head>
<body>
<input type="button" value="Add New"><br>
<hr>
<table border="1" cellspacing="0" width="800">
<tr>
<th>NO.</th>
<th>Brand Name</th>
<th>Company Name</th>
<th>Ordered</th>
<th>Description</th>
<th>Status</th>
<th>Operation</th>
</tr>
<c:forEach items="${brands}" var="brand" varStatus="id">
<tr align="center">
<%--<td>${brand.id}</td>--%>
<%--<td>${id.index}</td>--%>
<td>${id.count}</td>
<td>${brand.brandName}</td>
<td>${brand.companyName}</td>
<td>${brand.ordered}</td>
<td>${brand.description}</td>
<c:if test="${brand.status == 1}">
<td>Normal</td>
</c:if>
<c:if test="${brand.status == 0}">
<td>Disable</td>
</c:if>
<%--<td>${brand.status}</td>--%>
<td><a href="#">Edit</a> <a href="#">Delete</a> </td>
</tr>
</c:forEach>
</table>
<hr>
<c:forEach begin="1" end="10" step="1" var="i">
<a href="#">${i}</a>
</c:forEach>
</body>
</html>