设为首页 加入收藏

TOP

Java后端02(jsp)(一)
2023-08-06 07:49:51 】 浏览:65
Tags:Java 后端 jsp

jsp

? servlet 是无法将后端获取的数据传递给html 页面的,无法再servlet 中通过转发或者是重定向的方式,给html 页面传递响应的后端数据,servlet 中由于拼接过于繁琐,是不适合写html 的因此引入了 jsp ,既可以编写 html标签,也可以写 Java 代码,

<dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.3.3</version>
</dependency>

jsp的本质是Servlet

  1. 静态资源 html css js 图片
  2. html标签
  3. <%@ %> 指令
  4. <% %> 可以在这个标签中书写Java代码
  5. <%= %> 用于输出一段内容呈现在html中
  6. <%-- --%> jsp中的注释,这个注释和html的注释不一样,这个注释相当于是生成在jsp专业的servlet中,最终的页面是看不到这个注释的
  7. <jsp:include page="xx"> 用于做页面引用和包含,类似于html 的iframe 标签

页面包含

<jsp:include page="被包含的文件名"></jsp:include>
  1. cookie 由服务器创建由浏览器保存,服务器通过响应给客户端发送 cookie(一般不会以明文形式出现)
// 通过servlet实现cookie
@WebServlet("/testCookie")
public class TestCookieServlet extends HttpServlet {
    @Override
    protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 实例化一个 cookie 对象
        Cookie c = new Cookie("jerk","wyf");
        // 设置 cookie 的有效期(单位秒)
        c.setMaxAge(60*24*60);
        // 设置 cookie 的触发路径,当设置为 / 的时候,浏览器向服务器发送的所有请求都会携带这个 cookie
        c.setPath("/");
        // 将 cookie 数据添加到响应中
        resp.addCookie(c);
    }
}

// 通过servlet读取cookie
@WebServlet("/getCookie")
public class GetCookieServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 读 cookie
        Cookie[] cookies = req.getCookies();
        if(null != cookies){
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + " : " + cookie.getValue());
            }
        }
    }
}
  1. 由于cookie 的安全性不高,因此引入了 session

?session 代表着单服务器会话,从用户打开一个网页开始,无论在这个网页中访问了多少页面,点击了多少链接,都属于同一个会话,知道用户关闭浏览器为止,session 是由 tomcat 自己创建,开发者只是获取了 tomcat 创建的 session,在jsp中,session 是内置对象,并不需要定义,就可以直接使用,如果是在 servlet 中 需要使用 HttpSession session = req.getSession() 来获取,但是 jsp 并不需要 session 对象本身可以用俩存储数据,用于跨页面的信息传递

<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" isELIgnored="false" %>
<html>
	<head>
    	<title>Title</title>
	</head>
    <body>
    <%
        session.setAttribute("session01","robot01");
    %>
    <a href="getSession.jsp">点击跳转到获取信息的jsp</a>
    </body>
</html>

四个作用域对象

  1. pageContext 页面对象,用于在当前页面进行传值
  2. request 请求对象,用于在一次请求中传值
  3. session 会话对象,用于在同一个会话中做数据传递
  4. application 全局对象,所有用户共享的对象
pageContext 传参:
    <%pageContext.setAttribute("direct","robot01");%>
    <%=pageContext.getAttribute("direct")%>
request 传参:
	<%request.setAttribute("student",student);%>
	<%Student student = (Student) request.getAttribute("student");%>
session 传参:
	<%session.setAttribute("robotName","robot01");%>
	<%=session.getAttribute("robotName")%>
application 传参:

九大内置对象

在jsp中不需要定义就可以直接使用的对象

  1. request 请求
  2. response 响应
  3. out 输出
  4. pageContext 代表页面作用域对象
  5. session 会话对象
  6. application 全局对象
  7. page 等价于当前 jsp 转译的 servlet类 的实例--this
  8. config 用于获取web.xml中初始化参数的,一般不用
  9. exception 用于做jsp页面的异常处理

j

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇quarkus依赖注入之五:拦截器(Int.. 下一篇Sprint Boot学习路线4

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目