1.所有的类都放在包里
<%
SomeHelperClass test = new SomeHelperClass(...);
String someString = SomeUtilityClass.someStaticMethod(...);
%>
系统会认为SomeHelperClass 和SomeUtilityClass在哪个包中呢?
由JSP页面转换而成的servlet所在的包,可是JSP规范并没有将这个包标准化,因此没人知道这个包在哪。因此最好把类都放在包里,把编译好的class文件放到web应用的ROOT\WEB-INF\classes.
<%
SomeHelperClass test = new SomeHelperClass(...);
String someString = coreservlets.SomeUtilityClass.someStaticMethod(...);
%>
2.JSP/Servlet的对应
<H2>foo</H2>
<%!
Private String randomHeading(){
Return(<H2> + Math.random() + </H2>); // 只用于定义字段和方法
}
%>
<%= bar() %>
<% bar(); %>
生成的Servlet
public class xxxx implements HttpJspPage{
private String randomHeading(){
return(<H2> + Math.random() + </H2>);
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException{
Response.setContentType(text/html);
HttpSession session = request.getSession();
JspWriter out = response.getWriter();
out.println(<H2>foo</H2>);
out.println(bar());
bar();
}
}
3._jspService自动定义的8个局部变量
request - HttpServletRequest
response - HttpServletResponse
out - JspWriter (对输出进行缓存,通过page指令的buffer属性调整缓冲区的大小)
session - HttpSession
application - ServletContext,由Web应用中所有的servlet和JSP页面共享
config - ServletConfig,可以使用它来读取初始化参数,不过实际中,初始化参数在jspInit中读取,而非_jspService
pageContext - 存储的是与当前页面相关的值
page - this的同义词,为了在脚本语言中作占位符