JSP hands-on notes (on)

JSP

JSP, a tool that can directly use Html code and then write java code in html

Learning website: how2java

(1) The execution process of JSP

Take the hello.jsp file as an example:

<%@page contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="java.util.*"%>
 
你好 JSP
 
<br>
 
<%=new Date().toLocaleString()%>
  1. Translate hello.jsp into hello_jsp.java; (hello_jsp.java is located in tomat\work\Catalina\_\org\apache\jsp)
  2. hello_jsp.java is a servlet, which compiles hello_jsp.java into hello_jsp.class;
  3. Execute hello_jsp to generate html
  4. Return the html response to the browser via the http protocol
  • The Java program translated into jsp file is a servlet

    Because this class inherits HttpJspBase, and HttpJspBase inherits the HttpServlet class, it can be said that the Java generated by jsp file translation is a servlet.
    Insert picture description here

Jsp execution process

(Two), JSP page elements

  1. Static content

    html, css, javascript, etc.

  2. instruction

    End with <%@Begin%>, such as <%@page import=“java.util.*”%>

  3. Expression <%=%>

    Output a piece of html

    <%=%>与<%out.println%><

    The two are equivalent, out is an implicit object of jsp and can be used directly. There are 9 types of implicit objects, please refer to the chapter on implicit objects.

    Note: <%=%> does not need to end with a semicolon, <%%> needs to end with a semicolon, just like java code

  4. Scriptlet

    Between <%%>, you can write any Java code

  5. statement

    Fields or methods can be declared between <%!%>, but this is not recommended

  6. action

    <jsp:include page=“Filename”>

    Include another page in the jsp page.

  7. Comment <%-- --%>

    Different from html comments, through jsp comments, the browser can't see the corresponding code, which is equivalent to commenting out in the servlet

JSP page elements

Jsp page elements

(Three), include in Jsp

The information part of the page that needs to be added to almost every page, such as the copyright notice information at the bottom of each page, can be included with include, which can avoid the workload of writing each page separately and modifying the public information page

  • Directive include

    <jsp:include file="footer.jsp"/>
    

    The content of footer.jsp in the instruction include will be inserted into the .Java translated by jsp, and only one Java file will be generated at the end. So the variables defined in hello.jsp can be accessed directly in footer.jsp

  • Action include

    <jsp:include page="footer.jsp"/>
    

    The content of footer.jsp in the action include will not be inserted into the .Java file translated by jsp, and a footer.java will exist independently. Each page containing footer.jsp accesses footer_jsp.java on the server, and then embeds the returned result in the response.

    The action include is an independent access to footer.jsp, which needs to pass parameters.

    <%! hello.jsp %>
    <%@page contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8" import="java.util.*"%>
      
    你好 JSP
    <%=new Date().toLocaleString()%>
     
    <jsp:include page="footer.jsp">
        <jsp:param  name="year" value="2017" />
    </jsp:include>
    
    <%! footer.jsp %>
    <hr>
        <p style="text-align:center">copyright@<%=request.getParameter("year")%>
    </p>
    

(Four), jump

​ Jump is divided into client-side jump and server-side jump

  • Client jump

    <%
        response.sendRedirect("hello.jsp");
    %>
    

    The client-side jump effect can be seen in the debugging tool of the browser. When accessing jump.jsp, it returns 302 (referring to temporary client-side jump), and then jumps to hello.jsp

  • Server jump

    <%
    	request.getRequestDispatcher("hello.jsp").forward(request, response);
    %>
    <%! 或者 %>
    <jsp:forward page="hello.jsp"/>
    

The difference between client-side jump and server-side jump:

Server jump
Client jump

(5), cookie

Cookie is a way for browser and server to exchange data

  1. Cookies are created by the server, but are not stored on the server
  2. After creation, it will be sent to the browser and saved in the user's local area.
  3. The next time you visit the website, the cookie will be sent to the server

setCookie.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="javax.servlet.http.Cookie"%>
 
