jsp页面跳转跳转路径写法

一般的写法为

/项目名/文件的url-pattem

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是连接数据库的代码示例,其使用了JDBC连接MySQL数据库: ``` import java.sql.*; public class DBConnection { private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; private static final String DB_URL = "jdbc:mysql://localhost/products"; private static final String USER = "root"; private static final String PASS = "password"; public static Connection getConnection() { Connection conn = null; try { Class.forName(JDBC_DRIVER); conn = DriverManager.getConnection(DB_URL, USER, PASS); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return conn; } } ``` 在以上代码,我们定义了JDBC驱动的名称、数据库的URL、用户名和密码。getConnection方法用于获取数据库的连接,并返回一个Connection对象。 在QueryProductServlet类,我们可以使用上述的DBConnection类获取数据库连接,并执行SQL语句查询数据,然后将结果存储到request对象,用于在JSP页面显示。具体的代码实现可以参考以下示例: ``` import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/QueryProductServlet") public class QueryProductServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String productId = request.getParameter("productId"); List<Product> productList = new ArrayList<>(); try (Connection conn = DBConnection.getConnection()) { String sql; PreparedStatement pstmt; if (productId == null) { // 查询所有商品信息 sql = "SELECT * FROM products"; pstmt = conn.prepareStatement(sql); } else { // 根据商品号查询商品信息 sql = "SELECT * FROM products WHERE id = ?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, productId); } ResultSet rs = pstmt.executeQuery(); while (rs.next()) { Product product = new Product(); product.setId(rs.getInt("id")); product.setName(rs.getString("name")); product.setPrice(rs.getDouble("price")); productList.add(product); } rs.close(); } catch (SQLException e) { e.printStackTrace(); request.getRequestDispatcher("error.jsp").forward(request, response); } if (productId == null) { // 显示所有商品信息 request.setAttribute("productList", productList); request.getRequestDispatcher("displayAllProduct.jsp").forward(request, response); } else if (productList.size() == 0) { // 商品号不存在 request.getRequestDispatcher("error.jsp").forward(request, response); } else { // 显示单个商品信息 request.setAttribute("product", productList.get(0)); request.getRequestDispatcher("displayProduct.jsp").forward(request, response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } ``` 以上代码,我们首先获取用户输入的商品号,然后使用DBConnection类获取数据库连接,根据用户输入的商品号,执行不同的SQL语句查询数据。查询结果存储到List<Product>,然后根据查询结果跳转到不同的JSP页面显示查询结果。如果查询出错,则跳转到error.jsp页面显示错误信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值