我正在练习使用PostgreSQL和JSP开发一个简单的Web应用程序。

让我简单地解释一下。

我有一个“用户”表,其中包含有关足球运动员的信息(姓名,姓氏,国家/地区)。

我有这个JSP,它可以查询以获得表中包含的玩家的所有国家。然后,JSP将每个国家/地区显示为链接。

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8" import="model.User"%>
<!DOCTYPE html>

    <head>
        <link rel="stylesheet" type="text/css" href="layout.css">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Choose a Country</title>
    </head>
    <body>
        <h1>Choose a Country</h1>
        <p>

<%
       Connection c = null;
       Statement stmt = null;
       try {
         Class.forName("org.postgresql.Driver");
         c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/firstapp",
                           "postgres", "admin");

         c.setAutoCommit(false);
         System.out.println("Opened database successfully" +"\n");

         stmt = c.createStatement();
         ResultSet rs = stmt.executeQuery("SELECT DISTINCT country FROM users;");

         while (rs.next() ) {
            //int id = rs.getInt("player_id");
            String  country = rs.getString("country");

            User u = (User) request.getAttribute("user"); %>

            <a href="./showCountry.jsp"><% out.print(country); %></a>

            <br/>
       <%
               }
         rs.close();
         stmt.close();
         c.close();

       } catch (Exception e) {
         System.err.println( e.getClass().getName()+": "+ e.getMessage() );
         System.exit(0);
       }%>

</p>
    </body>


我想要的是以下内容:当我单击列表中的一个国家(链接)时,另一个JSP应该执行另一个查询,如下所示:

SELECT * from users where country = *country chosen from the first JSP*


因此,第二个JSP应该以某种方式选择第一个JSP所选择的国家

这是我的第二个JSP

<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"
        import="model.User"%>
<!DOCTYPE html>


    <head>
        <link rel="stylesheet" type="text/css" href="layout.css">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Players from chosen country</title>
        <input Type="BUTTON" Value="Home Page" onclick="location.href='index.html'">
    </head>
    <body>
        <h1>Players from chosen country</h1>
        <p>
<%
       Connection c = null;
       Statement stmt = null;
       try {
           Class.forName("org.postgresql.Driver");
           c = DriverManager
                   .getConnection("jdbc:postgresql://localhost:5432/firstapp",
                           "postgres", "admin");

         c.setAutoCommit(false);
         System.out.println("Opened database successfully" +"\n");

         stmt = c.createStatement();
         ResultSet rs = stmt.executeQuery( "SELECT * FROM users WHERE country = *chosen country*;" );

         while (rs.next() ) {
            int id = rs.getInt("user_id");
            String  firstname = rs.getString("firstname");
            String  lastname = rs.getString("lastname");
            String  country = rs.getString("country");



    User u = (User) request.getAttribute("user"); %>


First Name: <% out.print(firstname); %> <br/>
Last Name: <% out.print(lastname); %> <br/>
Country: <% out.print(country); %> <br/>
<p/>
       <%
               }
         rs.close();
         stmt.close();
         c.close();

       } catch ( Exception e ) {
         System.err.println( e.getClass().getName()+": "+ e.getMessage() );
         System.exit(0);
       }%>

</p>
</body>


如何将所选国家/地区从一个JSP传递到另一个JSP?

非常感谢!

最佳答案

在您的第一个代码中替换以下代码



 <a href="./showCountry.jsp"><% out.print(country); %></a>




 <a href="./showCountry.jsp?country=<%=country%>"><% out.print(country); %></a>


您可以使用以下命令在second.jsp中设置国家/地区值:

String country = request.getParameter("country");


现在,您可以将国家/地区变量传递给Query,如下所示

ResultSet rs = stmt.executeQuery( "SELECT * FROM users WHERE country ='"+country+"';" );

10-07 13:06
查看更多