Jsp在web开发中常用到的技术(一)

本文阅读 2 分钟
首页 代码,Java 正文

文章目录 JSP学习总结 @TOC 一、JSP快速入门 二、JSP原理 三、JSP脚本 四、EL表达式 五、JSTL 标签 标签

img jsp坐标:

<dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.2</version>
      <scope>provided</scope>
    </dependency>

案例:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>Hello JSP</h1>
<%
    System.out.println("Hello JSP");
%>

</body>
</html>

运行结果: img

img img

img 案例:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <%!
        //定义两个变量
        int a=1;
        int b=3;
    %>
    <%!
        //定义一个test方法
        public String test(){ 
            //定义一个变量str
            String str="小飞侠";
            return str;
        }
    %>
</head>
<body>
<%
    //输出两个变量的和
    System.out.println(a+b);  //控制台输出4
%>
<br>
<%
    //调用test方法,输出返回值
    System.out.println(test()); //控制台输出小飞侠
%>
<br>
<%=a+b %>
<br>
<%=test()%>
</body>
</html>

运行结果: img

img 案例:

package com.study.pojo;

public class Student { 
    private String id;  //学号
    private String name;  //姓名
    private String gender;  //性别
    private int age;  //年龄

    public Student() { 
    }

    public Student(String id, String name, String gender, int age) { 
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    public String getId() { 
        return id;
    }

    public void setId(String id) { 
        this.id = id;
    }

    public String getName() { 
        return name;
    }

    public void setName(String name) { 
        this.name = name;
    }

    public String getGender() { 
        return gender;
    }

    public void setGender(String gender) { 
        this.gender = gender;
    }

    public int getAge() { 
        return age;
    }

    public void setAge(int age) { 
        this.age = age;
    }

    @Override
    public String toString() { 
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                '}';
    }
}

package com.study.Servlet;
import com.study.pojo.Student;
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.ArrayList;
import java.util.List;
@WebServlet("/el")
public class ElTestServlet extends HttpServlet { 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
        //准备数据
        List<Student> stus=new ArrayList<>();
        stus.add(new Student("001","小马哥","男",23));
        stus.add(new Student("002","小飞侠","男",24));
        stus.add(new Student("003","马奎斯","男",25));
        //保存到request域中
        req.setAttribute("students",stus);
        //req.setAttribute("status",1);
        req.getRequestDispatcher("/ElTest.jsp").forward(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
        this.doGet(req, resp);
    }
}

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
${ students}

</body>
</html>

注意:如果输出页面是${students},原因是缺少了一个能让页面识别el表达式的页头语句,在jsp页面头加:<%@page isELIgnored=“false”%>即可。 运行结果: img

<c:if>标签

img 坐标:

<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>

Jstl标签库:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

案例:

package com.study.Servlet;
import com.study.pojo.Student;
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.ArrayList;
import java.util.List;
@WebServlet("/el")
public class ElTestServlet extends HttpServlet { 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
        //准备数据
        List<Student> stus=new ArrayList<>();
        stus.add(new Student("001","小马哥","男",23));
        stus.add(new Student("002","小飞侠","男",24));
        stus.add(new Student("003","马奎斯","男",25));
        //保存到request域中
        req.setAttribute("students",stus);
        req.setAttribute("status",1);
        //转发到Jstl-if.jsp页面中
        req.getRequestDispatcher("/Jstl-if.jsp").forward(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
        this.doGet(req, resp);
    }
}


<%--
  Created by IntelliJ IDEA.
  User: 86166
  Date: 2022/6/14
  Time: 11:27
  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" %>
<%@page isELIgnored="false" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<c:if test="${status==1}">
    <h1>访问成功!</h1>
</c:if>
</body>
</html>

运行结果: img

<c:forEach>标签

items:遍历的容器 var:遍历产生的临时变量 varStatus:遍历状态对象 img 案例:

package com.study.Servlet;
import com.study.pojo.Student;
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.ArrayList;
import java.util.List;
@WebServlet("/el")
public class ElTestServlet extends HttpServlet { 
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
        //准备数据
        List<Student> stus=new ArrayList<>();
        stus.add(new Student("001","小马哥","男",23));
        stus.add(new Student("002","小飞侠","男",24));
        stus.add(new Student("003","马奎斯","男",25));
        //保存到request域中
        req.setAttribute("students",stus);
        //转发到forEach.jsp页面中
        req.getRequestDispatcher("/forEach.jsp").forward(req,resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
        this.doGet(req, resp);
    }
}

<%@ 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>
<table border="1" bgcolor="#00ced1">
    <tr>
        <td>学号</td>
        <td>姓名</td>
        <td>性别</td>
        <td>年龄</td>
    </tr>
    <c:forEach items="${students}" var="stu" varStatus="stuID">
        <tr>
            <td>${ stu.id}</td>
            <%--<td>${ stuID.index}</td> //学号就变成了0,1,2--%>
            <td>${ stu.name}</td>
            <td>${ stu.gender}</td>
            <td>${ stu.age}</td>
        </tr>

    </c:forEach>


</table>

</body>
</html>

运行结果: img

本文为互联网自动采集或经作者授权后发布,本文观点不代表立场,若侵权下架请联系我们删帖处理!文章出自:https://blog.csdn.net/qq_43514330/article/details/125276124
-- 展开阅读全文 --
安全面试之XSS(跨站脚本攻击)
« 上一篇 07-24

发表评论

成为第一个评论的人

热门文章

标签TAG

最近回复