会将html代码和Java代码(包括引入的包等等)都写着在里面,TomCat会自动jsp文件转换成servlet文件,来代替之前的手动创建servlet,但是这样很臃肿,所有代码都在jsp中
由上可知,servlet会根据你已经编写好的jsp文件而撞见TomCat文件,提一嘴,编写java代码在jsp文件中时有三种,分别对应了打印,方法等等,具体看下面图,(自动创建的servlet类会根据不同的<%...%>
等来在servlet类中添加对应的东西),下面图中的JSP脚本分类中的放在.......
是指自创的servlet
中进行添加
因为jsp比较麻烦,需要将Java和html的代码全部写在jsp中,所以有了el表达式
,将Java代码写在Java类中,再将其数据直接扔进域
中(域有很多种),然后再在jsp中直接调用域中的数据,这样就方便很多,看起来不复杂
代码实现:tomcat运行后访问Java代码的路径"/se"
,会自动跳到jsp
代码,其中的EL表达式
会在域中寻找与之对应的数据
package com.itjh.servlet;
import com.itjh.testdao.user;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/se")
public class ServletEl extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<user> list=new ArrayList<user>();
list.add(new user("chenlong",18));
list.add(new user("xiangsu",20));
//存储到request域中,且起个名字叫 list
request.setAttribute("list",list);
//转发到el_demo.jsp中
request.getRequestDispatcher("/el_demo.jsp").forward(request,response);
System.out.println("谭志郎");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
jsp文件:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:if test="${id==1}">
<h2>xib</h2>
</c:if>
</body>
</html>
代替了原来的if语句
代替了原来jsp中的循环遍历
这样就看来简洁了,比如之前在jsp中写Java遍历代码的的时候需要写很多,比较繁琐,所以换了一种表达式
先导两个包:
<dependency>
<groupId>taglibsgroupId>
<artifactId>standardartifactId>
<version>1.1.2version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.1.2version>
dependency>
可能会无法下载standard
包,于是添加下面这个新的阿里仓库:
<repositories>
<repository>
<id>nexus-aliyun</id>
<name>nexus-aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
完整版:这里我在插件中重新指定了端口号,因为原来的冲突了
<?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>jsplel</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.3.3</version>
<scope>provided</scope>
</dependency>
<!-- standard.jar -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>nexus-aliyun</id>
<name>nexus-aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<build>
<plugins>
<!-- tomcat插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8001</port>
</configuration>
</plugin>
</plugins>
</build>
</project>
java代码部分:
package com.itjh.servlet;
import com.itjh.dao.User;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/sif")
public class Servletif extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<User> list=new ArrayList<User>();
list.add(new User(19,"成龙","男"));
list.add(new User(21,"叶全真","女"));
request.setAttribute("list",list);
request.setAttribute("id",1);
//转到指定的jsp文件中
request.getRequestDispatcher("/jspl.jsp").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
要在jsp文件中添加
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!--不要忽略el语句-->
<%@ page isELIgnored="false"%>
jsp文件完整版:代替if语句和foreach语句(包括其他遍历语句)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<c:if test="${id==1}">
<h2>xib</h2>
</c:if>
<!-- var:随便起名;items对应Java代码中的域的键;h1中的-->
<!--属性名对标User实体类中的得属性的get方法-->
<c:forEach items="${list}" var="us">
<h1>${us.age}</h1>
<h1>${us.name}</h1>
<h1>${us.gander}</h1>
</c:forEach>
</body>
</html>
结果:在网页上通过访问Java代码路径,对自动跳转到jsp文件中,得到xib
和list数组的遍历的结果
总结:
EL:一种将外面的Java装进jsp的一种方式
JSTL:一种将EL带进来的数据进行if和foreach(等遍历) 的语法