从数据库搜索后如何在JSP页面中显示数据?

时间:2018-08-02 09:49:20

标签: java sql jsp servlets jdbc

我只是在做一个JSP / Servlet项目。但是我有一个问题。当用户以格式输入名称时,SearchController中会发生操作以从数据库中搜索数据。但是问题是,如何在jsp页面中显示数据?

下面是搜索表单

<form action="SearchController">
  <input type="text" placeholder="search" name="search">
  <button type="submit"><i class="fa fa-search"></i></button>
</form>

下面是servlet

@WebServlet("/SearchController")
public class SearchController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);

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

        // Connect to mysql and verify username password

        try {
            Class.forName("com.mysql.jdbc.Driver");
         // loads driver
        Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/loginpublic?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT", "root", "1234"); // gets a new connection

        PreparedStatement ps = c.prepareStatement("select from login where search=? ");

        ResultSet rs = ps.executeQuery();

        } catch (ClassNotFoundException | SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

由于您未提供有关所使用框架的任何信息,因此,我只能建议您通过Google搜索JSP PPR(部分页面刷新)。注意,JSP背后总有东西。

类似https://myfaces.apache.org/trinidad/devguide/ppr.html

即:如果您使用的是ADF,则可以查看2个jsp标记属性:

  1. 已渲染
  2. partialTrigger