The difference between Servlet & Jsp-getparameter and Request.getattribute

Source: Internet
Author: User

The GetParameter method gets the request parameters for the form or URL. Parameters are passed from the Web client to the Web service side. For example, a servlet like the following:

@WebServlet (name = "HelloServlet", Urlpatterns = {"/hello")}) Public classHelloServletextendsHttpServlet {@Overrideprotected voidDoget (httpservletrequest req, HttpServletResponse resp)throwsServletexception, IOException {enumeration<String> Paramnames =Req.getparameternames ();  while(Paramnames.hasmoreelements ()) {String paramname=(String) paramnames.nextelement (); Resp.getwriter (). println (ParamName+ ": " +Req.getparameter (paramname)); }    }    }

When the/HELLO?FOO=A&BAR=B request is received, the container passes the two parameters, Foo and bar, through HttpServletRequest to the Doget method.

C:\users\huey>curl "Http://localhost:8080/hello-mvn-web/hello?foo=a&bar=b" foo:abar:b

Request.getattribute gets the scope in the request scope property. Unlike GetParameter, which obtains parameters from the client, attribute is passed inside the servlet container. One common scenario is when a request is forwarded from one servlet to another, attribute is passed through the request. For example, the following a.jsp:

<% request.setattribute ("User", "Huey");%> <  page= "b.jsp"/>

A property named user is set in a.jsp and then forwarded to B.SJP. The contents of b.jsp are as follows:

<%     String user = (string) request.getattribute ("user");    Out.println ("Hello," + User + "!"); %>

Test Access/a.jsp:

C:\users\huey>curl "http://localhost:8080/hello-mvn-web/a.jsp" Hello, huey!

Another difference is that the GetParameter method returns a String object that the GetAttribute method returns as an object. This is also related to the difference, because attribute is passed inside the servlet container, so it can be any Java object, and parameter is passed from the HTTP client to the server, so it cannot be any Java object, only a string.

For Attribute,servletrequest, SetAttribute and RemoveAttribute are also provided to set up and remove attribute. For Parameter,servletrequest, only the GetParameter method is provided.

The difference between Servlet & Jsp-getparameter and Request.getattribute

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.