0
<% Integer i=(Integer)request.getAttribute("count"); %>

I want to increment/decrement my array counter(i) by pressing the link next/prev. But so far it has not worked. I tried using script. My code is:

<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>

The script code is:

function prev(i)
{
if(i==-1)
    i=0;
document.getElementById("prev").value=i;
request.setAttribute("count",i);
document.getElementById("Form").submit();
}

And my servlet code is:

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);    

Could you please look through this and tell me what is wrong or could you tell me a better way to implement this??

My error is: Controller request is undefined.

My web-xml file is

<?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>
4

4 回答 4

0

It looks like you're mixing up server side and client side code. Expression such as <%=(i-1)%> is evaluated on the server, and the variable i there will not be the same as the one on your javascript

You need to review your business requirements, where should the counter be stored? On the server / client side?

于 2013-06-14T05:02:36.080 回答
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();
}`

小服务程序代码

Integer num=Integer.valuesOf(request.getParameter("prev"));
if(num==1)
    request.getRequestDispatcher("/jsp/Page2.jsp").forward(request, response);
else if(num==2)
于 2013-06-14T06:05:22.087 回答
0

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>
于 2013-06-14T07:22:15.110 回答
0

表单值作为请求参数传递给 servlet。您可以简单地获取值 request.getParameter("prev"); 您正在分配值 request.setAttribute(i); 这不是必需的。

于 2013-06-14T05:09:32.063 回答