我有一个状态类型为Tinyint的数据库。我想使用链接和端点(例如'notifi?id =&status = 1&userid =')将0更改为1(总是)。似乎没有POST请求它不起作用。
我已经尝试为状态设置一个永久号码,并尝试更新和插入命令,但可以使用。
这是我的代码:
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