Difference between getAttribute and getParameter in java

Source: Internet
Author: User
Tags what parameter tomcat

1. getAttribute is used to obtain the setAttribute in jsp? Specified attribute

2. What parameter gets is string; what attribute gets is object.

3. request. the data transmitted by the getParameter () method is transmitted from the Web client to the Web server, representing the HTTP request data; request. the data transmitted by the setAttribute () and getAttribute () methods only exists in the Web container and is shared among the Web components with forwarding relationships. That is, the request. getAttribute () method returns objects in the request range, while the request. getParameter () method obtains the data submitted by http.


4. The HttpServletRequest class has the setAttribute () method instead of the setParameter () method.


5. When two Web components are linked, the linked component obtains the request parameters through the getParameter () method,


6. When the forwarding relationship is between two Web components, the forwarding target component shares the data within the request range with the conversion source component through the getAttribute () method.

-- GetParameter is of the String type. Or a http://a.jsp? Id = 123 in 123, or the data submitted by a form.
-- GetAttribute can be an object.
-- GetParameter () is used to obtain the parameter value passed by POST/GET;
-- GetAttribute () is used to obtain the data value in the object container;
-- GetParameter: used for client redirection, that is, when a link is clicked or a button is submitted for value transfer, it is used to receive data when a form or url is redirected for value transfer.
-- GetAttribute: used for server redirection, that is, the forward function is used in the sevlet, or the mapping. findForward function is used in struts. GetAttribute can only receive values transmitted by the program using setAttribute.
-- GetParameter () is used to obtain the parameter value passed by POST/GET;
-- GetAttribute () is the value of the SESSION;


GetParameter is generally used for parameters passed through forms and links.

Request. getAttribute ("name", "jerry ")

Example

The code is as follows: Copy code

<Jsp: include page = "login. jsp" flush = "true">
<Jsp: param name = "username" value = "<% = Request. getParameter (" username ") %>"/>
<Jsp: param name = "password" value = "<% = Request. getParameter (" password ") %>"/>
</Jsp: include>
 

The specific reason may be due to different tomcat versions. When compiling the jsp page, jsp processes <% = request. getParameter ("username") %> as a variable. Therefore, an error will be reported when you encounter it ."


Error message:

Org. apache. jasper. jasperException:/index1.jsp (line: 14, column: 38) Attribute value Request. getParameter ("username") is quoted with "which must be escaped when used within the value


There are two solutions to solve the above problems:

Solution 1
Rewrite it as follows:

The code is as follows: Copy code
<% = Request. getParameter ("username") %>

Solution 2
In the final analysis, it is a problem with the tomcat version. Change the tomcat version.

Personal opinion:

When you pass the value as a link parameter to the next page or serve, you can use getParameter () to get it. For example, aa. jsp? Id = 1; there is also form submission.

When the user places the value in an attribute in the request. setAttribute ("aa", "tt"), "aa", which can be any attribute name, can be obtained using getAttribute.


The request has a smaller range. It is just a request. Simply put, it is an operation on the page. request. getParameter () is used to obtain parameters from the url and form on the previous page. However, if a request involves multiple classes, you can use request. setAttribute () and request. getAttribute () to obtain parameters later. However, after the result is output, the request is over.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.