好久没有用JSP了,今天复习一下:
jsp是解释成servlet执行的.
新建一个jsp文件,里面仅输入一行文字redirct.
在tomcat/work/Catalina/localhost/JspTutorial/org/apache/jsp里面有一个redirct_jsp.java文件.
打开后可发现有如下代码:
JspFactory _jspxFactory = null;
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
_jspxFactory = JspFactory.getDefaultFactory();
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;
out.write("redirct");
因为类继承了httpservlet所以有了两个对象HttpServletRequest,HttpServletResponse
另外JSP里面定义了几个内置对象(见上面代码):pageContext,session,application,config,out,page
如果你在代码块里面用System.out.println( "Evaluating date now" );语句,运行jsp将发现其不会在页面里显示文字而是将文字显示到了服务器的log里.
如果将将文字显示到页面上就可以用到out
This variable does not need to be declared. It is already predefined for scriptlets, along with some other variables.
Another very useful pre-defined variable is "request". It is of type javax.servlet.http.HttpServletRequest
A "request" in server-side processing refers to the transaction between a browser and the server. When someone clicks or enters a URL, the browser sends a "request" to the server for that URL, and shows the data returned. As a part of this "request", various data is available, including the file the browser wants from the server, and if the request is coming from pressing a SUBMIT button, the information the user has entered in the form fields.
The JSP "request" variable is used to obtain information from the request as sent by the browser. For instance, you can find out the name of the client's host (if available, otherwise the IP address will be returned.)
A similar variable is "response". This can be used to affect the response being sent to the browser. For instance, you can call response.sendRedirect( anotherUrl ); to send a response to the browser that it should load a different URL. This response will actualy go all the way to the browser. The browser will then send a different request, to "anotherUrl". This is a little different from some other JSP mechanisms we will come across, for including another page or forwarding the browser to another page.
jsp中引用java代码的方式有<%= %> <% %> <%@ characters %> <%! %>
page directive
This one is a "page directive". The page directive can contain the list of all imported packages. To import more than one item, separate the package names by commas, e.g.
<%@ page import="java.util.*,java.text.*" %>
include directive
The include directive is used to physically include the contents of another file.
The included file can be HTML or JSP or anything else
-- the result is as if the original JSP file actually contained the included text.
To see this directive in action, create a new JSP
<%! %>
里面可以放入jsp的声明
The JSP you write turns into a class definition. All the scriptlets you write are placed inside a single method of this class.
You can also add variable and method declarations to this class. You can then use these variables and methods from your scriptlets and expressions.
To add a declaration, you must use the <%! and %> sequences to enclose your declarations, as shown below.
<%@ page import="java.util.*" %> <HTML> <BODY> <%! Date theDate = new Date(); Date getDate() { System.out.println( "In getDate() method" ); return theDate; } %> Hello! The time is now <%= getDate() %> </BODY> </HTML>
The example has been created a little contrived, to show variable and method declarations.
Here we are declaring a Date variable theDate, and the method getDate. Both of these are available now in our scriptlets and expressions.
But this example no longer works! The date will be the same, no matter how often you reload the page.
This is because these are declarations, and will only be evaluated once when the page is loaded! (Just as if you were creating a class and had variable initialization declared in it.)
英文只能看懂不能写>>
以上的代码在浏览器里面有时间显示但是在刷新时不能象<%= new java.util.Date()%>一样实时更新.
public final class declaration_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
Date theDate = new Date();
Date getDate()
{
System.out.println( "In getDate() method" );
return theDate;
}
被声明成了全局变量及全局方法.因为声明只调用一次,所以就不能实时更新了.
如果加一个方法用于重新初始化theDate变量,每次显示时都调用一个就可以了.
<%@ page import="java.util.*" %>
<HTML>
<BODY>
<%!
Date theDate = new Date();
Date getDate()
{
System.out.println( "In getDate() method" );
computeDate();
return theDate;
}
void computeDate()
{
theDate = new Date();
}
%>
Hello! The time is now <%= getDate() %>
</BODY>
</HTML>
刷新看一个,时间可以变了.