我有一个状态为 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/