Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jsp

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.

like image 968
Jack Avatar asked Nov 06 '15 00:11

Jack


2 Answers

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.

like image 174
markspace Avatar answered Dec 01 '22 02:12

markspace


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

like image 34
Andreas Avatar answered Dec 01 '22 02:12

Andreas