Foreword in modern web development, embedding Java script in JSP is not recommended, because it is not conducive to the maintenance of code. There are a number of good, alternative ways to avoid writing Java scripts in the JSP. This article is only an understanding of JSP system technology.
class member Definitions1. Put the defined code in the <%! Between%>. The members that are defined are converted to members of the Servlet class. 2. You can define any member method and member fields, either static members or instance members. 3, note that Jsp/servlet is working in a multi-threaded environment, the definition of member variables to pay attention to thread safety issues. It is generally recommended to use local variables in the _jspservice method. <%@ page contenttype= "text/html; Charset=utf-8 "pageencoding =" UTF-8 "trimdirectivewhitespaces =" true "session=" true "%><%!Public void Jspinit (){System.out.println ("JSP initialization work: Jspinit");} Public void Jspdestroy (){System.out.println ("JSP cleanup work: Jspdestroy");} private Final int a = +; %><! DOCTYPE Html>
Small Script Fragment1, the definition of small script using <%%>2, the Java code in the small script will be inserted into the _jspservice method, where it is defined, in order to insert there. <%@ page contenttype= "text/html; Charset=utf-8 "pageencoding =" UTF-8 "trimdirectivewhitespaces=" true "session =" true "%> <! DOCTYPE html><% int a = 100+10;out.print ("100+10=" +a + "<br/>");out.print ("La la la la la");%></body>
Expression Script1. <%= expression%>, note the end of the expression do not semicolon. 2. Used to get a string literal representation of a Java variable (object). The default is to call the object's ToString method to get <! DOCTYPE Html>
Java scripts in the JSP JSP