jsp built-in object learning

request object

request object is mainly used to receive data transmitted from client to server through HTTP protocol link.
The main method of request is to deal with the parameters and options in the request submitted by the client browser.
Partial methods:

object getAttribute(String name) 		//Returns the attribute value specified by name, or null if not specified
Enumeration getAttibuteNames()		//Returns an enumeration of all available attribute names
String getCharacterEncoding()					//Return character encoding
Cookie[] getCOokie()		//Returns the cookie object of the client, resulting in an array of cookies
String getContentTyoe()		//Get the MIME type of the requester
String[]getParameterValues(String name)		//Returns all arrays containing the parameter name
void setAttribute(String key,Object obj)			//Set the property value of the property

The difference between getParameter() getParameterValues() getParameterNames()
getParameter() Gets the value of a single name in the form;
getParameterValues() gets all the value values of the same name in the form;
getParameterNames() gets the value of all name s in the form;
Give an example by radio and multiple selection:
Create pages with multiple check boxes and check boxes in the JSP file and display them in par.jsp.

<form action="par.jsp" method="post">
    //Hobbies:
    <input type="checkbox" name="hobby" value="book">Read a Book
    <input type="checkbox" name="hobby" value="game">Play games
    <input type="checkbox" name="hobby" value="music">Listen to the music
    <input type="checkbox" name="hobby" value="ball">Play a ball<br>
    <input type="radio" name="sex" value="male" checked>male
    <input type="radio" name="sex" value="female">female<br>
    <select name="address">
        <option value="Beijing">Beijing</option>
        <option value="Shanghai">Shanghai</option>
        <option value="Guangzhou">Guangzhou</option>
    </select><br>
    <input type="submit" value="Submission">
</form>

par.jsp

<body>
    <%
        request.setCharacterEncoding("utf-8");
        String hobby[]=request.getParameterValues("hobby");
        if(hobby!=null){
            for (int i=0;i<hobby.length;i++){
                out.println(hobby[i]+"<br>");
            }
        }
        String sex =request.getParameter("sex");
        out.println(sex);
        String address=request.getParameter("address");
        out.println(address);
    %>
    <%=request.getMethod()%><br>
<%=request.getRemoteAddr()%>
<%=request.getLocale().getDisplayCountry()%>
</body>

Show the effect:

response

The main function of response is to respond to clients.
The main methods are:

void addCookie(Cookie cookie)		//Add a Cookie object to save client user information
sendRedirect(java.lang.String location)		//Redirecting client requests
sendError(int xc)		//Send error message to customer Duan using specified status code
sendError(int xc,String msg)		//Send error messages to clients using specified status codes and descriptive information

sendRedirect and hyperlinks are somewhat similar

session object

When a user connects to a server, the server creates a session object for each user to save the status of each user.
. Partial methods:

void setAttribute(String name,Object value)		//Binding a value object to a session with a name name
object getAttribute(String name)		//Get the attribute value of name from the session object and return null if the attribute does not exist
void removeAttriute(String name)		//Delete the name attribute from the session, and if it does not exist, it will not execute, nor will an exception occur
void invalidate()		//Invalidate session while deleting attribute objects 
Boolean isNew()		//Detecting whether the current user is a new session
long getCreationTime()		//Returns the request time that the web container received to the customer group during the session

Id of session object
When a client first accesses a web application, the container creates a session object for it, and the server automatically assigns a unique id number to it to identify the unique identity of the user.

application

When the web server starts, the web server creates an application object for each web service directory. Application Your objects are opposed to each other and correspond to the web services directory. When a client browses between pages of the website he visits, the application object is the same until the server shuts down.

Object getAtttibute(String name)		//Returns the value of the application object attribute specified by name
void setvttribute(String name,Object object)		//Set properties, specify property names and property values
void removeAttribute(String name)		//Delete the corresponding attribute according to the attribute name
String getInitParamter()		//Get the initialization parameter value according to the initialization parameter name

page object

page object refers to the object of the current jsp program itself.

config object

The config object is used by the jsp container to pass information to the servlet program when it is initialized.

Main methods:

getServletContent()		//Returns a ServletContent object with server-related information
g
etInitParameterNames()		//Returns an enumerated object consisting of the names of all the parameters required to initialize the Servlet program.
getInitParameter(String name)		//Returns the value of the initial parameter of the servlet program, named name

pageContext object

The pageContext object provides access to all jsp objects and namespaces. You can access eight built-in objects other than yourself.

HttpSession getsession()		//Return session object
Object getPage()		//Return page object
		//Reason by analogy

exception object

Exception object is an exception object, which is generated when the page runs abnormally. If a jsp page wants to generate this object, it must change isErrorPage to true, otherwise it cannot be compiled.
Main methods:

String getMessage()		//Returns exception description
String toString()		//Returns a brief description of the exception
void printStackTrace()		//Display exceptions and their stack trajectories
Throwable FillInStack()		//Rewrite exception execution stack trajectory

Examples:
Create an error page

<%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="error.jsp" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <% int result=5/0;%>
</body>
</html>

Note the addition of an error.jsp to specify the error page
error.jsp code

<%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
//Exception information: <%=exception.getMessage()%><br>
//Exception Description: <%=exception.toString()%>
</body>
</html>

Note: isErrorPAge is set to true
Finally the browser opens

Keywords: JSP Session Attribute Java

Added by Perry Mason on Sat, 10 Aug 2019 13:36:11 +0300