消息关闭
    暂无新消息!
图里是要实现的效果


但是现在莫名的一直数据传不过去,代码还没全写完,不知道问题出在哪里,请大神支支招

这是我的Servlet代码

package com.limbo.servlet;

import com.limbo.test.Search;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "SearchServlet",urlPatterns = "/Search")
public class SearchServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String choose=request.getParameter("v");
        Search search=new Search();

        switch (choose){
            case "a":
            search.setA(search.getA()+1);
            break;
            case "b":
                search.setB(search.getB()+1);
                break;
            case "c":
                search.setC(search.getC()+1);
                break;
            case "d":
                search.setD(search.getD()+1);
                break;
                default:
                    System.out.println("错误!");
        }
        int total=search.getA()+search.getB()+search.getC()+search.getD();
        double percentA=search.getA()/total;
        double percentB=search.getB()/total;
        double percentC=search.getC()/total;
        double percentD=search.getD()/total;
        request.setAttribute("p1",percentA);
        request.setAttribute("p2",percentB);
        request.setAttribute("p3",percentC);
        request.setAttribute("p4",percentD);
        request.getRequestDispatcher("LifeSearch.jsp").forward(request,response);
//        PrintWriter out=response.getWriter();
//        out.println(percentA);
//        request.getRequestDispatcher("LifeSearch.jsp");


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}



这是对象封装

package com.limbo.test;

public class Search {
    private int a;
    private int b;

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public int getB() {
        return b;
    }

    public void setB(int b) {
        this.b = b;
    }

    public int getC() {
        return c;
    }

    public void setC(int c) {
        this.c = c;
    }

    public int getD() {
        return d;
    }

    public void setD(int d) {
        this.d = d;
    }

    private int c;
    private int d;

}


这是JSP页面代码


<%--
  Created by IntelliJ IDEA.
  User: Limbo
  Date: 2017/8/21
  Time: 14:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>LifeSearch</title>
</head>
<body>
<hr size="10" width="50%" color="black">
<%
    String percentA=request.getParameter("p1");
    String percentB=request.getParameter("p2");
    String percentC=request.getParameter("p3");
    String percentD=request.getParameter("p4");

%>
市民消费水平调查表
<form action="/Search" method="post">
    <input type="radio" name="v" value="a">1500以下<br>
    <input type="radio" name="v" value="b">1500-3000<br>
    <input type="radio" name="v" value="c">3000-5000<br>
    <input type="radio" name="v" value="d">5000以上<br>
    <input type="submit">
</form>
<button type="button">查看结果</button>
<table align="right" border="1">
    <tr>1500以下 <%=percentA%></tr><br>
    <tr>1500-3000 <%=percentB%></tr><br>
    <tr>3000-5000 <%=percentC%></tr><br>
    <tr>5000以上 <%=percentD%></tr><br>
</table>
</body>
</html>

5个回答

︿ 2
String percentA=request.getParameter("p1");
    String percentB=request.getParameter("p2");
    String percentC=request.getParameter("p3");
    String percentD=request.getParameter("p4");
修改为
String percentA=request.getAttribute("p1");
    String percentB=request.getAttribute("p2");
    String percentC=request.getAttribute("p3");
    String percentD=request.getAttribute("p4");
︿ 1
你的doget请求里面是不是忘记写处理方法,然后请求为get请求的原因。不知道是不是这个原因,请试一下