212

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?

Sajad Bahmani
  • 17,325
  • 27
  • 86
  • 108
Josh Curren
  • 10,171
  • 17
  • 62
  • 73

9 Answers9

264

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");
%>
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • Is there something you have to do to enable EL? I'm using jboss6 and when i use ${param.accountID} it is treated as regular text from the browser. – simgineer May 03 '12 at 15:32
  • @simgineer First, the file MUST be JSP, not just plain HTML. Read the following topics: [Expression language, don't show variable value](http://stackoverflow.com/questions/6505107/expression-language-dont-show-variable-value), [EL expressions are not evaluated in JBoss AS 4.2.2](http://stackoverflow.com/questions/6343557/el-expressions-are-not-evaluated-in-jboss-as-4-2-2), [Expression Language in JSP not working](http://stackoverflow.com/questions/6505107/expression-language-dont-show-variable-value). – informatik01 Jun 25 '13 at 14:52
192

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>!");
    }
%>
Sajad Bahmani
  • 17,325
  • 27
  • 86
  • 108
  • 98
    Scriptlets are considered bad practice. – BalusC Dec 12 '09 at 11:23
  • 14
    Don't forget xmlEncode.. this is currently vulnerable to reflected xss – Esailija Feb 10 '13 at 21:15
  • 5
    -1 for XSS vulnerability. Would also be -1 for using a scriptlet when the job can be done using EL if I could vote twice. Seriously, *DO NOT DO THIS*. – Jules May 16 '17 at 23:08
  • Suggesting the use of scriptlets when a better solution exists is harmful to code quality. – Suketu Bhuta Aug 25 '17 at 22:11
  • 3
    Regarding "Scriptlets are considered bad practice.", see [this answer in another question](https://stackoverflow.com/a/2189123/4266338) for alternatives. – Pixelstix Dec 07 '18 at 19:29
87

Use EL (JSP Expression Language):

${param.accountID}

Taylor Leese
  • 51,004
  • 28
  • 112
  • 141
21

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>

Léa Massiot
  • 1,928
  • 6
  • 25
  • 43
  • Worth noting: param includes more values than just the URL query arguments (e.g. the values in the model map). if you need to check specifically if a value is in the URL query arguments (and, e.g. will be submitted as part of a form submission), you can look at ${pageContext.request.queryString} - but it's not broken down into a nice map like params is. – Paul Feb 18 '19 at 16:10
5

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.

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
4
String accountID = request.getParameter("accountID");
johannes
  • 15,807
  • 3
  • 44
  • 57
1

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.

rdj7
  • 1,905
  • 4
  • 18
  • 33
jay Patel
  • 11
  • 1
0

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);
}
0

page 1 : Detail page 2 : <% String id = request.getParameter("userid");%> // now you can using id for sql query of hsql detail product

null
  • 1
  • 1