Difference between getAttribute() and getParameter()
getParameter()
returns http request parameters. Those passed from the client to the server. For examplehttp://example.com/servlet?parameter=1
. Can only returnString
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
Non Resizable Window Border and Positioning
Scanner Double Value - Inputmismatchexception
Android Listview Selected Item Stay Highlighted
Launching Activities Within a Tab in Android
File.Encoding Has No Effect, Lc_All Environment Variable Does It
"Java.Sql.Sqlexception: I/O Error: Connection Reset" in Linux Server
Command Working in Terminal, But "No Closing Quote" Error When Used Process.Exec
Exception While Submitting a Mapreduce Job from Remote System
How to Instantiate an Abstract Class
Maximum Length of Intent Putextra Method? (Force Close)
Image Encryption/Decryption Using Aes256 Symmetric Block Ciphers
How to Add a Recyclerview Inside Another Recyclerview
Tomcat 7: How to Set Initial Heap Size Correctly