Nine built-in objects and scopes of jsp

1. pageContext saves data in only one page

<body>
<%
	// Set the data key - > value of two page ranges
	pageContext.setAttribute("name","page Wang Er Xiao");
	pageContext.setAttribute("age",12);
%>
<%
	// Value
	String name=(String)pageContext.getAttribute("name");
	int age=(Integer)pageContext.getAttribute("age");
%>
<font>Full name:<%=name %></font>
<font>Age:<%=age %></font>
</body>

2. The request scope only saves data in one request

<%
	// Set data key - > value of two request ranges
	request.setAttribute("name","request Wang Er Xiao");
	request.setAttribute("age",12);
%>
<jsp:forward page="requestTarget.jsp"></jsp:forward>

requestTarget,jsp

<%@ page import="java.util.*" %>
<body>
<%
	// Value
	String name=(String)request.getAttribute("name");
	int age=(Integer)request.getAttribute("age");
	// Get header information
	Enumeration enu=request.getHeaderNames();
	while(enu.hasMoreElements()){
		String headerName=(String)enu.nextElement();
		String headerValue=request.getHeader(headerName);
%>
	<h4><%=headerName %>&nbsp;<%=headerValue %></h4>
<%
	}
%>
<font>Full name:<%=name %></font>
<font>Age:<%=age %></font>
</body>

3. Session scope saves data in one session, and data is saved in the server. If the browser is not closed, it will be valid for half an hour by default. A single browser is valid

<body>
<%
	// Set the data key - > value of two session ranges
	session.setAttribute("name","session Wang Er Xiao");
	session.setAttribute("age",12);
%>
<h1>session Value setting completed!</h1>
</body>
<body>
<%
	// Value
	String name=(String)session.getAttribute("name");
	int age=(Integer)session.getAttribute("age");
%>
<font>Full name:<%=name %></font>
<font>Age:<%=age %></font>
</body>

4. Data is saved on the whole server within the application scope and shared by all users

<body>
<%
	// Set data key - > value of two application ranges
	application.setAttribute("name","application Wang Er Xiao");
	application.setAttribute("age",12);
%>
<h1>application Value setting completed!</h1>
</body>
<body>
<%
	// Value
	String name=(String)application.getAttribute("name");
	int age=(Integer)application.getAttribute("age");
%>
<font>Full name:<%=name %></font>
<font>Age:<%=age %></font>
</body>

Response object to send information to the client in response to the client's request

Continuous display of real-time time time

<body>
<%
	// Refresh the page every second
	response.setHeader("refresh","1");
	// Get current time
	Date myDate=new Date();
%>
//Current time: <% = mydate. Tolocalestring()% >
</body>

Page redirection

<body>
<%
	// Redirect, client jump
	response.sendRedirect("index.html");
%>
</body>

Operation cookie

cookie information exists in the client (insecure), session information exists in the server (secure)

out object

Like the client output various types of data, but also can manage the application server output buffer.

Stream flush flushes the cache, or something will be missing

<body>
<%
	out.println("<h1>");
	out.println("Hello Jsp Servlet");
	out.println("</h1>");
%>
</body>
<body>
<%
	int totalbuffer=out.getBufferSize();  // Get the total buffer size
	int available=out.getRemaining(); // Get the size of unused buffers
	int user=totalbuffer-available;  // Get the buffer size used
	out.println("Total buffer size:"+totalbuffer);
	out.println("Size of unused buffers:"+available);
	out.println("Buffer size used:"+user);
%>
</body>

config object

During servlet initialization, jsp engine passes information through config

The bottom layer of jsp is servlet

web.xml

Request init to jump to sysInit.jsp

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  	<servlet-name>init</servlet-name>
  	<jsp-file>/sysInit.jsp</jsp-file>
  	<init-param>
  		<param-name>jdbcName</param-name>
  		<param-value>com.mysql.jdbc.Driver</param-value>
  	</init-param>
  	<init-param>
  		<param-name>dbUrl</param-name>
  		<param-value>jdbc:mysql://localhost:3306/db_xx</param-value>
  	</init-param>
  </servlet>

sysInit.jsp

<body>
<%
	String jdbcName=config.getInitParameter("jdbcName");
	String dbUrl=config.getInitParameter("dbUrl");
%>
<h1>Driver name:<%=jdbcName %></h1>
<h1>Connection address:<%=dbUrl %></h1>
</body>

exception object

<%@ page errorPage="error.jsp"%>
<body>
<%
	int a=1;
	int b=0;
	out.println(a/b);
%>
</body>

error.jsp

<%@ page isErrorPage="true"%>
<body>
<%
	if(exception!=null){
		out.println("Program error message:");
		out.println(exception.getMessage());
	}
%>
</body>

pageContext object

Context object to set the value of the page range. Get request and session, application, page

<body>
<%
	pageContext.setAttribute("name0", "pageInfo");
	request.setAttribute("name1", "requestInfo");
	session.setAttribute("name2", "sessionInfo");
	application.setAttribute("name3", "applicationInfo");
	
	out.println("Use pageContext<br/>");
	out.println("page Property value in:"+pageContext.getAttribute("name0")+"<br/>");
	out.println("request Property value in:"+pageContext.getRequest().getAttribute("name1")+"<br/>");
	out.println("session Property value in:"+pageContext.getSession().getAttribute("name2")+"<br/>");
	out.println("application Property value in:"+pageContext.getServletContext().getAttribute("name3")+"<br/>");
%>
</body>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Keywords: JSP Session MySQL JDBC

Added by vexious on Thu, 02 Jan 2020 00:30:39 +0200