1. Difference between request. getParameter () and request. getAttribute ()
1. request. getParameter () is achieved through the implementation of the container to obtain data transmitted through methods such as post and get, request. setAttribute () and getAttribute () are transferred only within the web Container, but only in the request processing phase.
2. The data transmitted by the request. getParameter () method is transmitted from the Web Client to the Web server, representing the HTTP request data. The request. getParameter () method returns String-type data.
The data transmitted by the request. setAttribute () and getAttribute () methods only exists in the Web container. The HttpServletRequest class has the setAttribute () method instead of the setParameter () method. Let's take an example. If two WEB pages are in a link, they are starting from 1. JSP link 2. in JSP, the link is 2. JSP can obtain request parameters through the getParameter () method.
Suppose that 1. JSP contains
- <Form Name="Form1" Method="Post" Action="2.jsp">
- Enter the User Name:<Input Type="Text" Name="Username">
- <Input Type="Submit" Name="Submit" Value="Submit">
- Form>
The request parameter username is obtained through the request. getParameter ("username") method in 2. JSP:
<% String username = request. getParameter ("username"); %> however, if the two WEB pages are in a forwarding relationship, you can use the getAttribute () method to forward data from the source WEB shared request range, let's take an example. 1. JSP and 2.JSP
1. JSP wants to pass the current username to 2.jsp. How can this data be transmitted? Call the following setAttribute () method in 1.jsp:
- <%
- StringUsername=Request. GetParameter ("username ");
- Request. setAttribute ("username", username );
- %>
- <Jsp: forward Page="2.jsp" />
- In 2. jsp, use the getAttribute () method to obtain the username:
- <% StringUsername= (String) request. getAttribute ("username"); %>
2. request. getAttribute () and request. setAttribute ()
Request. getAttribute ("nameOfObj") to obtain the Value of the control in the form of JSP page 1. In fact, the Object name and value in the Form Control are stored in a hash table. Therefore, the name of the Object is given here and the corresponding value is found in the hash table.
The request is used to pass values between different pages. when setAttribute (position, nameOfObj) is set. JSP to B. after a JSP request is passed, the request will lose the scope of its function. setAttribute (). When session. setAttribute () is used, this value is always stored in a process.
P.S.: JavaScript and JSP cannot pass values to each other because JavaScript runs on the client and JSP runs on the server. If you want to transfer parameters between them, you can set a hidden control in JSP and use its value in combination with the preceding usage to pass the required value.
- Brief Introduction to advanced operations on JSP Databases
- Interactive use of JSP data and JavaScript data
- Functions and principles of several encodings in JSP and Servlet
- How to solve the problem of garbled JSP page display
- Connect to various databases using JDBC in JSP