<%
    Cookie c = new Cookie("name", "Gareen");
    c.setMaxAge(60 * 24 * 60);//cookie保留时间
    c.setPath("127.0.0.1");	//服务器主机名
    response.addCookie(c);	//通过response将这个cookie保存到浏览器端口
%>
 
<a href="getCookie.jsp">跳转到获取cookie的页面</a>

getCookie.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="javax.servlet.http.Cookie"%>
 
<%
    Cookie[] cookies = request.getCookies();
    if (null != cookies)
        for (int d = 0; d <= cookies.length - 1; d++) {
            out.print(cookies[d].getName() + ":" + cookies[d].getValue() + "<br>");
        }
%>

(6), Session

​ Session Session refers to the beginning of the user opening the browser to visit the website, no matter how many pages are visited on this website, how many links are clicked, they all belong to the same session, until the user closes the browser, this belongs to a session Session.

setSession.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="javax.servlet.http.Cookie"%>
 
<%
   session.setAttribute("name", "teemo");
%>
 
<a href="getSession.jsp">跳转到获取session的页面</a>

getSession.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="javax.servlet.http.Cookie"%>
 
<%
    String name = (String)session.getAttribute("name");
%>
 
session中的name: <%=name%>

Session validity period

​ After logging in to a website, you can continue to access it in a short time without having to log in again. However, if you do not log in for a long time, you will still be required to log in again after the validity period of the Session.

​ The validity period of the session is 30min by default in Tomcat.

It can be adjusted in the session-config configuration in tomcat/conf/web.xml


(7), JSP scope

Four scopes in Jsp:

  • pageContext current page
  • requestContext one request
  • sessionContext current session
  • applicationContext global, shared by all users

7.1 pageContext current page

The data through pageContext.setAttribute(key, value) can only be accessed on the current page and cannot be accessed on other pages.

7.2 requestContext one request

Indicates that as the request ends, the data in it will be recycled.

Commonly used writing is as follows:

request.setAttribute("name","gareen"); 
request.getAttribute("name")

You can also use pageContext to do it, but it is not commonly used, as follows:

 
pageContext.setAttribute("name","gareen",pageContext.REQUEST_SCOPE);
pageContext.getAttribute("name",pageContext.REQUEST_SCOPE)

(1) requestContext and server-side jump

RequestContext refers to a request.
If a server-side jump occurs, jump from setContext.jsp to getContext.jsp. In fact, this is still a request. So in getContext.jsp, you can get the value set in requestContext

This is also a way to pass data between pages

setContext.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    request.setAttribute("name","gareen");
%>
<jsp:forward page="getContext.jsp"/>

getContext.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 
<%=request.getAttribute("name")%>

(2) requestContext and client jump

When the client jumps, a new visit will occur to the browser, and the new visit will generate a new request object

Therefore, in the case of page jump, data cannot be passed through request .

setContext.jsp

<%@ page language="java" contentType="text/html;charset=UTF-8"
    pageEncoding="UTF-8" %>
<%
	request.setAttribute("name","gareen");
	response.sendRedirect("getContext.jsp");
%>

getContext.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 
<%=request.getAttribute("name")%>

7.3 sessionContext current session

​ Refers to the session. From the moment a user opens the website, no matter how many web pages are visited, the link belongs to the same session until the browser is closed.

​ Therefore, data transfer between pages can also be passed through Session. But the sessions corresponding to different users are different, so the session cannot share data between different users.

The usage of sessionContext is similar to requestContext:

  • setContext.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
    session.setAttribute("name","gareen");
    response.sendRedirect("getContext.jsp");
%>
  • getContext.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 
<%=session.getAttribute("name")%>

You can also use the following methods:

pageContext.setAttribute("name","gareen",pageContext.SESSION_SCOPE);
pageContext.getAttribute("name",pageContext.SESSION_SCOPE)

7.4 applicationContext global

All users share the same data.

Use application object in JSP, application object is an instance of ServletContext interface

It can be obtained by request.getSevletContext(); the application mapping is the web application itself.

So application == request.getServletContext() will return true

To be continued below

Jsp上手笔记(下)更新中,待续……

Guess you like

Origin blog.csdn.net/weixin_40849588/article/details/95678121