<% Integer i=(Integer)request.getAttribute("count"); %>
我想通过按下next / prev链接来增加/减少我的数组计数器(i)。 但到目前为止它还没有奏效。我尝试使用脚本。我的代码是:
<form action="<%=application.getContextPath()%>/Controller" method="get" id="Form">
<input type="hidden" value="" name="prev" id="prev"></input>
<a class="button_prev" href="javascript:prev('<%=(i-1)%>')">prev</a>
</form>
脚本代码是:
function prev(i)
{
if(i==-1)
i=0;
document.getElementById("prev").value=i;
request.setAttribute("count",i);
document.getElementById("Form").submit();
}
我的servlet代码是:
Integer num=(Integer)request.getAttribute("chapter");
if(num==1)
request.getRequestDispatcher("/jsp/Page2.jsp").forward(request, response);
else if(num==2)
request.getRequestDispatcher("/jsp/Page3.jsp").forward(request, response);
else if(num==3)
request.getRequestDispatcher("/jsp/Page4.jsp").forward(request, response);
请你仔细看看,告诉我出了什么问题,或者你能告诉我一个更好的方法来实现这个吗?
我的错误是:控制器请求未定义。
我的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">
<display-name>Website</display-name>
<servlet>
<description>
</description>
<display-name>Controller</display-name>
<servlet-name>Controller</servlet-name>
<servlet-class>
com.tcs.website.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/Controller</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
答案 0 :(得分:0)
看起来你正在混淆服务器端和客户端代码。在服务器上评估<%=(i-1)%>
等表达式,并且变量i与javascript上的变量不同
您需要检查您的业务需求,计数器应存储在何处?在服务器/客户端?
答案 1 :(得分:0)
表单值作为请求参数传递给servlet。 您只需获取值request.getParameter(“prev”); 您正在分配值request.setAttribute(i);这不是必需的。
答案 2 :(得分:0)
试试这个
<form action="/Controller" method="get" id="Form">
<input type="hidden" value="" name="prev" id="prev"></input>
<a class="button_prev" href="javascript:prev()">prev</a>
</form>
Javascript代码
`function prev(){`
`var i = parseInt(document.getElementById("prev").value)-1;
if(i==-1)i=0;
document.getElementById("prev").value=i;
document.getElementById("Form").submit();
}`
Servlet代码
Integer num=Integer.valuesOf(request.getParameter("prev"));
if(num==1)
request.getRequestDispatcher("/jsp/Page2.jsp").forward(request, response);
else if(num==2)
答案 3 :(得分:0)
1)你为什么称我为“阵列计数器”?
2)你的servlet代码检索'chapter'属性,但你的html或javascript中没有提到'chapter'。
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代码中,变量请求引用了什么?
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 ------------------------------------------- < / p>
<?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>