JSP--built-in object

1. out object

Used to output various data types to the client; manage the output buffer on the application server.

There are mainly two methods, out.print() and out.println() . The difference between the two is that the out.print() function will not change the line after the output is completed, and the out.println() function will end the current line, the next output statement will start output on the next line. But wrapping in output doesn't wrap on a web page. A line break on a web page should print the string "<br>".

2. The request object

Responsible for obtaining the request information from the client, there are mainly the following methods:

String getMethod() gets the submission method;

String getRequestURI() gets the requested URL address;

String getProtocol() gets the protocol name;

String getServletPath() obtains the path of the server file requested by the client;

String getQueryString() gets the query part of the URL;

String getServerName() gets the server name;

String getServerPort() to get the server port number;

String getRemoteAddr() gets the IP address of the client.

In addition, the request object can also obtain the parameters of the client. The two commonly used methods are as follows:

String getParameter(String name) obtains the name value transmitted from the client to the server, and returns null when the parameter name transmitted to this function has no actual parameter corresponding to it;

String[] getParameterValues(String name) Returns all values ​​of the specified parameter as a string array.

3. The response object

Responsible for sending a response to the client, you can use the response object to redirect, redirecting is to jump to another page. Methods as below:

response.sendRedirect(target page path);

Note: Another main form of the redirection method:

<jsp:forward page=""></jsp:forward>

The difference between the two: ① Judging from the address display of the browser:

The forward method belongs to the server side to request resources. The server directly accesses the target address, reads the response content of the target address, and then sends the read content to the browser, so the address of the client browser remains unchanged;

The response method is to tell the client to let the browser know which address to request, which is equivalent to re-requesting by the client, so the address display bar will change.

② From the perspective of data sharing:

The page forwarded by the forward method and the target page forwarded to can share the data in the request;

Neither the page forwarded by the response method nor the target page forwarded to can share the data in the request;

③ From the function point of view:

The forward method can only forward requests between resources in the same web application, which can be understood as an internal operation of the server;

The response method can redirect to other resources of the current application, as well as resources in other applications on the same site, or even use absolute URLs to redirect to other site resources.

④ From the perspective of efficiency:

The forward method is more efficient because the jump only occurs on the server side;

The response method is relatively inefficient, because it is equivalent to making another request.

4. The session object is responsible for saving some information during a session of the same client.

5. The application object represents the environment information of the entire application.

6. The exception object represents the exception that occurred on the page, and the page exception information can be obtained through it.

7. The page object represents the current JSP page itself, such as this in the Java class definition.

8. The pageContext object represents the context of this JSP.

9. The config object represents the ServletConfig of this JSP.

Attachment: Cookie operation

The following methods are mainly used when writing cookies:

response.addCookie(Cookie c) : write the Cookie to the client through this method;

Cookie.setMaxAge(int second) : Set the survival time of Cookie through this method, and the parameter indicates the number of seconds of survival.

Exercise 1. Enter the user name and password on page 1, process the data on page 2, and print the correct user name and password output to page 3, otherwise print "login failed" to page 4.

The code for page 1 is as follows:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
  <body>
    <form action="Jsp2.jsp">
      用户名:<input type="text" name="user">
      密码:<input type="password" name="password">
      <input type="submit" name="submit" value="提交">
    </form>
  </body>
</html>

The page 2 code is as follows:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
  <body>
    <%
      String s1=request.getParameter("user");
      String s2=request.getParameter("password");
      if(s1.equals("qwert")&&s2.equals("123456")){
    %>
      <jsp:forward page="Jsp3.jsp"></jsp:forward>
      <%
      }else{
      %>
      <jsp:forward page="Jsp4.jsp"></jsp:forward>
     <%
      }
     %>
  </body>
</html>

The code for page 3 is as follows:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
  <body>
    <% 
      out.print("登录成功"+"<br>");
      out.print("用户名为:"+request.getParameter("user")+"<br>");
      out.print("密码为:"+request.getParameter("password")+"<br>");
    %>
  </body>
</html>

The code for page 4 is as follows:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
  <body>
    <% 
      out.print("登录失败");
    %>
  </body>
</html>

Exercise 2. Page 1 calculates the square of the specified data, and page 2 calculates the cube of the original data. (using the Cookie method)

The code for page 1 is as follows:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
  <body>
    <%
      String s1="10";
      int num=Integer.parseInt(s1);
     %>
     该数字的平方为:<%=num*num %>
    <%
      Cookie cookie=new Cookie("num",s1);
      cookie.setMaxAge(500);
      response.addCookie(cookie);
     %>
     <a href="Jsp6.jsp">页面2</a>
  </body>
</html>

The page 2 code is as follows:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html> 
  <body>
    <%
      String s1=null;
      Cookie[] cookies=request.getCookies();
      for(int i=0;i<cookies.length;i++){
        if(cookies[i].getName().equals("num")){
          s1=cookies[i].getValue();
          break;
      }
  }
  int number=Integer.parseInt(s1);
   %>
   该数字的立方为:<%=number*number*number %>
  </body>
</html>

Guess you like

Origin blog.csdn.net/weixin_65089091/article/details/129450677