Servlet提交数据

Servlet提交数据



前言

使用Servlet获取html提交的数据(JSON)


一、创建submit.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
    <title>用AJAX以JSON的方式提交数据</title>
    <script type="text/javascript" src="jquery-3.5.1.min.js"></script>
</head>
<body>
<!--显示form的内容,然后script中获取html中提交的name和id,封装成对象person,然后post提交ajax
    在post中用JSON.stringify将提交的数据转化为JSON格式的字符串-->
<form>
    名字:<input type="text" id="name"/><br>
    账号:<input type="text" id="id"/><br>
<!--  点击提交后将form中的数据取出来,组织成json格式,用ajax发送出去  -->
    <input type="button" value="提交" id="sender">
</form>
<div id="messageDiv"></div>
<script>
    $('#sender').click(function () {
        var name = document.getElementById('name').value;
        var id = document.getElementById('id').value;
        var person = {"name": name, "id": id};//json对象
        var url = "submitServlet";
        //使用post方式提交ajax
        $.post(
            url,//访问的页面
            {"data": JSON.stringify(person)},//提交的数据,JSON.stringify将javascript对象转化为JSON格式的字符串
            function (data) {//响应函数
                alert("提交成功,请在tomcat控制台查看服务器端接收到的数据");
            }
        );
    });
</script>

</body>
</html>

二、创建SubmitServlet

1.创建Person类

/**
 * @ClassName Person
 * @Author $童一
 * @Description $
 * @Param $
 * @return $
 * @Date $ $
 **/

public class Person {
    private String name;
    private int id;

    public String getName() {
        return name;
    }

    public int getId() {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Person[name="+ name + ",id=" + id +"]";
    }
}

2.创建SubmitServlet

mport net.sf.json.JSONObject;

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

@WebServlet(name = "SubmitServlet")
//Servlet提交数据
public class SubmitServlet extends HttpServlet {
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String data = request.getParameter("data");
        System.out.println("服务端接收的数据是:"+data);
        JSONObject json = JSONObject.fromObject(data);
        System.out.println("转换为JSON对象之后是:"+json);
        Person person = (Person) JSONObject.toBean(json, Person.class);
        System.out.println("转换为Hero对象之后:"+person);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

3.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>SubmitServlet</servlet-name>
        <servlet-class>SubmitServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>SubmitServlet</servlet-name>
        <url-pattern>/submitServlet</url-pattern>
    </servlet-mapping>
<web-xml>

总结

启动tomcat,访问http://localhost:8080/SubmitServlet/submit.html提交数据,html将获取的数据封装成person对象,在post中通过JSON.stringify()方法将javascript对象转化为JSON格式的字符串,提交数据,通过url访问SubmitServlet在tomcat控制台输出前端传回的数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值