JSP basics

JSP basics


Working principle
1. The JSP engine will compile the JSP file into a .class file, which is a servlet class, which contains some objects and variables needed in the servlet, as well as user data passed in as MAP parameters.
2. When requesting the JSP file, it refers to a jspservic method in the servlet class with the relevant user parameters as parameters to return the page content.



JSP Syntax
1. Script programs can contain any number of Java statements, variables, methods, or expressions, as long as they are valid in the scripting language.
Syntax format:
<% code fragment%>



JSP declaration (just defined, not output)
1. A declaration statement can declare one or more variables and methods for subsequent Java code use. In the JSP file, you must declare these variables and methods before you can use them.
The syntax format of JSP declaration:
<%! declaration; [ declaration; ]+ ... %> //One more "!"



JSP expression (the result becomes String and put here)
1. A JSP expression contains The scripting language expression is first converted to a String and then inserted where the expression appears.
2. Since the value of the expression will be converted into a String, you can use the expression in a text line regardless of whether it is an HTML tag or not.
3. The expression element can contain any expression that conforms to the Java language specification, but cannot use a semicolon to end the expression.
The syntax format of JSP expression:
<%= expression%>



JSP comment
<%-- Comment--%> JSP comment, the comment content will not be sent to the browser or even compiled <!-- Comment--> HTML comment, you can see the comment content JSP
when viewing the source code of the web page through the browser



Directives (which are performed when the servlet is generated)
JSP directives are used to set properties related to the entire JSP page. (It is the attribute of the relevant object in the servlet)
JSP directive syntax format:
<%@ directive attribute="value" %>

<%@ page ... %> Define the dependency attributes of the page, such as scripting language, error page, cache requirements etc.
<%@ include ... %> include other files
<%@ taglib ... %> import tag library definitions, which can be custom tags

http://www.runoob.com/jsp/jsp-directives. html



JSP behavior (on request)
JSP behavior tags use XML syntax structures to control the servlet engine.
A syntax format:
<jsp:action_name attribute="value" />


include directive, which is to import files when JSP files are converted into servlets, and the jsp:include action here is different, the time to insert files is when the page is when requested.
http://www.runoob.com/jsp/jsp-actions.html



JSP Implicit Objects
JSP supports nine automatically defined variables, which are called implicit objects. (It is the object included by default in the servlet)
request Instance of
HttpServletRequest class response Instance of HttpServletResponse class
out Instance of PrintWriter class, used to output the result to the web page Instance of
HttpSession class
application ServletContext instance, related to the application context
config ServletConfig Instance of the class pageContext An instance of
the PageContext class, which provides access to all objects and namespaces of the JSP page. The
page is similar to the this keyword in the Java class.
The object of the Exception class represents the corresponding exception object in the JSP page where the error occurred

http:/ /www.runoob.com/jsp/jsp-implicit-objects.html



JSP form processing
JSP reading form data
getParameter(): Use the request.getParameter() method to get the value of the form parameter.
getParameterValues(): Get data such as checkbox class (same name but multiple values). Receive array variables, such as checkbox type
getParameterNames(): This method can get the names of all variables, and this method returns an Emumeration.
getInputStream(): Call this method to read the binary data stream from the client.

<form action="main.jsp" method="POST/GET">
Site name: <input type="text" name="name">
<br />
URL: <input type="text" name="url " />
<input type="submit" value="submit" />
</form>



JSP filter (actually a java filter) The filters in
JSP and Servlet are all Java classes.
Filters can dynamically intercept requests and responses to transform or use the information contained in the request or response.
One or more filters can be attached to a servlet or a group of servlets. Filters can also be attached to JavaServer Pages (JSP) files and HTML pages.



JSP Cookie processing
JSP Cookie processing needs to encode and decode Chinese, the method is as follows:
String str = java.net.URLEncoder.encode("Chinese"); //Encoding
String str = java.net.URLDecoder.decode("



Cookie cookie = new Cookie("key","value");//cookie name and value as parameters//name and value cannot contain spaces or the following characters: [ ] ( ) = , " / ? @ : ;
cookie.setMaxAge(60*60*24);//Set the validity period
response.addCookie(cookie);// Send the cookie to the HTTP response header and

use JSP to read the Cookie
Cookie cookie = null;
   Cookie[] cookies = null;
   / / Get the data of cookies, which is an array
   cookies = request.getCookies();
   if( cookies != null ){
      out.println("<h2> Find Cookie name and value</h2>");
      for (int i = 0; i < cookies.length; i++){
         cookie = cookies[i];

         out.print("Parameter name: " + cookie.getName());
         out.print("<br>");
         out.print( "Parameter value: " + URLDecoder. decode(cookie.getValue(), "utf-8") +" <br>");
         out.print("------------------------------------------------<br>");
      }
  }


use JSP deletes Cookie
Cookie cookie = null;
   Cookie[] cookies = null;
   // Get cookies under the current domain name, which is an array
   cookies = request.getCookies();
   if( cookies != null ){
  out.println("<h2 > find cookie name and value</h2>");
      for (int i = 0; i < cookies.length; i++){
         cookie = cookies[i];
         if((cookie.getName( )).compareTo("name ") == 0 ){
            cookie.setMaxAge(0);
            response.addCookie(cookie);
            out.print("Delete Cookie: " +
            cookie.getName( ) + "<br/>");
         }
         out.print("参数名 : " + cookie.getName());
         out.print("<br>");
         out.print("参数值: " + URLDecoder.decode(cookie.getValue(), "utf-8") +" <br>");
         out.print("------------------------------------<br>");
      }
  }


JSP Session
if (session.isNew()){
      title = "访问菜鸟教程实例";
      session.setAttribute(userIDKey, userID);
      session.setAttribute(visitCountKey,  visitCount);
   } else {
   visitCount = (Integer)session.getAttribute(visitCountKey);
   visitCount += 1;
   userID = (String)session.getAttribute(userIDKey);
   session.setAttribute(visitCountKey,  visitCount);
   }



JSP page redirection
public void response.sendRedirect(String location)
throws IOException

You can also use setStatus() and setHeader() methods to get the same effect:
....
String site = "http://www.runoob.com " ;
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", site);
....



JSP auto-refresh
JSP provides a mechanism to make this easy, it can auto-refresh periodically page.
public void setIntHeader(String header, int headerValue)

<%
   // Set to refresh every 5 seconds
   response.setIntHeader("Refresh", 5);
   // Get current time
   Calendar calendar = new GregorianCalendar();
   String am_pm;
   int hour = calendar.get(Calendar.HOUR);
   int minute = calendar.get(Calendar.MINUTE);
   int second = calendar.get(Calendar.SECOND);
   if(calendar.get(Calendar.AM_PM) == 0)
      am_pm = "AM";
   else
      am_pm = "PM" ;
   String CT = hour+":"+ minute +":"+ second +" "+ am_pm;
   out.println("The current time is: " + CT + "\n");
%>


JSP send mail



JSP standard tag library (JSTL) (that is, a library that replaces code with tags, which is convenient to use)



Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326685556&siteId=291194637
jsp