Jack
Jack

Reputation: 6620

How to make sure there is not any parameter in the url?

I know how to make sure if a specific parameter exists in the url. I would like to make sure there is not any parameter in the url not just a specified one. How can I accomplish this? I have a long list of parameters and it looks like inefficient to have a list of conditions for all of them.

Sample URL is: www.domain.com/Product/UniqueId

As each product has many unique parameters, users might reach product pages through, unique code,unique supplier, unique sig, unique pod etc of the products.

For example:

www.domain.com/Product/UniqueId?code=uniqueCode
www.domain.com/Product/UniqueId?sig=uniqueSig
www.domain.com/Product/UniqueId?pod=uniquepod
www.domain.com/Product/UniqueId?circle=circleNumber&location=Location&color=Color

The questions that I found relevant but not useful in my case were 1,2,3,4

I need to do such testing in JSP not Java.

Upvotes: 7

Views: 3723

Answers (7)

Yzgeav Zohar
Yzgeav Zohar

Reputation: 175

Use the methods of java.net.URI to get all you need about the URL.

Upvotes: 2

NonameSL
NonameSL

Reputation: 1475

We can easily check for parameters using Regex. Lets say you have the following URLs:

www.domain.com/Product/UniqueId?foo=bar
www.domain.com/Product/UniqueId

Now, the actual URL can be anything. We can mark it as .*, which means anything. (Dot = any character, * = Zero or more times. There are more complex regexes to check for URLs but you said you are given URLs hence they aren't relevant).

After you matched your URL, you want to check for parameters. Parameters are in the syntax of:

?key=value&key=value&key=value...

So we want to check for a question mark followed by a combination of keys and values. The regex would be:

\?((.*=.*)(&?))+

That matches a question mark, followed by: a key (.), an equals sign, a value(.) and a possible & (&?), 1 or more times (+)

So you can put it in a method like so:

public boolean hasParameters(String url){
    return url.matches(".*\\?((.*=.*)(&?))+");
}

www.domain.com/Product/UniqueId?foo=bar would match the regex, and www.domain.com/Product/UniqueId wouldn't, because it isn't followed by the parameters.

P.S: The double slash before the question mark is in sake of java, not in sake of regex.

Link: Regex checker with the following regex & example

If you want to use it in JSP, simply put this method in a scriplet (started with <% and ended with %>), or anywhere else in your code actually.

Upvotes: 2

markspace
markspace

Reputation: 11030

This works out a bit easier with the URI class:

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;

public class UrlTest2
{

   public static void main( String[] args )
           throws MalformedURLException, URISyntaxException
   {
      URI url = new URI( "www.domain.com/Product/UniqueId" );
      System.out.println( url.getQuery() );  // prints "null"
      URI url2 = new URI( "www.domain.com/Product/UniqueId?pod=uniquepod" );
      System.out.println( url2.getQuery() );  // prints "pod=uniquepod"
   }
}

So if getQuery() returns null there are no parameters. Otherwise, there are.

Regarding your edit: Well, I don't really recommend putting code inside a JSP. It's consider bad practice nowadays.

But if you must, it looks like you can ask for all of the parameters and if the result is 0, there weren't any.

<%
   Map params = request.getParameterMap();
   boolean noParams = (params.getSize() == 0);
%>

JSP code is untested, but should give you the idea. You can then use the boolean noParams later in the JSP code.

Upvotes: 5

Andreas
Andreas

Reputation: 159096

Followup

With the new requirement that it has to be done in JSP, even though the Spring handler code listed in the original answer below could add a Model attribute with the relevant information, here is how to do that.

Similar two ways as below:

<!-- Use the parameter Map -->
<c:if test="${empty param}">
  <!-- no query parameters given -->
</c:if>
<!-- Use the request object -->
<c:if test="${pageContext.request.queryString == null}">
  <!-- no query parameters given -->
</c:if>

Original Answer

You tagged spring-mvc, so that means you have something like this (would have been nice if you had posted it):

@Controller
public class ProductController {

    @RequestMapping("/Product/UniqueId")
    public String uniqueId(Model model,
                           @RequestParam(path="code"    , required=false) String code,
                           @RequestParam(path="sig"     , required=false) String sig,
                           @RequestParam(path="pod"     , required=false) String pod,
                           @RequestParam(path="circle"  , required=false) String circle,
                           @RequestParam(path="location", required=false) String location,
                           @RequestParam(path="color"   , required=false) String color) {
        // code here
    }
}

Here are two ways of checking for "no query parameters":

// Get all parameters in a Map
public String uniqueId(Model model,
                       ...,
                       @RequestParam Map<String, String> allParams) {
    if (allParams.isEmpty()) {
        // no query parameters given
    }
    // code here
}
// Use the request object
public String uniqueId(Model model,
                       ...,
                       HttpServletRequest request) {
    if (request.getQueryString() == null) {
        // no query parameters given
    }
    // code here
}

You might want to check this link for all the various types of parameters that Spring can give you:
http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-arguments

Upvotes: 4

Master Slave
Master Slave

Reputation: 28519

JSP hold an implicit object a paramValue that maps the request parameters to an array. You can use this object, and JSTL length function to learn the number of parameters directly in jsp, something like

<c:if test="${fn:length(paramValues) gt 0}">...</c:if>

make sure to include the following taglibs at the beginning of your jsp page

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

Upvotes: 2

AlpharettaTechy
AlpharettaTechy

Reputation: 390

Not sure what you mean by "to make sure there is not any parameter".. you just can't, clients can use whatever URL they want to hit your server. You can only choose to process based on the URL string your server code receives.

Otherwise, here is two pointers:

  1. Are you trying to check or parse the URL ? If so you can do: url.indexOf('?') < 0 , assuming url varialble has the entire url string...

  2. Also see Java URL api to get query string, etc.

Upvotes: 2

bakki
bakki

Reputation: 314

try this http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#contains(java.lang.CharSequence).

I gave you code just to find is there any parameters passed in url. It won't give the number of parameters like that..

String stringObj ="www.domain.com/Product/UniqueId";
(stringObj.contains("?"))?true: false;

Upvotes: 2

Related Questions