How to transfer data from JSP to servlet when submitting HTML form

前端 未结 4 1122
陌清茗
陌清茗 2020-11-21 23:14

I have a JSP page with an HTML form:


4条回答
  •  梦毁少年i
    2020-11-21 23:52

    Well, there are plenty of database tutorials online for java (what you're looking for is called JDBC). But if you are using plain servlets, you will have a class that extends HttpServlet and inside it you will have two methods that look like

    public void doPost(HttpServletRequest req, HttpServletResponse resp){
    
    }
    

    and

    public void doGet(HttpServletRequest req, HttpServletResponse resp){
    
    }
    

    One of them is called to handle GET operations and another is used to handle POST operations. You will then use the HttpServletRequest object to get the parameters that were passed as part of the form like so:

    String name = req.getParameter("name");
    

    Then, once you have the data from the form, it's relatively easy to add it to a database using a JDBC tutorial that is widely available on the web. I also suggest searching for a basic Java servlet tutorial to get you started. It's very easy, although there are a number of steps that need to be configured correctly.

提交回复
热议问题