JAVA:对于更新和插入,我可以使用端点或 GET 请求来处理 servlet 中的数据吗

标签 java mysql sql jsp servlets

我有一个状态为 Tinyint 类型的数据库。我想使用链接和端点(如“notifi?id=&status=1&userid=”)将 0 更改为 1(始终)。似乎没有 POST 请求就无法工作。

我尝试为状态设置永久编号,并尝试了更新和插入命令,但没有成功。

这是我的 dao 代码:

public void markRead(Notify notify)  {

    try {
        Connection con= NotifyDao.getConnection();
        PreparedStatement ps=con.prepareStatement(
                "UPDATE notifications set status=? where userid=?");

        ps.setInt(1,notify.getNotifyid());
        ps.setInt(2,notify.getNotifystatus());

        ps.executeUpdate();

        con.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }


}

我的servlet代码

 protected void doPost(HttpServletRequest request,
                      HttpServletResponse response) throws ServletException, IOException {

Notify notify = new Notify();

    int id = Integer.parseInt(request.getParameter("id"));
    int status = Integer.parseInt(request.getParameter("status"));
    int userid = Integer.parseInt(request.getParameter("userid"));

    if (status==1){

        request.setAttribute("status", status);

        NotifyDao notifyDao = new NotifyDao();

        notifyDao.markRead(notify);

    }else  {

        response.sendRedirect("error.jsp");
    }

任何帮助或指导将不胜感激

最佳答案

在您的 servlet 中重写另一个方法 doGet(HttpServletRequest request, HttpServletResponse response)doPost() 一起调用 do post 方法,

像这样:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
{
//your existing code
}  

protected void doGet(HttpServletRequest request, HttpServletResponse response)
{
doPost(request,response);
}

另请参阅Oracle Docs

关于JAVA:对于更新和插入,我可以使用端点或 GET 请求来处理 servlet 中的数据吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55369462/

相关文章:

java - Reveal/know/get 发送页面使用servlet中的request或session

mysql - mysql全文搜索中的前缀和后缀通配符

sql - 车辆识别码(VIN)设计

sql - 如何根据此列将 SELECT 输出列转换为任意值?

sql - 加入 100 个表

java - java 中十六进制数字的 extratinf RGB 分量

java.sql.SQLTransactionRollbackException : At least one parameter to the current statement is uninitialized 异常

javascript - 如何在 Javascript 中从 Vert.x 服务器接收 websocket 消息?

php - codeigniter ActiveRecord 方法转换为查询

mysql - 我用 Rails 创建了 2 个独立的应用程序,但它们使用的是同一个数据库! (MySQL)?