ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java-JSP(1)

2022-08-31 00:02:14  阅读:235  来源: 互联网

标签:Java pageContext final JSP jsp servlet javax out


JSP

1.jsp原理

jsp全称JavaServer Pages,是一种动态网页技术,JSP将Java代码和特定变动内容嵌入到静态的页面中,实现以静态页面为模板,动态生成其中的部分内容。jsp文件在最后会会转变为servlet代码。
IDEA tomcat的工作空间

我们发现jsp页面转变成立java

浏览器向服务器发送请求,其实就是在访问servlet

//初始化
public void _jspInit() {
  }
//销毁
  public void _jspDestroy() {
  }
//jsp服务
  public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)

判断请求

内置对象

final javax.servlet.jsp.PageContext pageContext;//页面上下文javax.servlet.http.HttpSession session = null;//session
final javax.servlet.ServletContext application;//ServletContext application
final javax.servlet.ServletConfig config;//配置
javax.servlet.jsp.JspWriter out = null;//out
final java.lang.Object page = this;//当前页
final javax.servlet.http.HttpServletRequest request//请求
final javax.servlet.http.HttpServletResponse response///响应

输出页面前增加的代码

response.setContentType("text/html");
      pageContext = _jspxFactory.getPageContext(this, request, response,
      			null, true, 8192, true);
      _jspx_page_context = pageContext;
      application = pageContext.getServletContext();
      config = pageContext.getServletConfig();
      session = pageContext.getSession();
      out = pageContext.getOut();
      _jspx_out = out;

这些对象我们可以在jsp中直接使用
我们在<%%>中写Java代码

<%
    session.invalidate();
%>

源码

<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 30/8/2022
  Time: 下午7:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>hello</title>
</head>
<body>
<%
    String name="jinnice";
%>
name:<%=name%>
</body>
</html>

在jsp中
java代码原封不动输出
html代码就会用out.write

jsp基础语法和指令

我们可以创建普通项目,然后在项目上添加web支持
jsp是Java的一种技术应用也有属于自己的语法
我们可以通过热部署方式使用tomcat不用每次都重新启动,部署工件时用war exploded
jsp使用

<%--jsp变量或表达式
用来将程序输出到客户端
<%=变量或者表达式%>
--%>
  <%= new java.util.Date()%>
<%--jsp脚本片段--%>
  <%
    int sum=0;
    for(int i=0;i<=100;i++)
    {
      sum+=i;
    }
    out.print("<h1>sum="+sum+"<h1>");
  %>

jsp声明,在<%!>中可以写方法和全局变量

<%!
   public void say()
   {
     System.out.println("welcome");
   }
 %>

jsp声明会被编译到jsp生成的jsp的类里,其他的会到jspservice方法

jsp指令

定制错误页面

<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 30/8/2022
  Time: 下午11:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page errorPage="error/500.jsp" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    int i=1/0;
%>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 30/8/2022
  Time: 下午11:06
  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>
<img src="../img/河大景色.jpg">
</body>
</html>

在web.xml中定制错误页面

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <error-page>
        <error-code>500</error-code>
        <location>/error/500.jsp</location>
    </error-page>
</web-app>

标签:Java,pageContext,final,JSP,jsp,servlet,javax,out
来源: https://www.cnblogs.com/jinnice/p/16638549.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有