将java字符串传递给html或jsp作为表单

时间:2017-10-02 19:46:44

标签: java html jsp

目前我正在制作汽车租赁应用程序,现在我正从数据表上的按钮抓取id。表单数据将id传递给后面,id是我如何查询后端以捕获所需的字符串。现在我试图重新尝试重新编写一个新的HTML页面,并准备好在html的占位符中更新这些字符串。 servlet如下。

protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    String carid = req.getParameter("id");
    String year;
    String make;
    String model;
    String color;

    ResultSet rs = null;
    Connection conn = null;
    Statement st = null;

    try {
        Context ctx = new InitialContext();
        Context env = (Context) ctx.lookup("java:comp/env");
        DataSource ds = (DataSource) env.lookup("jdbc/carRentalSystem");
        conn = ds.getConnection();
        st = conn.createStatement();

        rs = st.executeQuery("SELECT * FROM cardetails where id='" + carid + "'");
        if (rs.next()) {
            year = rs.getString("Year");
            make = rs.getString("Make");
            model = rs.getString("Model");
            color = rs.getString("color");

        } else {
            System.out.println("Invalid password, please try again");
            res.sendRedirect("login.html");
        }
    } catch (Exception e) {
        System.out.println(e);
    } finally {
        try {
            if (st != null) st.close();
        } catch (java.sql.SQLException e) { }
        try {
            if (conn != null) conn.close();
        } catch (java.sql.SQLException e) { }
        try {
            if (rs != null) rs.close();
        } catch (java.sql.SQLException e) { }
    }
}

1 个答案:

答案 0 :(得分:0)

我试图重定向到另一页,抱歉让人感到困惑。