2014年6月5日 星期四

java-variable java-global-variable , jsp-global-variable,jsp-variable

inf: http://www.easywayserver.com/blog/global-variable-in-jsp/



Global variable in JSP

Global variable are variable which can access in whole JSP class in any method declared in class.
Global variable in JSP should be defined in declaration part <%! %>. Code inside this declaration can be accessed from any method e.g. jspInit(), jspDestroy(), and jspService().
jsp file
<%@ page language="java" %>
<%!
 String sGlobalVariable="defining variable here and access anywhere in JSP";
 
 public void getStringValue()
 {
 
 }
%>
<html>
<head>
<title>Global variable in JSP</title>
</head>

<body>
<%=sGlobalVariable%>
</body>
</html>
When execute on web browser, it will create jsp to servlet and see the sGlobalVariable is not in service method of JSP, this means you can access this variable in any method in JSP
public final class global_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {


 String sGlobalVariable="defining variable here and access anywhere in JSP";

 public void getStringValue()
 {

 }

  public void _jspInit() {
  }

  public void _jspDestroy() {
  }

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

  }
}

沒有留言:

張貼留言