简体   繁体   中英

pass value from java to jsp or servlet

i am working on a project. my work is retrieve data from database and displaying them. if required sent to next page for further processing. my problem is i got value from database and i want to send it another jsp or servlet using href. value i am retrieving is :

        <%=special.getString("id")%>

send it to:

      <a href="new.jsp?id=<%=special.getString("id")%>" class="action_button">Buy Now</a>

but when send data like this error is

错误消息是

is that correct? how do i do it? what is the correct method. i am struck here for long time please help me.

id周围使用单引号,而不是双引号。

"new.jsp?id=<%=special.getString('id')%>"

Use single quote inside double quote. I am trying to type code but not able to type. now it should work

You need to escape your quotes first or use single quotes so that your double quotes can work:

Note: Untested.

<a href='new.jsp?id=<%=special.getString("id")%>' class="action_button">Buy Now</a>

I see in your error message it says DOUBLE_WHITESPACE in QUERY . I would suggest you try encoding you url.

  <% String id=java.net.URLEncoder.encode(special.getString("id") , "UTF-8");%>
  <a href='new.jsp?id=<%=id%>' class="action_button">Buy Now</a>

If you're serious about JSP development ditch scriptlets (which went out of common use 10+ years ago) and familiarize yourself with the Java Standard Tag Libraries and JSP Expression Language.

I am not quite sure what 'special' is here, however using EL your code will look something like the below:

<!-- special is an object with a method getId()-->
<a href="new.jsp?id=${special.id}" class="action_button">Buy Now</a>

or

<!-- special is an object with a method getString(String key) -->
<a href="new.jsp?id=${special.getString('id')}" class="action_button">Buy Now</a>

If this doesn't work then there is no bean with key 'special' in any scope.

Note that if you are working with a database in your JSP you should consider refactoring to use the standard JSTL SQL tags. See below for an example:

http://www.tutorialspoint.com/jsp/jstl_sql_query_tag.htm

See also:

http://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm

http://beginnersbook.com/2013/11/jsp-expression-language-el/

*Note for the second example to work your app need to be compliant with the Servlet 3 specification (as passing mthod params was nut supported in EL before this). See further: https://stackoverflow.com/a/6337222/1356423

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM