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