Difference Between Getattribute() and Getparameter()

Difference between getAttribute() and getParameter()


  • getParameter() returns http request parameters. Those passed from the client to the server. For example http://example.com/servlet?parameter=1. Can only return String

  • getAttribute() is for server-side usage only - you fill the request with attributes that you can use within the same request. For example - you set an attribute in a servlet, and read it from a JSP. Can be used for any object, not just string.

Java servlet: request.getParameter and request.setAttribute connected in a way I don't understand

getParameter and getAttribute are completely unrelated.

getParameter

Returns the value of a request parameter as a String, or null if the
parameter does not exist. Request parameters are extra information
sent with the request. For HTTP servlets, parameters are contained in
the query string or posted form data.

getAttribute

Returns the value of the named attribute as an Object, or null if no
attribute of the given name exists.

In other words, returns a value that was set using setAttribute().

Java servlet not receiving request attributes

The problem is method selection.

request.getParameter("yourAttributeName") only works for retrieving form data (<form></form>) - aka data from your .jsp page - as well as query parameters.

If one wishes to send information from one Java servlet to another Java servlet, as in the above code, one must use:

request.getAttribute("myAttributeName");

JSP Servlet getParameter giving null

While using enctype="multipart/form-data" you can not directly get parameters by using request.getParameter(name);. While using it, form fields aren't available as parameter of the request, they are included in the stream, so you can not get it the normal way. You can find a way to do this in the docs of using
http://commons.apache.org/proper/commons-fileupload//using.html,
under the section Processing the uploaded items.

Source:

Sending additional data with multipart

Getting null from request.getAttribute()

You are confusing request.getAttribute("") and request.getParameter(""). You need to use the latter:

public class UpdateServlet extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");

System.out.println(request.getParameter("input_id"));
System.out.println(request.getParameter("input_name"));
System.out.println(request.getParameter("input_beschreibung"));

RequestDispatcher rd=request.getRequestDispatcher("LoadServlet");
rd.forward(request, response);

}

See here for some further discussion:

https://stackoverflow.com/a/5243833/1356423



Related Topics



Leave a reply



Submit