jsp隐式对象都包括什么?

编写jsp时,Sun公司提供了便利,request、response、out、session、application、config、pageContext(代表本页)可以直接用,叫做隐式对象。想想jsp会被转成一个Servlet,这些对象就自然会用了。
马克- to-win:马克 java社区:防盗版实名手机尾号: 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编写过程可以用所有隐式对象,自己写的类和jsp里面自己写的方法。




例 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不能用,因为不在service里面*/
//       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>



输出结果:
8192
null
abc is null
xyz
hello
3
hello again




转成的文件是:



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不能用,因为不在service里面*/
//       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只在这一页时共享 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>");
    }
  }
}