如何在JavaScript文件中正确编写servlet url

时间:2016-12-20 08:38:29

标签: javascript xml tomcat servlets

我应该如何在myscript.js中编写url来在服务器上找到servlet BeerSelect?我已多次尝试但失败了。

myscripts.js:

   var url= "../SelectBeer.do?ImageID=" + escape(itemName); // How can I write the url correctly?

的index.html:

<html>
<head>
    <script src="scripts/myscripts.js" type="text/javascript"></script>
</head>

的web.xml:

 <servlet>
    <servlet-name> Ch3 Beer </servlet-name> 
    <servlet-class>com.example.BeerSelect</servlet-class> 
  </servlet>

  <servlet-mapping>
    <servlet-name> Ch3 Beer </servlet-name> 
    <url-pattern>/SelectBeer.do</url-pattern>
  </servlet-mapping>

directory tree

1 个答案:

答案 0 :(得分:0)

你试过吗

        var url= "SelectBeer.do?ImageID=" + encodeURIComponent(itemName);

另见https://stackoverflow.com/a/332888/1536382