最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

html - Ways to access the query parameters JSP, JSTL, Javascript - Stack Overflow

matteradmin14PV0评论

I have been using the following 3 ways to access the query parameters in the URL.

JSP

String success = request.getParameter("success");
if(success!=null) {
//do something
}

JSTL

<c:if test="${not empty param.success}">
//do something
</c:if>

JavaScript

function getQueryParameter ( parameterName ) {
  var queryString = window.top.location.search.substring(1);
  var parameterName = parameterName + "=";
  if ( queryString.length > 0 ) {
    begin = queryString.indexOf ( parameterName );
    if ( begin != -1 ) {
      begin += parameterName.length;
      end = queryString.indexOf ( "&" , begin );
        if ( end == -1 ) {
        end = queryString.length
      }
      return unescape ( queryString.substring ( begin, end ) );
    }
  }
  return "null";
}

All 3 of these work for me, but I would like to know if there is any of these has any issue and which one is preferred?

I have been using the following 3 ways to access the query parameters in the URL.

JSP

String success = request.getParameter("success");
if(success!=null) {
//do something
}

JSTL

<c:if test="${not empty param.success}">
//do something
</c:if>

JavaScript

function getQueryParameter ( parameterName ) {
  var queryString = window.top.location.search.substring(1);
  var parameterName = parameterName + "=";
  if ( queryString.length > 0 ) {
    begin = queryString.indexOf ( parameterName );
    if ( begin != -1 ) {
      begin += parameterName.length;
      end = queryString.indexOf ( "&" , begin );
        if ( end == -1 ) {
        end = queryString.length
      }
      return unescape ( queryString.substring ( begin, end ) );
    }
  }
  return "null";
}

All 3 of these work for me, but I would like to know if there is any of these has any issue and which one is preferred?

Share Improve this question edited Oct 25, 2012 at 6:24 Pulkit Mittal asked Oct 25, 2012 at 6:14 Pulkit MittalPulkit Mittal 6,0765 gold badges22 silver badges28 bronze badges 2
  • is there any best way to access params in javascript(jquery) rather than you java script code? – Mahender Reddy Yasa Commented Jan 18, 2016 at 12:45
  • @MahenderReddyYasa stackoverflow.com/questions/901115/… – Pulkit Mittal Commented Jan 19, 2016 at 12:51
Add a comment  | 

1 Answer 1

Reset to default 17

In the first approach you are accessing the request params using Scriptlets, definitely it works, but you have to make an additional check for null. Therefore Scriptlets are always avoided.

In the second approach using JSTLs, it is better than first approach, a refined version over scriptlets giving you more flexibility and robustness. So, it is something we would always encourage. This is the best approach of the three.

In the last approach, using Javascript, though another method; but never encouraged. It involves extra care to handle params carefully and you have better options to do the same job. Not an ideal task for Javascript to handle that!

Post a comment

comment list (0)

  1. No comments so far