将参数从jsp动态传递给另一个jsp

时间:2016-07-25 14:42:10

标签: java html jsp servlets jstl

我正在研究用户登录和预测足球比赛的java应用程序,我创建了一个管理jsp,让所有团队从数据库中的存储表中命名,以设置本周匹配,并在匹配后设置最终结果完成将它们与用户结果进行比较并计算积分,顶级用户等......

我的admin.jsp我从jstl foreach循环中选择匹配团队,稍后设置最终结果,将它们与用户预测结果进行比较。

<%@page import="pws.daoImp.UsersDaoImp"%>
<%@page import="java.lang.String"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="pws.beans.Users"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>


<html>

<%       request.getAttribute("admminresult");
%>

<%
    List<String> teams1 = new ArrayList();


    UsersDaoImp udi = new UsersDaoImp();
    List<String> admminresult = new ArrayList();
    admminresult = udi.getadminresult();
    request.setAttribute("admminresult", admminresult);
    List<String> teams = new ArrayList();
    teams = udi.getallteams();
    request.setAttribute("teams", teams);



%>

<head>
    <link rel="stylesheet" type="text/css" href="css/main.css" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Match Prediction</title>


</head>
<body>





<div>
    <h1 class="title">Welcome to Match Prediction Site </h1>
    <div id="logo" >
        <img src="images/cl.png" alt="Smiley face" width="142" height="142">
    </div>
</div>
<form action ="AdminUserHandling" method = "post" >

    <div class="form-lables">
        <h1><select name="clteam1" >
                <c:forEach items="${teams}"  var="teams"  >
                    <option name="clm1g1" value="${teams}">
                        ${teams}
                    </option>
                </c:forEach>
            </select> Vs <select name="clteam1" >
                <c:forEach items="${teams}"  var="teams"  >
                    <option name="clm1g1" value="${teams}">
                        ${teams}
                    </option>
                </c:forEach>
            </select> </h1>
        <label for="user_lic">Goals : </label><input id="user_lic" name="clm1g1" type="number" min="" max="10" step="1" value ="1"/>
        <label for="user_lic">Goals : </label><input id="user_lic" name="clm1g2" type="number" min="" max="10" step="1" value ="1"/>
    </div>


    <input type ="submit" value="Submit" />

</form>

</body>
</html>

和我的servlet代码

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


     UsersDaoImp udi = new UsersDaoImp();


    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    String team1 = request.getParameter("clteam1");
    String team2 = request.getParameter("clteam2");
    String team1gl = request.getParameter("clteam1gl");
    String team2gl = request.getParameter("clteam2gl");
     Users user = (Users) request.getSession().getAttribute("user");



    System.out.println(team1);

    System.out.println(team2);

     udi.adminhandling(team1, team2,team1gl,team2gl); //save teams name and goals in database

    processRequest(request, response);
}


}

我的问题是我在管理页面jsp中设置了团队名称和最终结果,但是有问题将它们动态地传递给用户jsp,如果没有想法,那么任何帮助都会非常感激。

1 个答案:

答案 0 :(得分:0)

您可以使用 RequestDispatcher

  

定义一个从客户端接收请求并发送的对象   它们可以访问任何资源(例如servlet,HTML文件或JSP文件)   服务器。 servlet容器创建RequestDispatcher   object,用作位于的服务器资源的包装器   特定路径或特定名称。

RequestDispatcher rd = request.getRequestDispatcher("/path/to/your/users.jsp"); // mention correct path to your user.jsp here
request.setAttribute("teams",teams);
rd.forward(request, response);

您可以将对象设置为会话

 HttpSession session = request.getSession(false);
 session.setAttribute("teams",teams);