简体   繁体   English

发送/接收带参数的 GET 请求

[英]Sending/Receiving GET Request with Parameters

I have a situation in which I want to select all records from a database given a specific id.我有一种情况,我想从给定特定 ID 的数据库中选择所有记录。 The request is first sent from JavaScript, which is received by a Servlet which accesses a DAO that in turn queries a database.请求首先从 JavaScript 发送,由 Servlet 接收,后者访问 DAO,然后查询数据库。 The data will then, obviously, make its way back to the front-end.显然,数据随后将返回前端。 I'm just a little cloudy on passing these parameters along so that the database is queried correctly.在传递这些参数以便正确查询数据库时,我有点不明白。

I am currently getting a 500 error which is due to my parameters not being passed along correctly.我目前收到 500 错误,这是由于我的参数没有正确传递。

Starting at the JavaScript with the initial request:从带有初始请求的 JavaScript 开始:

let xhr = new XMLHttpRequest();
xhr.open('GET', 'http://localhost:8080/project1attempt/reimbursement? 
employee_id=' + x._id, true);
xhr.send();

Receiving the parameters at the Servlet is my biggest point of confusion , therefore the code here is incomplete ( rs is a Reimbursement Service):在 Servlet 接收参数是我最大的困惑点,因此这里的代码不完整( rs是一个报销服务):

protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
throws ServletException, IOException {

    resp.setContentType("application/json");
    int id = ;
    List<Reimbursement> reimbursements = rs.findAllReimbursements(id);
    String json = new ObjectMapper().writeValueAsString(reimbursements);
    resp.getWriter().write(json);
}

And the query:和查询:

public List<Reimbursement> findAllReimbursements(int id) {
    List<Reimbursement> reimbursements = new ArrayList<>();

    try
        (Connection c = manager.getConnection()) {

        String sql = "SELECT reimbursement_id, date, description, amount, 
typing_id, employee_id" +
                "FROM reimbursements" +
                "WHERE reimbursement_id = ?";

        PreparedStatement ps = c.prepareStatement(sql);
        ps.setInt(1, id);
        ResultSet rs = ps.executeQuery();

        Reimbursement r = null;
        while (rs.next()) {
            r = new Reimbursement();
            r.setId(rs.getInt("reimbursement_id"));
            r.setDate(rs.getDate("date"));
            r.setDescription(rs.getString("description"));
            r.setAmount(rs.getDouble("amount"));
            r.setTypingId(rs.getInt("typing_id"));
            r.setEmployeeId(rs.getInt("employee_id"));
            reimbursements.add(r);
        }
        return reimbursements;
    } catch (SQLException e) {
        throw new BlabApplicationDataException("Could not connect to 
Reimbursement Repository" + id);
    }
 }

You can use getParameter method of HttpServletRequest to get the URL parameter you need.您可以使用HttpServletRequest getParameter方法来获取您需要的 URL 参数。

Probably, this is the line you are looking for.可能,这就是您正在寻找的线路。

String idStr = req.getParameter('employee_id');
if(idStr != null) { 
   int id = Integer.parseInt(idStr);
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM