I'm trying to get the query parameter client_id from a GET call with a url like this:
https://example.com?client_id=aclient-id¶m2=value2¶m3=value3
I'm getting a null value for clientId when I try to get the query parameter, any ideas why this is happening?
HttpServletRequest httpRequest = (HttpServletRequest) request;
final String clientId = httpRequest.getParameter("client_id");
The other calls like http.getRequestURI() and http.getMethod return the expected values.
Please check what the string value of the client id that is actually being sent to the server. In your example you say clientId in your code you're looking for client_id, while they make semantic sense to humans, those are different values to a computer.
You can also take a peek at all the parameter values in your HttpServletRequest:
httpRequest.getParameterMap()
.entrySet()
.stream()
.forEach(System.out::println);
In JSP how do I get parameters from the URL?
For example I have a URL www.somesite.com/Transaction_List.jsp?accountID=5
I want to get the 5.
Is there a request.getAttribute( "accountID" ) like there is for sessions or something similar?
About the Implicit Objects of the Unified Expression Language, the Java EE 5 Tutorial writes:
Implicit Objects
The JSP expression language defines a set of implicit objects:
pageContext: The context for the JSP page. Provides access to various objects including:
servletContext: The context for the JSP page’s servlet and any web components contained in the same application. See Accessing the Web Context.
session: The session object for the client. See Maintaining Client State.
request: The request triggering the execution of the JSP page. See Getting Information from Requests.
response: The response returned by the JSP page. See Constructing Responses.
In addition, several implicit objects are available that allow easy access to the following objects:
param: Maps a request parameter name to a single value
paramValues: Maps a request parameter name to an array of values
header: Maps a request header name to a single value
headerValues: Maps a request header name to an array of values
cookie: Maps a cookie name to a single cookie
initParam: Maps a context initialization parameter name to a single value
Finally, there are objects that allow access to the various scoped variables described in Using Scope Objects.
pageScope: Maps page-scoped variable names to their values
requestScope: Maps request-scoped variable names to their values
sessionScope: Maps session-scoped variable names to their values
applicationScope: Maps application-scoped variable names to their values
The interesting parts are in bold :)
So, to answer your question, you should be able to access it like this (using EL):
${param.accountID}
Or, using JSP Scriptlets (not recommended):
<%
String accountId = request.getParameter("accountID");
%>
In a GET request, the request parameters are taken from the query string (the data following the question mark on the URL). For example, the URL http://hostname.com?p1=v1&p2=v2 contains two request parameters - - p1 and p2. In a POST request, the request parameters are taken from both query string and the posted data which is encoded in the body of the request.
This example demonstrates how to include the value of a request parameter in the generated output:
Hello <b><%= request.getParameter("name") %></b>!
If the page was accessed with the URL:
http://hostname.com/mywebapp/mypage.jsp?name=John+Smith
the resulting output would be:
Hello <b>John Smith</b>!
If name is not specified on the query string, the output would be:
Hello <b>null</b>!
This example uses the value of a query parameter in a scriptlet:
<%
if (request.getParameter("name") == null) {
out.println("Please enter your name.");
} else {
out.println("Hello <b>"+request. getParameter("name")+"</b>!");
}
%>
Use EL (JSP Expression Language):
${param.accountID}
If I may add a comment here...
<c:out value="${param.accountID}"></c:out>
doesn't work for me (it prints a 0).
Instead, this works:
<c:out value="${param['accountID']}"></c:out>
request.getParameter("accountID") is what you're looking for. This is part of the Java Servlet API. See http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.html for more information.
String accountID = request.getParameter("accountID");
www.somesite.com/Transaction_List.jsp?accountID=5
For this URL there is a method call request.getParameter in java , if you want a number here cast into int, similarly for string value cast into string. so for your requirement , just copy past below line in page,
int accountId =(int)request.getParameter("accountID");
you can now call this value useing accountId in whole page.
here accountId is name of parameter you can also get more than one parameters using this, but this not work. It will only work with GET method if you hit POST request then their will be an error.
Hope this is helpful.
example you wanted to delete the subject record with its subject_id
#RequestMapping(value="subject_setup/delete/{subjectid}",method = RequestMethod.GET)
public ModelAndView delete(#PathVariable int subjectid) {
subjectsDao.delete(subjectid);
return new ModelAndView("redirect:/subject_setup");
}
and the parameter will be used for input on your query
public int delete(int subjectid) {
String sql = "update tbl_subject set isdeleted= '1' where id = "+subjectid+"";
return template.update(sql);
}
page 1 :
Detail
page 2 :
<% String id = request.getParameter("userid");%>
// now you can using id for sql query of hsql detail product
I'm switching from lucene to solr and I'm querying it with solrj.
After a query I'd like to obtain the explain and I executed this code:
QueryResponse queryResult = solrClient.query(collection, query, POST);
List<SolrDocument> recommended = queryResult.getResults();
Map<String, String> explainMap = queryResult.getExplainMap();
Map<String, Object> debugMap = queryResult.getDebugMap();
The explainMap contains the explanation of my query as a String.
Is it possible to obtain an object of explanation to avoid parsing the string?
For example I'd like to extract all the fields that matched and their value (without score and its composition).
My documents have only one textual field and many categorical or numeric fields.
I have to make a http request with encrypted query param from one end to another. Say for an example,
String queryParam = "bookName=WingsOfFire&author=AbdulKalam";
String encryptedQueryParam = encrypt(queryParam, encryptionKey);
The final url would be like
domain_name?encryptedQueryParam
On the other end, I would decrypt the query params and obtain the query String. Like
String decryptParam = decrypt(encryptedQueryParam, decryptKey);
Here I'm manually de-serialising my query params at the other end instead of request.getPameter("value").
Is there any other option to handle it in a better way? Any help would be greatly appreciated :-)
I have query parameters which are being sent from browser in the following format
sort[0][field]:prodId
sort[0][dir]:asc
How can I retrieve the above parameters in server using #QueryParam?
From chrome console
take:5
skip:0
page:1
pageSize:5
sort[0][field]:prodId
sort[0][dir]:asc
#QueryParam should be obtained from a Query String that is appended to the end of the request URL. Something like
http://host:port/app/something?key1=value2&key2=value2
You could then get value1 and value2 with
#QueryParam("key1") String value1,
#QueryParam("key2") String value2
Now in the title of your post, you use the word "Form". If this is form data you are trying to submit, you should consider some things. When putting the form data in the query String, this is usually done with data that is not sensitive, and used mainly for GET request, where the parameter values are used to help filter in getting the resource. If this is sensitive data that should be stored on the server, you generally want to POST the data as form data in the body of the request, as seen in the answer from your previous post
UPDATE
If you don't know the key names, which is required to use #QueryParam, you can obtain the entire query string from an injected UriInfo. Something like
#GET
#Path("/path/to/resource")
public Response getKendo( #Context UriInfo uriInfo) {
MultivaluedMap params = uriInfo.getQueryParameters();
StringBuilder builder = new StringBuilder();
for (Object key : params.keySet()) {
builder.append(key).append(":")
.append(params.getFirst(key)).append("\n");
}
return Response.ok(builder.toString()).build();
}
getQueryParameters() will return all the keys and values in MultivalueMap
Alternatively, if you know the keys, which are shown in the URL you posted in the comment
test.jsp?take=5&skip=0&page=1&pageSize=5&sort%5B0%5D%5Bfield%5D=prodId&sort%5B0%5D%5Bdir%5D=asc
then you cause just use all those key for the QueryParam, i.e.
public Response getKendo(#QueryParam("take") int take,
#QueryParam("skip") int skip,
#QueryParam("page") int page,
#QueryParam("sort[0][field]") String field...) {
}
All this crazy stuff sort%5B0%5D%5Bfield%5D is just how URL are encoded with special character, but JAX-RS will convert back to their rightful form, ie "sort[0][field]"