• jsp2



    JSP内置对象
    1:内置对象简介
      JSP内置对象是WEB容器创建的一组对象,不是用new 关键就可以使用的
    2:四种作用域范围
    3:out
    4:request/response
    5:seession
    6:applicaton
    7:其他

        
    JSP内置对象是WEB容器中创建的一组对象,可以直接使用不需要new,如截图中的out 对象



    五大常用对象: out、request、response、session、application<br>
    其它四个不常用对象:page、pageContext、exception、config
    1. <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>
    2. <%
    3. String path = request.getContextPath();
    4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    5. %>
    6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    7. <html>
    8. <head>
    9. <base href="<%=basePath%>">
    10. <title>My JSP 'MyJsp.jsp' starting page</title>
    11. <meta http-equiv="pragma" content="no-cache">
    12. <meta http-equiv="cache-control" content="no-cache">
    13. <meta http-equiv="expires" content="0">
    14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    15. <meta http-equiv="description" content="This is my page">
    16. <!--
    17. <link rel="stylesheet" type="text/css" href="styles.css">
    18. -->
    19. </head>
    20. <body>
    21.  <h1>out内置对象</h1>
          <%
          //clear()清除缓冲区
          out.println("<h2>静夜思</h2>");
          out.println("床前明月光<br>");
          out.println("床前明月光<br>");
          out.flush();//没任何区别输出,缓冲区变大,调用clear()的话,会打印前几句,false
          //clearBuffer() 不会抛出异常
          out.println("床前明月光<br>");
          out.println("床前明月光<br>");
          %>
          缓冲区大小:<%=out.getBufferSize() %>byte<br>
          缓冲区剩余大小:<%=out.getRemaining() %>byte<br>
        是否清空缓冲区:<%=out.isAutoFlush() %><br>
        </body>
    22. </html>

    1. <%@ page language="java" import="java.util.*"
    2. contentType="text/html; charset=UTF-8"%>
    3. <%
    4. String path = request.getContextPath();
    5. String basePath = request.getScheme() + "://"
    6. + request.getServerName() + ":" + request.getServerPort()
    7. + path + "/";
    8. %>
    9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    10. <html>
    11. <head>
    12. <base href="<%=basePath%>">
    13. <title>My JSP 'login.jsp' starting page</title>
    14. <meta http-equiv="pragma" content="no-cache">
    15. <meta http-equiv="cache-control" content="no-cache">
    16. <meta http-equiv="expires" content="0">
    17. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18. <meta http-equiv="description" content="This is my page">
    19. <!--
    20. <link rel="stylesheet" type="text/css" href="styles.css">
    21. -->
    22. </head>
    23. <body>
    24. <h1>用户登录</h1>
    25. <hr>
    26. <from action="dologin.jsp" name="loginForm" method="get"><%--post --%>
    27. <table>
    28. <tr>
    29. <td>用户名:</td>
    30. <td><input type="text" name="username"></td>
    31. </tr>
    32. <tr>
    33. <td>密码:</td>
    34. <td><input type="password" name="password"></td>
    35. </tr>
    36. <tr>
    37. <td colspan="2"><input type="submit" value="登陆"></td>
    38. </tr>
    39. </table>
    40. </from>
    41. </body>
    42. </html>

    1. <body>
    2. <h1>登陆成功</h1>
    3. <hr>
    4. </body>


    1. <%@ page language="java" import="java.util.*"
    2. contentType="text/html; charset=UTF-8"%>
    3. <%
    4. String path = request.getContextPath();
    5. String basePath = request.getScheme() + "://"
    6. + request.getServerName() + ":" + request.getServerPort()
    7. + path + "/";
    8. %>
    9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    10. <html>
    11. <head>
    12. <base href="<%=basePath%>">
    13. <title>My JSP 'req.jsp' starting page</title>
    14. <meta http-equiv="pragma" content="no-cache">
    15. <meta http-equiv="cache-control" content="no-cache">
    16. <meta http-equiv="expires" content="0">
    17. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    18. <meta http-equiv="description" content="This is my page">
    19. <!--
    20. <link rel="stylesheet" type="text/css" href="styles.css">
    21. -->
    22. </head>
    23. <body>
    24. <h1>用户注册</h1>
    25. <hr>
    26. <form name="regForm" action="request.jsp" method="post">
    27. <table>
    28. <tr>
    29. <td>用户名:</td>
    30. <td><input type="text" name="username"></td>
    31. </tr>
    32. <tr>
    33. <td>爱好:</td>
    34. <td><input type="checkbox" name="favorite" value="read">读书
    35. <input type="checkbox" name="favorite" value="music">音乐 <input
    36. type="checkbox" name="favorite" value="movie">电影 <input
    37. type="checkbox" name="favorite" value="internet">上网</td>
    38. </tr>
    39. <tr>
    40. <td colspan="2"><input type="submit" value="登陆"></td>
    41. </tr>
    42. </table>
    43. </form>
    44. <br>
    45. <a href="request.jsp?username=lisi"> 测试url传参</a> <!-- 中文“李四”会出问题,通过tomcat-conf-server-Connector标签+URLEncoding="utf-8"-重启tomcat -->
    46. </body>
    47. </html>
    1. <body>
    2. <h1>requst内置对象</h1>
    3. <%
    4. request.setCharacterEncoding("utf-8");//可以提交汉字
    5. request.setAttribute("password", "123456");
    6. %>
    7. 用户名:<%=request.getParameter("username")%><br> 爱好:<%
    8. if (request.getParameterValues("favorite") != null) {
    9. String[] favorites = request.getParameterValues("favorite");
    10. for (int i = 0; i < favorites.length; i++) {
    11. out.println(favorites[i] + "&nbsp;");
    12. }
    13. }
    14. %><br> 密码:<%=request.getAttribute("password")%><br>
    15. 请求体的MIME类型:<%=request.getContentType()%><br> 协议类型及版本号:<%=request.getProtocol()%><br>
    16. 服务器主机名:<%=request.getServerName()%><br> 服务器端口号:<%=request.getServerPort()%><br>
    17. 请求文件的长度(字节):<%=request.getContentLength()%><br> 请求客户端的ip地址:<%=request.getRemoteAddr()%><br>
    18. 请求真实路径 :<%=request.getRealPath("request.jsp")%><br> 请求上下文路径:<%=request.getContextPath()%><br>
    19. </body>
    response对象
    -
    1. <%@page import="java.io.PrintWriter"%>
    2. <%@ page language="java" import="java.util.*"
    3. contentType="text/html; charset=UTF-8"%>
    4. <%
    5. response.setContentType("text/html; charset=UTF-8");//设置的响应的MIMI类型
    6. out.println("<h1>reponse内置对象</h1>");
    7. out.println("<br>tttt");
    8. PrintWriter out = response.getWriter();//获取输出流对象
    9. outer.println("大家好,我是response输出流");//输出 提前与内置out,用.fluse(清缓存)可以将out提前
    10. response.sendRedirect("reg.jsp");//请求重定向(直接跳转)
    1. %>
    -请求重定向
      客户端行为-----response.sendRedirect(),两次请求,地址栏地址改变
    -请求转发
      服务器行为-----request.getRequestDispatcher().forward(req,resp);地址不改变
    1. <body>
    2. <h1>用户注册</h1>
    3. <hr>
    4. <form name="regForm" action="response.jsp" method="post">
    5. <-- action="response.jsp"-->
    6. <table>
    7. <tr>
    8. <td>用户名:</td>
    9. <td><input type="text" name="username"></td>
    10. </tr>
    11. <tr>
    12. <td>爱好:</td>
    13. <td><input type="checkbox" name="favorite" value="read">读书
    14. <input type="checkbox" name="favorite" value="music">音乐 <input
    15. type="checkbox" name="favorite" value="movie">电影 <input
    16. type="checkbox" name="favorite" value="internet">上网</td>
    17. </tr>
    18. <tr>
    19. <td colspan="2"><input type="submit" value="登陆"></td>
    20. </tr>
    21. </table>
    22. </form>
    23. <br>
    24. <a href="request.jsp?username=lisi"> 测试url传参</a>
    25. <!-- 中文“李四”会出问题,通过tomcat-conf-server-Connector标签+URLEncoding="utf-8"-重启tomcat -->
    26. </body>

    1. <%@page import="java.io.*,java.util.*"%>
    2. <%@ page language="java" import="java.util.*"
    3. contentType="text/html; charset=UTF-8"%>
    4. <%
    5. response.setContentType("text/html; charset=UTF-8");//设置的响应的MIMI类型
    6. out.println("<h1>reponse内置对象</h1>");
    7. out.println("<br>tttt");
    8. PrintWriter outer = response.getWriter();//获取输出流对象
    9. outer.println("大家好,我是response输出流");//输出 提前与内置out,用.fluse(清缓存)可以将out提前
    10. //response.sendRedirect("reg.jsp");//请求重定向(直接跳转)
    11. //请求重定向 response.sendRedirect("request.jsp");
    12. //请求转发
    13. request.getRequestDispatcher("request.jsp").forward(request, response);
    14. %>
    session
     客户端和服务器一次对话----用户浏览网站花费的时间
      在服务器内存中保存着不同用户session
                     内置对象
    1. <body>
    2. <h1>session内置对象</h1>
    3. <hr>
    4. <%
    5. SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    6. Date d=new Date(session.getCreationTime());/* 时间化 */
    7. session.setAttribute("username", "admin");
    8. %>
    9. Session创建时间:<%=sdf.format(d) %><br><!-- 直接session.getCreationTime()会出现一长串代码 -->
    10. Session的ID编号:<%=session.getId() %><br>
    11. 从Session获取用户名:<%=session.getAttribute("username") %><br>
    12. </body>
    1. <body>
    2. <h1>session内置对象1</h1>
    3. <hr>
    4. <%
    5. SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    6. Date d=new Date(session.getCreationTime());/* 时间化 */
    7. session.setAttribute("username", "admin");
    8. session.setAttribute("password", "123456");
    9. session.setAttribute("age", "18");
    10. //设置当前session最大生成期限单位是秒
    11. session.setMaxInactiveInterval(10);//10s
    12. %>
    13. Session创建时间:<%=sdf.format(d) %><br><!-- 直接session.getCreationTime()会出现一长串代码 -->
    14. Session的ID编号:<%=session.getId() %><br>
    15. 从Session获取用户名:<%=session.getAttribute("username") %><br>
    16. <a href="session2.jsp" target="_blank">用超链接跳转到Session2</a>
    17. </body>
    1. <body>
    2. <h1>session内置对象2</h1>
    3. <hr>
    4. <%
    5. // SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    6. // Date d=new Date(session.getCreationTime());/* 时间化 */
    7. // session.setAttribute("username", "admin");
    8. %>
    9. Session的ID编号:<%=session.getId()%><br> 从Session获取用户名:<%=session.getAttribute("username")%><br>
    10. session中保存的全部属性:<%
    11. String[] names = session.getValueNames();
    12. for (int i = 0; i < names.length; i++) {
    13. out.println(names[i] + "&nbsp;");
    14. }
    15. %>
    16. </body>
    session生命周期
    创建-活动-销毁
      session对象
    Tomcat默认超时30s
    .设置Session超时两种方式
     1.session.setMaxInactiveInterval(时间)//单位是秒
    2:在web.xml配置
     <session-confi> <session-timeout>10</session-timeout></session-confi>//单位分
    application
    实现用户间数据共享,可存放全局变量
    开始与服务器启动,终止服务器关闭
      public void setAttribute(String,Object)使用指定名称将对象绑定到对话
    public Object getAttribute(String)返回uzai次对话中指定名称绑定在一起的对象,没有返回null
    Enumeration getAttributeNames()返回可用属性名枚举
    String getServerInfo()返回JSP(SERVET)引擎名及版本号
    1. <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>
    2. <%
    3. String path = request.getContextPath();
    4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    5. %>
    6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    7. <html>
    8. <head>
    9. <base href="<%=basePath%>">
    10. <title>My JSP 'application.jsp' starting page</title>
    11. <meta http-equiv="pragma" content="no-cache">
    12. <meta http-equiv="cache-control" content="no-cache">
    13. <meta http-equiv="expires" content="0">
    14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    15. <meta http-equiv="description" content="This is my page">
    16. <!--
    17. <link rel="stylesheet" type="text/css" href="styles.css">
    18. -->
    19. </head>
    20. <body>
    21. <h1>application内置对象</h1>
    22. <hr>
    23. <%
    24. application.setAttribute("city", "北京");
    25. application.setAttribute("postcode", "10000");
    26. application.setAttribute("email", "lisi@126.com");
    27. %>
    28. 所在城市:<%=application.getAttribute("city") %><br>
    29. application所有属性:<%
    30. Enumeration attibute=application.getAttributeNames();
    31. while(attibute.hasMoreElements()){
    32. out.println(attibute.nextElement()+" ");
    33. }
    34. %><br>
    35. JSp(SERVET)引擎名及版本号:<%=application.getServerInfo() %>
    36. </body>
    37. </html>
    Page
    指向当前JSP页面本身,java.lang.Object
    1. <body>
    2. <h1>page内置对象</h1>
    3. <hr>
    4. 当前page页面对象的字符串描述:<%=page.toString() %>
    5. </body>
    PageContext
    提供对JSP页面内所有对象及名字空间的访问
    可以访问本页所在session---也可以application的某一属性
    1. <body>
    2. <h1>pageContext内置对象</h1>
    3. <hr>
    4. 用户名是:<%=pageContext.getSession().getAttribute("username") %><br>
    5. <%//跳转到注册页面
    6. //pageContext.forward("req.jsp");
    7. //包含include.jsp内容
    8. pageContext.include("include.jsp");
    9. %>
    10. </body>
    1. <%@page import="javax.enterprise.inject.New"%>
    2. <%@page import="java.text.*"%>
    3. <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>
    4. <%
    5. String path = request.getContextPath();
    6. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    7. Date date=new Date();
    8. SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日");
    9. String s=sdf.format(date);
    10. out.println(s+"<br>");
    11. %>

    Config
    在一个Servet初始化时,JSP引擎向它传递信息,
    Exception
    是一个异常对象,
    1. <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" errorPage="exception.jsp"%>
    2. <!-- errorPage="exception.jsp"将异常交给 exception.jsp处理 -->
    3. <%
    4. String path = request.getContextPath();
    5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    6. %>
    7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    8. <html>
    9. <head>
    10. <base href="<%=basePath%>">
    11. <title>My JSP 'exception.jsp' starting page</title>
    12. <meta http-equiv="pragma" content="no-cache">
    13. <meta http-equiv="cache-control" content="no-cache">
    14. <meta http-equiv="expires" content="0">
    15. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    16. <meta http-equiv="description" content="This is my page">
    17. <!--
    18. <link rel="stylesheet" type="text/css" href="styles.css">
    19. -->
    20. </head>
    21. <body>
    22. <h1>测试异常页面</h1>
    23. <%
    24. System.out.println(100/0);//抛出算数异常
    25. %>
    26. </body>
    27. </html>
    1. <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" isErrorPage="true"%>
    2. <%
    3. String path = request.getContextPath();
    4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    5. %>
    6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    7. <html>
    8. <head>
    9. <base href="<%=basePath%>">
    10. <title>My JSP 'excepton.jsp' starting page</title>
    11. <meta http-equiv="pragma" content="no-cache">
    12. <meta http-equiv="cache-control" content="no-cache">
    13. <meta http-equiv="expires" content="0">
    14. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    15. <meta http-equiv="description" content="This is my page">
    16. <!--
    17. <link rel="stylesheet" type="text/css" href="styles.css">
    18. -->
    19. </head>
    20. <body>
    21. <h1>exception内置对象</h1>
    22. 异常消息:<%=exception.getMessage() %><br>
    23. 异常字符串描述:<%=exception.toString() %>
    24. </body>
    25. </html>
    用户登录例子:用户名admin,登陆成功内部转发login_success.jspbong提示成功,失败则请求重定向login_failure.jsp
    indexs.jsp
    1. <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>
    2. <%
    3. String path = request.getContextPath();
    4. String basePath = request.getScheme() + "://"
    5. + request.getServerName() + ":" + request.getServerPort()
    6. + path + "/";
    7. %>
    8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    9. <html>
    10. <head>
    11. <base href="<%=basePath%>">
    12. <title>My JSP 'index.jsp' starting page</title>
    13. <meta http-equiv="pragma" content="no-cache">
    14. <meta http-equiv="cache-control" content="no-cache">
    15. <meta http-equiv="expires" content="0">
    16. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    17. <meta http-equiv="description" content="This is my page">
    18. <!--
    19. <link rel="stylesheet" type="text/css" href="styles.css">
    20. -->
    21. </head>
    22. <body>
    23. <div id="container">
    24. <div></div>
    25. <div id="box">
    26. <form action="doologin.jsp" method="post">
    27. <p class="main">
    28. <label>用户名:</label><input name="username" value="" />
    29. <label>密码:</label><input type="possword" name="password" value="" />
    30. </p>
    31. <p cclass="space">
    32. <input type="submit" value="登录" class="login" style="cursor:poin">
    33. </p>
    34. </form>
    35. </div>
    36. </div>
    37. </body>
    38. </html>
    doologin.jsp
    1. <%@ page language="java" import="java.util.*"
    2. contentType="text/html; charset=UTF-8"%>
    3. <%
    4. String path = request.getContextPath();
    5. String basePath = request.getScheme() + "://"
    6. + request.getServerName() + ":" + request.getServerPort()
    7. + path + "/";
    8. String username = "";
    9. String password = "";
    10. request.setCharacterEncoding("utf-8");//防止中文乱码
    11. username = request.getParameter("username");
    12. password = request.getParameter("password");
    13. if ("admin".equals(username) && "admin".equals(password)) {
    14. session.setAttribute("loginUser", username);
    15. request.getRequestDispatcher("login_success.jsp").forward(
    16. request, response);
    17. } else {
    18. response.sendRedirect("login_failure.jsp");
    19. }
    20. %>
    login_success.jsp
    1. <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>
    2. <%
    3. String path = request.getContextPath();
    4. String basePath = request.getScheme() + "://"
    5. + request.getServerName() + ":" + request.getServerPort()
    6. + path + "/";
    7. %>
    8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    9. <html>
    10. <head>
    11. <base href="<%=basePath%>">
    12. <title>My JSP 'index.jsp' starting page</title>
    13. <meta http-equiv="pragma" content="no-cache">
    14. <meta http-equiv="cache-control" content="no-cache">
    15. <meta http-equiv="expires" content="0">
    16. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    17. <meta http-equiv="description" content="This is my page">
    18. <!--
    19. <link rel="stylesheet" type="text/css" href="styles.css">
    20. -->
    21. </head>
    22. <body>
    23. <div id="container">
    24. <div></div>
    25. <div id="box">
    26. <%
    27. String loginUser="";
    28. if(session.getAttribute("loginUser")!=null){
    29. loginUser=session.getAttribute("loginUser").toString();
    30. }
    31. %>
    32. 欢迎你<font color="red"><%=loginUser %></font>,登陆成功
    33. </div>
    34. </div>
    35. </body>
    36. </html>
    login_failure.jsp
    1. <%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8"%>
    2. <%
    3. String path = request.getContextPath();
    4. String basePath = request.getScheme() + "://"
    5. + request.getServerName() + ":" + request.getServerPort()
    6. + path + "/";
    7. %>
    8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    9. <html>
    10. <head>
    11. <base href="<%=basePath%>">
    12. <title>My JSP 'index.jsp' starting page</title>
    13. <meta http-equiv="pragma" content="no-cache">
    14. <meta http-equiv="cache-control" content="no-cache">
    15. <meta http-equiv="expires" content="0">
    16. <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    17. <meta http-equiv="description" content="This is my page">
    18. <!--
    19. <link rel="stylesheet" type="text/css" href="styles.css">
    20. -->
    21. </head>
    22. <body>
    23. <div id="container">
    24. <div></div>
    25. <div id="box">
    26. 登陆失败;
    27. <a href="index.jsp">返回</a>
    28. </div>
    29. </div>
    30. </body>
    31. </html>





  • 相关阅读:
    第九章
    第十章
    第八章
    第七章
    第六章
    第五章
    第四章
    第三章
    第二章
    第一章
  • 原文地址:https://www.cnblogs.com/liuruimiku/p/5405315.html
Copyright © 2020-2023  润新知