jsp

1. Overview of JSP

Disadvantages of Servlet:
1. HTML tags appear in the form of strings, and it is very cumbersome to process HTTP response messages;
2. All text and HTML are hard-coded, and minor changes in the presentation layer need to be recompiled;
personal opinion: 1. It is difficult to observe the realization of complex pages;
                2. The entire interaction logic is complicated;
                3. Changes need to be recompiled.
The emergence of JSP (JavaServer Pages) solves the above two problems


Note : Jsp: Essentially a Servlet
Servlet/jsp container translates jsp into Servlet.

2. JSP annotation

JSP comment: <%-- -->
HTML comment: <!- --> : Send to browser for processing

3. JSP implicit object

request       javax.servlet.http.HttpServletRequest
response      javax.servlet.http.HttpDervletResponse
out           javax.sevlet.jsp.JspWriter
session       javax.servlet.http.Httpsession
application   javax.servlet.ServletContext
config
pageContext
page

exception

例:request:

<%

    String username=request.getParameter("username");

 %>

      out:

<%
   out.println("Buffer size:"+response.getBufferSize());
   out.println("Servlet name:"+session.getId());
   out.println("Serlvet info:"+application.getServerInfo());
%>

4. Instructions

page:
The <%@page attribute="value1" attribute="value2"%>
page directive is a little bit similar to the attribute setting value attribute="value"
page directive common attribute setting:
    import : define one or more import and The java type used
    session: A value of true will import session management. A
                   value of false will be the opposite
   . info: Returns the result of calling the getServletInfo method of the Servlet class generated by the container.
   isErrorPage: identifies this page as an error handling page
    contentType : defines the content type of the implicit object response of this page, the default text/html
    pageEncoding : defines the character encoding of this page, the default is: ISO-8859-1
   language: defines in this page The scripting language type
   isELIgnored: Whether to ignore EL expressions when configuring. EL:Expression Language
include command: Include the contents of other files into the current JSP page. A page can contain multiple include directives
<%@include file="*.jspf">

Application: divide the page into modular design

Example: import


5. Script elements

  Basic elements:
  <% %> Java code blocks start with <% and end with %>
  Expressions:

  <%=expression %>

   Expression example: <%=Calendar.getInstance().getTimeInMillis() %>

  Declaration:
  You can declare variables or methods that are used in JSP pages.

  <%!   %> 

   example:

<%!

  public String getTodayDate(){
     return new Date().toGMTString();
  }
   public void jspInit(){
System.out.println("nsjk");

   public void jspDestroy(){
  System.out.println("hcsn");
   }
%>注:禁用脚本元素:
<jsp-property-group>
    <url-pattren>*.jsp<url-pattern>
    <scripting-invalid>true</scripting-invalid>
</jsp-property-group>

6. Action

   useBean
     创建实例:
   <jsp:useBean id="name" class="*">
     实例赋值setProperty
   <jsp:setProperty name="*"  property="attributeName" value="SttributeValue">
     输出实例值
   <jsp:getProperty name=""   property="attributeName">
   include
   <jsp:include page="*.jsp">
      <jsp:param name="text" value="how are you?">
   </jsp:include>
include directive and include action:
Include directive: Resource introduction occurs when the page is converted (jsp--->servlet), that is, when the jsp container converts the page into a servlet.
The action of include: Resource introduction occurs when the page is requested.

Summary: The include action can pass parameters, but the include command cannot pass parameters.
When applying the include command to include a file, because the included file will eventually generate a file, the include file and the included file cannot have variables or methods with the same name.
When applying <jsp:include> include files, since each file is compiled separately, variable and method names do not conflict.

   forward: transfer the current page to another page
   <jsp:forward page="*.jsp">
       <jsp:param name="text" value="Please login"/>
   </jsp:forward>

7. Error handling

The page directive isErrorPage attribute
identifies a JSP page as an error handling page
<%@page isErrorPage="true"%>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324690842&siteId=291194637
jsp