1)为什么称 i 为“数组计数器”?
2)您的 servlet 代码检索“章节”属性,但在您的 html 或 javascript 中没有提及“章节”。
3)您的javascript代码:
function prev(i)
{
if(i==-1)
i=0;
document.getElementById("prev").value=i;
request.setAttribute("count",i);
document.getElementById("Form").submit();
}
...在浏览器中执行。在浏览器中执行的代码不知道服务器上存在哪些变量和方法。同样,在服务器上执行的 servlet 不知道在浏览器中执行的某些 javascript 代码中存在哪些变量和方法。那么在您的 javascript 代码中,变量 request 指的是什么?
4)下面是一个完整的例子,你可以试试。你有很多活动部件,每个组件的路径都受不同的规则控制。
进入浏览器的网址:
http://localhost:8181/myapp2/Go/fish/in/a/lake
(我已将 Tomcat 配置为侦听端口 8181)
Tomcat中的目录结构:
webapps/myapp2/
js/
jquery-1.9.1.js
jquery-ui-1.10.2.js
myjs.js
jsp/
Initial.jsp
Page2.jsp
Page3.jsp
WEB-INF/
web.xml
classes/
com/
tcs/
website/
Controller.class
web.xml-------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>MySuperAwesomeController</servlet-name>
<servlet-class>com.tcs.website.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MySuperAwesomeController</servlet-name>
<url-pattern>/Go/fish/in/a/lake</url-pattern>
</servlet-mapping>
</web-app>
myjs.js----------------------------------------
$(document).ready( function() {
var chapter_val = $("#chapter_div").text();
chapter_val = parseInt(chapter_val);
chapter_val = (--chapter_val < 0) ? 0 : chapter_val;
alert("Assigning onclick handler to the link...\n" +
"When the link is clicked, the onclick function \n" +
"will change the link's url to include the chapter val => " + chapter_val);
$(".button_prev").on("click", function() {
this.href += "?chapter=" + chapter_val;
});
});
这是一个纯 javascript 版本(但你真的应该使用 jquery 来避免几个潜在的问题):
window.onload = function () {
var chapter_val = document.getElementById("chapter_div").childNodes[0].nodeValue;
chapter_val = parseInt(chapter_val);
chapter_val = (--chapter_val < 0) ? 0 : chapter_val;
alert("Assigning onclick handler to the link...\n" +
"When the link is clicked, the onclick function \n" +
"will change the link's url to include the chapter val => " + chapter_val);
document.getElementsByClassName("button_prev")[0].onclick = function() {
this.href += "?chapter=" + chapter_val;
};
};
Controller.java-----------------
package com.tcs.website;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Controller extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException,
ServletException {
String chapter = req.getParameter("chapter");
String jsp_path = null;
if (chapter == null) {
jsp_path = "/jsp/Initial.jsp";
}
else {
int chapter_num = 0;
try {
chapter_num = Integer.parseInt(req.getParameter("chapter"));
if ( (chapter_num == 3) || (chapter_num == 4) ) {
jsp_path = "/jsp/Page" +
Integer.toString(--chapter_num) +
".jsp";
}
}
catch (NumberFormatException e) {
System.out.println("Bad chapter number");
}
}
System.out.println("*********jsp_path=" + jsp_path); #goes to logs/catalina.out
req.getRequestDispatcher(jsp_path).forward(req, resp);
}
}
Initial.jsp----------
<!DOCTYPE html>
<html>
<head>
<title>Initial.jsp</title>
<script type='text/javascript' src='/myapp2/js/jquery-1.9.1.js'></script>
<script type='text/javascript' src='/myapp2/js/jquery-ui-1.10.2.js'></script>
<script type='text/javascript' src='/myapp2/js/myjs.js'></script>
</head>
<body>
<div id="chapter_div">5</div>
<a class="button_prev"
href="<%= application.getContextPath() %>/Go/fish/in/a/lake">prev</a>
</body>
</html>
Page2.jsp------------
<!DOCTYPE html>
<html>
<head>
<title>Page2.jsp</title>
<script type='text/javascript' src='/myapp2/js/jquery-1.9.1.js'></script>
<script type='text/javascript' src='/myapp2/js/jquery-ui-1.10.2.js'></script>
<script type='text/javascript' src='/myapp2/js/myjs.js'></script>
</head>
<body>
<div id="chapter_div"><%= request.getParameter("chapter") %></div>
<a class="button_prev"
href="<%= application.getContextPath() %>/Go/fish/in/a/lake">prev</a>
</body>
</html>
Page3.jsp------
<!DOCTYPE html>
<html>
<head>
<title>Page3.jsp</title>
<script type='text/javascript' src='/myapp2/js/jquery-1.9.1.js'></script>
<script type='text/javascript' src='/myapp2/js/jquery-ui-1.10.2.js'></script>
<script type='text/javascript' src='/myapp2/js/myjs.js'></script>
</head>
<body>
<div id="chapter_div"><%= request.getParameter("chapter") %></div>
<a class="button_prev"
href="<%= application.getContextPath() %>/Go/fish/in/a/lake">prev</a>
</body>
</html>