笔记:JSP

JSP简介
  1. JSP全称Java Server Pages,就是运行在java服务器中的页面,也就是JavaWeb中的动态页面,本质就是一个Servlet
  2. 其本身就是一个动态网页技术标准,主要构成有HTML网页代码、Java代码片段、JSP标签几部分组成,后缀是.jsp
  3. 一般我们将Servlet和JSP结合使用,Servlet负责业务,JSP负责显示
  4. 一般情况下,都是Servlet处理完的数据,转发到JSP,JSP负责显示工作

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

例如:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <%
    Date now = new Date();
  %>
  <h1>现在的时间是:<%= now %></h1>
  </body>
</html>

所获取的时间是动态的

JSP基本语法
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>语法</title>
</head>
<body>
<%--JSP脚本片段
作用:用来在里面写JAVA代码。
--%>
<% for (int i = 0; i < 100; i++) {
    //out.print("人生若只如初见,何事秋风悲画扇。");
  %>
<h1>人生若只如初见,何事秋风悲画扇。</h1>
<% }%>

<%--2.JSP表达式
    作用:用来向浏览器输出对象
--%>
<%="我是通过JSP表达式输出的"%>
</body>
</html>

JSP隐含对象

out:(JSPWriter):相当于respomse.getWriter()获取的对象,用于在页面中显示信息。
config:(ServletConfig):对应Servlet中的Servletconfig对象
page:(Object)对应当前Servlet对象,实际上就是this
pageContext:(PageContext)当前页的上下文,也是一个域对象
exception:(Throwable)错误页中异常对象
request:(HttpServletResquest)HttpServletResquest对象
response:(HttpServletResponse)HttpServletResponse对象
application:(ServletContext)ServletContext对象
session:(HttpSession)HttpSession对象
重点掌握四个域对象

四个域对象
<%--
  Created by IntelliJ IDEA.
  User: dell
  Date: 2020/10/8
  Time: 10:32
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>四个域对象</title>
</head>
<body>
<%--        四个域:
                page域 范围:当前页面
                    对应域对象:pageContext
                    域对象类型:pageContext
                requst域 范围:当前请求(一次请求)
                    对应域对象:request
                    域对象类型:HttpServletRequest
                session域 范围:当前会话(一次会话)
                    对应域对象:session
                    域对象类型:HttpSession
                application域 范围:当前WEB应用
                    对应域对象:application
                    域对象类型:ServletContext

                四个域对象都有以下三个方法
                    void setAttrinute(String key , Object value):
                    Object getAttrinute(String key)
                    void removeAttrinute(String key)

                四个域对象的使用规则:能用小的不用大的
                --%>
        <%--在当前页面添加四个属性--%>
        <%
        pageContext.setAttribute("pageKey","pageValue");
        request.setAttribute("requestKey","requestValue");
        session.setAttribute("sessionKey","sessionValue");
        application.setAttribute("applicationKey","applicationValue");
        %>
        <h1>在当前页面获取四个域的属性值</h1>
        page域中属性值为<%=pageContext.getAttribute("pageKey")%><br>
        request域中属性值为<%=request.getAttribute("requestKey")%><br>
        session域中属性值为<%=session.getAttribute("sessionKey")%><br>
        application域中属性值为<%=application.getAttribute("applicationKey")%><br>
</body>
</html>

因此,可以对之前的小案例进行改动

package servlet;

import bean.User;
import dao.UserDAOImpl;

import javax.servlet.RequestDispatcher;
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.sql.SQLException;

/*
* 处理用户登录的Servlet
*
* */
@WebServlet(name = "MyServlet")
public class MyServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("调用post");
        // 获取用户名密码
        String username = request.getParameter("username");
        String userpassword = request.getParameter("userpassword");
        UserDAOImpl userDAO = new UserDAOImpl();
        System.out.println(username);
        System.out.println(userpassword);
        // 调用UserDAOImpl中的验证方法
        try {
            boolean b = userDAO.checkUsernameAndPassword(username, userpassword);
            //用户名和密码正确(建议成功重定向,失败转发)
            if (b) {
                response.sendRedirect("pages/successful.jsp");
            } else {
                // 用户名密码不正确(转发到登录页面)
                request.setAttribute("msg","用户名或密码错误");
                RequestDispatcher requestDispatcher = request.getRequestDispatcher("index.jsp");
                // 请求转发
                requestDispatcher.forward(request,response);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

<%--
  Created by IntelliJ IDEA.
  User: dell
  Date: 2020/10/5
  Time: 15:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>登陆界面</title>
  <style type="text/css">
    body{
      background-color: cornflowerblue;
    }
  </style>
</head>
<body>
<h1>欢迎登陆</h1>
<form action="MyServlet" method="post">
  用户名称:<input type="text" name="username"><span style="color:red"><%=request.getAttribute("msg") == null ? " ":request.getAttribute("msg") %></span> <br>
  用户密码:<input type="password" name="userpassword"><br>
  <input type="submit" value="提交">
</form>
</body>
</html>

这样当输入错误时,可以生成一句提示

EL

1.EL是JSP内置的表达式语言,用以访问页面的上下文以及不同作用域中的对象,取得对象属性的值,或执行简单的运算或判断操作,EL在得到某个数据时,会自动进行数据类型的转换
2.EL表达式用于代替JSP表达式(<%=%>)在页面中做输出操作
3.EL表达式仅仅用来读取数据,而不能对数据进行修改
4.使用EL表达式输出数据时,如果有则输出数据,如果为null则什么也不输出
5.EL表达式语法:

${EL表达式(可完成取值、简单判断、简单运算等)}

6.EL取值的四个域

<%@ page import="java.util.Date" %><%--
  Created by IntelliJ IDEA.
  User: dell
  Date: 2020/10/9
  Time: 8:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>EL</title>
</head>
<body>
<%--
            EL全称: Expression Language 表达式语言
            作用:主要用来输出域对象中属性值
            EL表达式查找规则:
                先从page开始查找,找到后直接返回,不再去其他域进行查找,如果找不到再去request域中查找,以此类推...
                如果在application域中仍找不到,则返回空串
            EL给我们提供了四个Scope对象,用来精确获取指定域中属性值
                pageScope
                    获取page域中属性值
                requestScope
                    获取request域中属性值
                sessionScope
                    获取session域中属性值
                applicationScope
                    获取aoolication域中属性值
--%>
    <%
        Date date = new Date();
        // 将当前时间放到page域中才能显示
        pageContext.setAttribute("time",date);
        // 获取一个实体类的属性,实际上是调用了它的get方法,但是不需要写get,直接写get方法后的字母(首字母小写)
    %>
    通过EL表达式显示 ${pageScope.time}

</body>
</html>

由此,可以将那个小案例再修改一下
不用三元表达式

<%--
  Created by IntelliJ IDEA.
  User: dell
  Date: 2020/10/5
  Time: 15:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>登陆界面</title>
  <style type="text/css">
    body{
      background-color: cornflowerblue;
    }
  </style>
</head>
<body>
<h1>欢迎登陆</h1>
<form action="MyServlet" method="post">
  用户名称:<input type="text" name="username"><span style="color:red">${requestScope.msg }</span> <br>
  用户密码:<input type="password" name="userpassword"><br>
  <input type="submit" value="提交">
</form>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值