简体   繁体   English

将值从Java传递到jsp或servlet

[英]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. 我的问题是我从数据库中获得了价值,我想使用href向它发送另一个jsp或servlet。 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 . 我在您的错误消息中看到它DOUBLE_WHITESPACE in QUERY显示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. 如果您对JSP开发Ditch scriptlet十分认真(十多年前就已不常用),请熟悉Java标准标记库和JSP表达语言。

I am not quite sure what 'special' is here, however using EL your code will look something like the below: 我不太确定这里是什么“特殊”内容,但是使用EL,您的代码将如下所示:

<!-- 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. 如果这不起作用,则在任何范围内都没有带有键“特殊”的Bean。

Note that if you are working with a database in your JSP you should consider refactoring to use the standard JSTL SQL tags. 请注意,如果您在JSP中使用数据库,则应考虑重构以使用标准JSTL SQL标记。 See below for an example: 参见以下示例:

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

See also: 也可以看看:

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

http://beginnersbook.com/2013/11/jsp-expression-language-el/ 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). *请注意,要使第二个示例正常工作,您的应用必须符合Servlet 3规范(在此之前,EL中不支持通过mthod参数进行传递)。 See further: https://stackoverflow.com/a/6337222/1356423 进一步了解: https : //stackoverflow.com/a/6337222/1356423

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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