如何使用来自另一个JSP的参数进行查询

时间:2016-05-17 10:42:45

标签: java postgresql jsp servlets

我正在使用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?

非常感谢!

2 个答案:

答案 0 :(得分:1)

将第一个jsp中的链接更改为

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

在第二个jsp中,使用request.getParameter("country")获取从第一个jsp发送的值

答案 1 :(得分:1)

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

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

现在您可以将country变量传递给Query,如下所示

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