Java Servlet - 获取发布请求数据

时间:2017-03-27 16:04:38

标签: java angularjs servlets post

我有这个帖子请求,我使用Java servlet进行处理。

https://i.imgur.com/sJmw00W.png

但是,当我尝试使用该函数传递数据时 request.getParameter("app_id"),我得到一个空字符串。

使用AngularJS完成发布请求:

self.data = {
                app_id:self.app_id,
                process_id:self.process_id,
                desc:self.desc
            }

$http({
    url: "http://localhost:8080/addProcess",
    method: "POST",
    data: self.data
    });

这是java servlet代码:

package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AddProcess
 */
@WebServlet("/addProcess")
public class AddProcess extends HttpServlet {
    private static final long serialVersionUID = 1L;


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



                String process_id = request.getParameter("process_id");
                String app_id = request.getParameter("app_id");
                String desc = request.getParameter("desc");
                String driverClassMySQL = "com.mysql.jdbc.Driver";


                String sql = "INSERT INTO `processes` (`process_id`, `app_id`, `desc`) VALUES ('"
                        + process_id
                        + "', '"
                        + app_id
                        + "', '"
                        + desc
                        + "')";

                try {
                    Class.forName(driverClassMySQL).newInstance();
                    Connection con = DriverManager.getConnection("jdbc:mysql://localhost/gestao", "gestao", "gestaoricardo");
                    Statement s = con.createStatement();
                    s.executeUpdate(sql);
                    s.close();
                    con.close();
                } catch (InstantiationException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IllegalAccessException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (ClassNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

    }

}

0 个答案:

没有答案