I'm having trouble putting an attribute back into a request in java.
Basically I have a jsp that gets an 'id' from the link to that page:
Integer prodId = Integer.parseInt(request.getParameter("productId"));
Then in my command (I'm using the command pattern) I get the 'id' from the request and then return to the same jsp page but then the 'id' is no longer in the request and I get a number format exception
I have tried:
request.setAttribute("prodId", id);
but to no avail
Anyone got any ideas?
Thanks.
Request Attribute and Request Parameters are different things.
1.Try retrieving it as :
request.getAttribute("prodId");
from your JSP, once you set the request attribute in command class, as
request.setAttribute("prodId", id);
This code gets the request parameter, not request attribute.
request.getParameter("productId");
2.Or, if you are forwarding to the JSP page from the Command class :
//this will let you use request.getParameter() in JSP.
request.getRequestDispatcher(jspFilePath).forward(request,response);
Related
I was given this as an in class problem and recievd 0 as the result, but I can't find the fault with it. The questions is Write the lines of code you would need to use in a servlet to retrieve a parameter
from an incoming request and add it to the session as an attribute:
My answer:
String param1= request.getParameter("param1");
HttpSession session= request.getSession();
String parameter1= (String)request.getAttribute("param1");
session.setAttribute("param1", parameter1);
Also, is the '(String)' necessary in the second line? or just good practice?
Thanks :)
You've made a very common mistake, confusing attributes with parameters. In your code you have:
String parameter1= (String)request.getAttribute("param1");
Attributes can be thought of as meta-data of the request. For example, if the request is made via SSL then you can get data about the request from the attributes. Take a look at the documentation for getAttribute for more details.
Parameters, on the other hand, can be used to get URL parameters. Your last question actually points you in the correct direction - getParameter() already returns a String so you don't need to cast it.
Properly written the line above should be:
String parameter1= request.getParameter("param1");
getParameter() - used to get url parameter from request in server side(java side).
Where as If you want to fetch any values from java side to jsp(view side)
you can use setAttribute() in Server side(Java side) and get the value using getAttribute() from jsp.
My problem is when I'm trying to access a POST Variable with request.getParameter("name"), it works perfectly. But in some conditions, when a POST request arrives at my application, I also need to get GET Parameter from the Query String.
As far as I can see, with getParameter, you can only access current request's parameters, but, as in my condition, as I said, I also need to fetch GET Parameters inside doPost method.
Is there a way to fetch GET Parameters without parsing the Query String?
If you have parameters with the same name in the query string and in the posted form data, use getParameterValues().
Example:-
String fromQuery = request.getParameterValues("name")[0];
String fromForm = request.getParameterValues("name")[1];
The getParameter() method can return (if possible) both GET and POST parameters as it works transparently between GET and POST. You don't need to do any explicit work to get the GET parameters. you can use getParameter for both query parameters and POST parameters.
But should you do it? - It's considered a poor design practice especially if there is sensitive information to be sent.
Take a look at this answer:
HTTP POST with URL query parameters -- good idea or not?
I think you have a confusion here. You can retrieve all the request parameters (in both GET or POST or others) using the same getParameter(..) depending upon the type of request. If it's a GET request, you can retrieve all the GET parameters.
If it's a POST request, you can retrieve all the POST parameters. You get parameters using getParameter(...). And you make one request at a time. If you make a POST request in html or JSP file, you use doPost method receive all the parameters. At this point, there is nothing in GET request. Then after that, you make a GET request, you retrieve all the parameters in doGet method. At this moment, there is nothing in POST. Remember, HTTP requests are stateless.
To complete #Rei answer check out this code :
your form
<form action="?nom=nom1">
<input type="hidden" name="nom" value="nm2"/>
your doPost
System.out.println(request.getParameter("nom"));
String s = "";
for(String ss : request.getParameterValues("nom")) {
s += "|" + ss;
}
System.out.println(s);
System.out.println(request.getParameterMap().get("nom"));
what will be printed
nom1
|nom1|nm2
[Ljava.lang.String;#c7068db
ps : thanks to Julien for the code and testing
I want to get the page response (http://202.72.14.52/p2/cci/SearchHistory.aspx) but the page only allows post method. I could't get the response because I don't know what to set in the parameter name/value pairs. I have tried to insert some data in request parameter such as TextBox1=2015&TextBox2=1&TextBox3=2016&TextBox4=17&R1=v2&DropdownList1=01
Can you make a example using this web http://requestmaker.com/?
I have the following problem. I want to use a controller, same for every page in my application. It is a common form included via include jsp in every page and it sends a mail to a predefined email account. The problem is, when I am posting the form, I get redirected to a blank page, with the url being my RequestMapping value despite the method called is actually void. So I need now to redirect me, after sending the mail to the page where I came from. How do I get access to the url link of the page that redirected me, into sending the email? Thanks
When returning void and one isn't handling writing the response yourself Spring MVC delegates detection of which view to render to a RequestToViewNameTranslator for which there is a single implementation the DefaultRequestToViewNameTranslator. Which basically takes the incoming URL, strips any suffixes (like .html etc.) and uses that as the viewname. The behavior you see now.
I guess you have 3 possible solutions
Add a hidden form attribute which contains the current page
Use the referer request header to determine where the request came from (not a 100% solution, See Alternative to "Referer" Header)
Submit the form through ajax so that you stay on the current page
For option 2 add the HttpServletRequest as a parameter to your request handling method and retrieve the header.
public String foo(HttpServletRequest request) {
String referer = request.getHeader("Referer");
return "redirect:" + referer;
}
I would probably go for option 3 and maybe add option 1 as a fallback when no javascript is available.
How can I pass the value of a variable from javascript to a java variable?
<% String st = "<script>document.writeln(selected)</script>";
out.print("value = " + st);%>
This is my code for java getting the values from javascript variable, selected. But no value is displayed.
You have to make a request and send your variable from the browser (where Javascript lives) to the server (where the JSP lives) that way.
Ajax would work, or an HTML form.
In you JSP, you can then receive it as a request parameter (or as part of the request body, or as a request header).
Javascript runs on client. JSP runs on server. The only way to pass information from client to server in web environment is via HTTP parameters or HTTP headers during HTTP request or as a part of request body if method is POST or PUT.
So, you should create such request. It can be done using either changing of your document location or utilizing AJAX call.
you can pass parameter or make a hidden field inside your jsp code and using javascript assign value for this hidden field, then get parameter value in java code.
Use HTML forms.
On server side, you'll get the data in HTTPServletRequest parameter.
Check this too: Building my first Java Web Application