如何在JSP / Servlet中更新数据

问题描述 投票:0回答:1

我是JSP / Servlet的初学者,但我被困住了。我想编辑一个表单,但是修改后我的页面为空。

这里是一个例子

1)我单击udpate

enter image description here

2)我更改值

enter image description here

3)单击更新时,我有一个空白页,我不明白问题所在?

enter image description here

这是我的servlet(UserHandler)

public class UserHandler extends HttpServlet {    
    private static String INSERT = "/user.jsp";
    private static String Edit = "/edit.jsp";
    private static String UserRecord = "/listUser.jsp";
    private UserDao dao;

    public UserHandler() {
        super();
        dao = new UserDao();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String forward=""; 
        String action = request.getParameter("action"); 
        if (action.equalsIgnoreCase("delete"))
        { 
            int userId = Integer.parseInt(request.getParameter("userId")); 
            dao.removeUser(userId); 
            forward = UserRecord; 
            request.setAttribute("users", dao.getAllUsers());
        } 
        else if (action.equalsIgnoreCase("editform")){          
            forward = Edit; 
        }
        else if (action.equalsIgnoreCase("edit")){ 
            int userId = Integer.parseInt(request.getParameter("userId")); 
            UserBean user = dao.getUserById(userId); 
            request.setAttribute("user", user); 
        } else if (action.equalsIgnoreCase("listUser")){ 
            forward = UserRecord; request.setAttribute("users", dao.getAllUsers());
        } else { 
            forward = INSERT; 

        } RequestDispatcher view = request.getRequestDispatcher(forward); 

        view.forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String redirect="";

        String action = request.getParameter("action");


        if(action.equalsIgnoreCase("insert"))
        {


            UserBean user = new UserBean();

            user.setfName(request.getParameter("firstName"));
            user.setlName(request.getParameter("lastName"));
            String userid = request.getParameter("userid");
            if(userid == null || userid.isEmpty())
            {
                dao.addUser(user);
            }

            else { 
                user.setUserid(Integer.parseInt(userid)); 
                dao.editUser(user);
            } 

            RequestDispatcher rd = request.getRequestDispatcher(UserRecord); 
            request.setAttribute("users", dao.getAllUsers()); 
            rd.forward(request, response);



        }
    }

还有我的edit.jsp

<body>
<%
    UserBean user = new UserBean();
%>
<%
    UserDao dao = new UserDao();
%>
<form method="POST" action='UserHandler' name="frmEditUser"><input
    type="hidden" name="action" value="edit" /> <%
    String uid = request.getParameter("userId");
    if (!((uid) == null)) {
        int id = Integer.parseInt(uid);
        user = dao.getUserById(id);
 %>
<table>

    <tr>
        <td>First Name</td>
        <td><input type="text" name="firstName" value="<%=user.getfName()%>"/></td>
    </tr>
    <tr>
        <td>Last Name</td>
        <td><input type="text" name="lastName" value="<%=user.getlName()%>"/></td>
    </tr>
    <tr>
        <td></td>
        <td><input type="submit" value="Update" /></td>
    </tr>
</table>
<%
    } else
        out.println("ID Not Found");
%>
</form>
</body>

非常感谢您的帮助。

java jsp servlets crud
1个回答
0
投票

如果您的数据库已更新,我猜字符串UserRecord =“ /listUser.jsp”时出现问题

© www.soinside.com 2019 - 2024. All rights reserved.