【问题标题】:How to do a query with a parameter coming from another JSP如何使用来自另一个 JSP 的参数进行查询
【发布时间】:2016-05-17 10:42:45
【问题描述】:

我正在练习使用 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 传递到另一个?

非常感谢!

【问题讨论】:

    标签: java postgresql jsp servlets


    【解决方案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+"';" );
    

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 好的,但是如何在查询中添加国家变量?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-15
      • 1970-01-01
      • 1970-01-01
      • 2014-10-22
      • 1970-01-01
      相关资源
      最近更新 更多