美文网首页
JAVA Web学习(9)___第6章 JSP内置对象

JAVA Web学习(9)___第6章 JSP内置对象

作者: 岁月静好浅笑安然 | 来源:发表于2019-08-17 10:55 被阅读0次

    6.7 其他内置对象

    6.7.1 获取会话范围的pageContext对象

    方法 说明
    forward(String arg0) 把页面转发到另一个页面
    getAttribute(String arg0) 获取参数值
    getAttributeNamesInScope(int arg0) 获取某范围的参数名称的集合,返回Enuneration
    getException() 返回exception对象
    getRequest() 返回request对象
    getResponse() 返回response
    getSession() 返回session对象
    getOut() 返回out对象
    setAttribute(String arg0, String arg1) 设置属性值
    removeAttribute(String arg0,int arg1) 删除指定范围内的指定属性

    6.7.2 读取web.xml配置信息的config对象

    方法 说明
    getServletName() 获取servlet服务器名
    getServletContext() 获取servlet上下文
    getInitParameterNames() 获取服务器所有初始参数名称,返回值为java.util.Enumeration对象
    getInitParameter(String arg0) 获取服务器中name参数的初始值

    6.7.3 应答或请求的page对象

    这个对象就是页面实例的引用。它可以被看做是整个JSP页面的代表。
    page 对象就是this对象的同义词。

    方法 说明
    getClass() 返回当前Object的类
    hashCode() 返回该Object的哈希代码
    toString() 转换字符串
    equals(Object arg0) 比较该对象和指定对象是否相等

    6.7.4 获取异常信息的exception对象

    exception 对象包装了从先前页面中抛出的异常信息。它通常被用来产生对出错条件的适当响应。

    6.8实践练习

    1.添加留言和显示留言


    微信截图_20190817104652.png 微信截图_20190817104709.png
    • message_book.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'message_book.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body>
       <form action="message_show.jsp" method="post" >
      <table width="500" height="300" align="center" border="1" bordercolor="#66B3FF">
      <tr>
      <td height="30" align="center">留言簿</td>
      </tr>
      <tr>
      <td height="200" align="center">
      <textarea rows="10" cols="60" name="message"></textarea>
      </td>
      </tr>
       <tr>
      <td height="30" align="center">
     <input value="提交" type="submit" />
      </td>
      </tr>
      </table>
       </form>
      </body>
    </html>
    
    
    • message_show.jsp
    <%@page import="java.net.URLEncoder"%>
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <%String messageStr=  request.getParameter("message") ;
    messageStr=new String(messageStr.getBytes("ISO-8859-1"),"UTF-8");
    %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'message_show.jsp' starting page</title>
        
        <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache">
        <meta http-equiv="expires" content="0">    
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="This is my page">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body>
        <form action="message_book.jsp" method="post" >
      <table width="500" height="300" align="center" border="1" bordercolor="#66B3FF">
      
      <tr>
      <td height="200" align="center">
      <%=messageStr %>
      </td>
      </tr>
       <tr>
      <td height="30" align="center">
     <input value="返回" type="submit" />
      </td>
      </tr>
      </table>
       </form>
      </body>
    </html>
    
    

    2.账号密码登录,错误将页面重定向到错误页面

    • signin.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>登录页面</title>
        
        
    
      </head>
      
      <body>
      <form action="signin_check.jsp" method="post">
       账号:<input name="username" type="text"/><br>
       密码:<input name="password" type="password"/><br>
       <input name="登录" type="submit"/><br>
       </form>
      </body>
    </html>
    
    
    • signin_check.jsp
    <%@page import="javax.xml.soap.Text"%>
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'signin_success.jsp' starting page</title>
        
        
    
      </head>
      
      <body>
       <%
       String  username= request.getParameter("username"); 
       String  password= request.getParameter("password");
       %>
       <%
      if(username.equals("admin")&&password.equals("123456")){
        out.print("账号、密码正确");
      }else{
        response.sendRedirect("signin_error.jsp");
      }
       %>
      </body>
    </html>
    
    
    • signin_error.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'signin_error.jsp' starting page</title>
       
      </head>
      
      <body>
       登录错误. <br>
       <%
      
            
       %>
      </body>
    </html>
    
    

    相关文章

      网友评论

          本文标题:JAVA Web学习(9)___第6章 JSP内置对象

          本文链接:https://www.haomeiwen.com/subject/mxuhsctx.html