[JSP] JavaWeb common built-in objects

 

 

 

  1. session

 

//a页面

    <% request.getSession().setAttribute("key","session");%>
    <%=session.getId() %>
    <a href="requestPage.jsp">点击</a>
// jump page 
<% = session.getAttribute ( "Key")%> <% = session.getId ()%>

 

 

getSession () // create a session object, call with requst object parameters: to true : automatically created, false : do not create 

session.getId () // get id session of 
session.setMaxInactiveInterval ( 1 * 60 * 60 ) // Set session Auto delete time, in seconds, where 1 hour 

session.setAttribute ( "session", "12345678" ); // field to store session data, object type 

session.getAttribute ( "session" ); // get the data, remove type object 
session.removeAttribute ( "session"); // delete the specified data session scope

 

         2. request

This object is obtained by the user information submitted request.getParameter ();

This object contains the current context of the user request, including form information and transmission parameter information and the like

may also be obtained by a session object once the object
HttpSession session = request.getSession ();

This page can obtain data:


<form >
姓名:<input type="text" name="name" length="20"> 账号:<input type="text" name="id" length="20"> <input type="submit" value="go"><br> </form> <%=request.getParameter("name") %><br> <%=request.getParameter("id") %><br> <%! String a,b; %> <% if (request.getParameter("name")==null){ a=request.getParameter("name"); } if (request.getParameter("id")==null){ b=request.getParameter("name"); } %>

 

  

        Request.getParameter username = String ( "username" );
         // solve the Chinese garbled get requests 
        username = new new String (username.getBytes ( "ISO-8859-1"), "UTF-8");

Jump different pages, use action, servlet

 

 

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class s1
 */
@WebServlet("/s1")
public class s1 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public s1() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        System.out.println("hello");
         request.setAttribute("name","Feathers");
         request.getRequestDispatcher("requestPage.jsp").forward(request, response);
    }

    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        doGet(request, response);
    }

}

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<%@page import="java.util.*" %>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="s1">
   姓名:<input type="text"  name="name" length="20">
 账号:<input type="text" name="id" length="20">
 <input type="submit" value="go"><br>
</form >
 <%=request.getParameter("name") %><br>
  <%=request.getParameter("id") %><br>
  <%! String a,b; %>
 <%

 if(request.getParameter ( "name") == null ) { 
     A = request.getParameter ( "name" ); 

 } 
 IF (request.getParameter ( "ID") == null ) { 
     B = request.getParameter ( "name" ); 
 } 
  // into a key field of the request
 
 %> 
</ body> 
</ HTML>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
hey@
<%System.out.println((String)request.getAttribute("name")); %>
</body>
</html>

request.getRequestDispatcher () :( band data)

request.getRequestDispatcher ( "want to jump servlet name") .forward (request, response);
request.getRequestDispatcher("文件路径").forward(request, response);

 

 

   3.Response

 

This object is often used to jump completion page, automatically refresh the page, the page automatically jump, and other functions to disable caching

  method Page Jump:
Response.sendRedirect ( 'page path ");
  automatic page refresh method:
response.setHeader ( "refresh", 3) ; every 3 seconds refresh
  method of automatically jump page:
response.setHeader ( "refresh", "? 3; the URL of commom.jsp ref = = aaa");
  specify 3 seconds common.jsp automatically jump to the page and passing a parameter value aaa ref
  disable caching method:
response.setHeader ( "= the cache Control", "cache-NO");
response.serHeader ( "Pragma", "NO -cache ");
response.setHeader (" the Expires ", 0);
after disable caching, when we click the back, the client will automatically want to request a page on the server

 (This section Source: https://www.cnblogs.com/zhuxiang1633/p/9604487.html )

 

4.COOKIE

https://www.cnblogs.com/whgk/p/6422391.html

Guess you like

Origin www.cnblogs.com/SeasonBubble/p/11913353.html