jsp隱式對(duì)象都包括什么?

編寫(xiě)jsp時(shí),Sun公司提供了便利,request、response、out、session、application、config、pageContext(代表本頁(yè))可以直接用,叫做隱式對(duì)象。想想jsp會(huì)被轉(zhuǎn)成一個(gè)Servlet,這些對(duì)象就自然會(huì)用了。
馬克- to-win:馬克 java社區(qū):防盜版實(shí)名手機(jī)尾號(hào): 73203。



例 2.1

a.html:

<html>
<body bgcolor="red">
<form action="jsp1.jsp">
Customer Name: <input type=text name=text1>
<input type=submit value=Enter>
</form>
</body>
</html>


jsp1.jsp

<html>
<body bgcolor=green>
<%
String name=request.getParameter("text1");
%><font color=red>
<b>Welcome <%=name %>
</b>
</font>
</body>
</html>


jsp編寫(xiě)過(guò)程可以用所有隱式對(duì)象,自己寫(xiě)的類(lèi)和jsp里面自己寫(xiě)的方法。




例 2.2


package com;
public class DatabaseConn {
    public static void prin(){
        System.out.println("hello");
    }
}




<%@ page import="com.*,java.util.*" contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
jsp1
</title>
</head>
<body bgcolor="#ffffff">
<%!
    void pri()
    {
    /*下面的Session不能用,因?yàn)椴辉趕ervice里面*/
//       String count1=(String) session.getAttribute( "theNum" );
       System.out.println("hello again");
    }
%>

<%
   out.println(request.getHeader("User-Agent"));
   int n=response.getBufferSize();
   System.out.println(n);
   String count=(String) session.getAttribute( "theNum" );
   System.out.println(count);
   System.out.println("abc is "+config.getInitParameter("abc"));
   pageContext.setAttribute("abcd","xyz");
   System.out.println(pageContext.getAttribute("abcd"));
   DatabaseConn.prin();
   Date d=new Date();
   System.out.println(d.getMonth());
   pri();
%>

</body>
</html>



輸出結(jié)果:
8192
null
abc is null
xyz
hello
3
hello again




轉(zhuǎn)成的文件是:



package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import com.*;
import java.util.*;

public final class jsp1_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {


    void pri()
    {
    /*下面的Session不能用,因?yàn)椴辉趕ervice里面*/
//       String count1=(String) session.getAttribute( "theNum" );
       System.out.println("hello again");
    }



  public void _jspService(HttpServletRequest request, HttpServletResponse response)
        throws java.io.IOException, ServletException {

    PageContext pageContext = null;
    HttpSession session = null;
    ServletContext application = null;
    ServletConfig config = null;
    JspWriter out = null;

    try {
      response.setContentType("tex只在這一頁(yè)時(shí)共享 charset=GBK");
      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();

      out.write("\r\n");
      out.write("<html>\r\n");
      out.write("<head>\r\n");
      out.write("<title>\r\n");
      out.write("jsp1\r\n");
      out.write("</title>\r\n");
      out.write("</head>\r\n");
      out.write("<body bgcolor=\"#ffffff\">\r\n");
      out.write("\r\n");
      out.write("\r\n");
 
   out.println(request.getHeader("User-Agent"));
   int n=response.getBufferSize();
   System.out.println(n);
   String count=(String) session.getAttribute( "theNum" );
   System.out.println(count);
   System.out.println("abc is "+config.getInitParameter("abc"));
   pageContext.setAttribute("abcd","xyz");
   System.out.println(pageContext.getAttribute("abcd"));
   DatabaseConn.prin();
   Date d=new Date();
   System.out.println(d.getMonth());
   pri();

      out.write("\r\n");
      out.write("\r\n");
      out.write("</body>\r\n");
      out.write("</html>");
    }
  }
}