ª 事例:
<%@ page language=”java” contentType=”text/html”%>
<%@ page import=”java.util.Date”%>
<%!
Private String getGreeting()
{
Date now = new Date();
String greeting = null;
If( now.getHours()<12)
{
greeting = “Good morning”;
}
else if(now.getHours()<18)
{
gretting = “Good day”;
}
else
{
greeting = “good evening”;
}
Return greeting;
}
%>
<html>
<head>
<title>All Scripting Elements</title>
</head>
<body bgcolor=”white”>
<%=ge tGreeting()%>
<%if (request.g etParameter(“name”) = = null) {%>
Stranger!
<%} else {%>
Partner
<%}%>
How are you?
</body>
</html>
ª 转化后的servlet
import java.util.Date;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public class allinone$jsp extends HttpJspBase
{
//声明元素原封不动的插入到了类的顶级
//声明元素中的方法没有访问JSP隐式变量的权限,如果要访问,必须作为参数传给该方法
Private String getGreeting()
{
Date now = new Date();
String greeting = null;
If( now.getHours()<12)
{
greeting = “Good morning”;
}
else if(now.getHours()<18)
{
gretting = “Good day”;
}
else
{
greeting = “good evening”;
}
return greeting;
}
public void _jspService(HttpServletRequest request,
HttpServletResponse response)
throws java.io.IOException,ServletException
{
//JSP隐含变量都是局部变量
JspFactory _jspxFactory = null;
PageCOntext pageCOntext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
String _value = null;
try
{
_jspFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html");
pageContext =
_jspxFactory.getPageContext(this,request,response,
"",true,8129,true);
application =pageContext.getServletContext();
config = pageContext.getServletConfig()l
session =pageContext.getSession();
out = pageContext.getOut();
out.write("\r\n");
out.write("\r\n");
out.write("\r\n<html>\r\n<head>\r\n");
out.write("<title>all scription Elements<title>\r\n");
out.write("<\head>\r\n<body bgcolor=\"white\">\r\n");
//表达式元素也原封不动的插入,所以不应该在JSP表达式末尾使用分号的原因
out.print(getGreeting());
out.write("\r\n");
if(request.getParameter("name") ==null)
{
out.write("\r\n stranger! \r\n");
}
else
{
out.write("\r\n partner! \r\n");
}
out.write("\r\n How are you? \r\n");
out.write("</body>\r\n</html>\r\n");
}
catch(Throwable t)
{
if(out !== null & out.getBufferSize() !=0)
{
out.clearBuffer();
}
if(pageContext != null)
{
pageContext.handlePageException(t);
}
}
finally
{
if(_jspxFactory != null)
_jspxFactory.releasePageCOntext(pageContext);
}
}
}