Servlets 如何保存通过servlet发送的javascript扩展的数据并将其存储在couchdb数据库中?

Servlets 如何保存通过servlet发送的javascript扩展的数据并将其存储在couchdb数据库中?,servlets,Servlets,我正在做一个扩展,用户可以选择网页上的文本并点击扩展,这将把选择的内容保存在COUCHDB数据库中。文本的选择在javascript扩展中完成,数据通过servlet保存。但是在连接所有东西之后,错误在扩展上显示为:error SAVING:NOT FOUND。我已经粘贴了使用过的文件。另外,如果我在创建一个类似servlet的服务器文件后,在wamp服务器上运行相同的popup.js文件(扩展文件),那么它运行得很好。我认为servlet代码中存在一些问题,请发表您的建议。 提前谢谢 Serv

我正在做一个扩展,用户可以选择网页上的文本并点击扩展,这将把选择的内容保存在COUCHDB数据库中。文本的选择在javascript扩展中完成,数据通过servlet保存。但是在连接所有东西之后,错误在扩展上显示为:error SAVING:NOT FOUND。我已经粘贴了使用过的文件。另外,如果我在创建一个类似servlet的服务器文件后,在wamp服务器上运行相同的popup.js文件(扩展文件),那么它运行得很好。我认为servlet代码中存在一些问题,请发表您的建议。 提前谢谢

Servlet代码:

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.HashMap;
    import java.util.Map;

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

    import com.fourspaces.couchdb.Database;
    import com.fourspaces.couchdb.Document;
    import com.fourspaces.couchdb.Session;
    import java.io.InputStream;

    public class NewServlet extends HttpServlet
    {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
            @Override
        public void doGet(HttpServletRequest req,HttpServletResponse res)throws           

    ServletException, IOException
        {
            doPost(req,res);
        }
            @Override
        public void doPost(HttpServletRequest req,HttpServletResponse res)throws 

    ServletException, IOException
        {
            res.setContentType("text/html");
            PrintWriter out=res.getWriter();
                    InputStream requestBodyInput = req.getInputStream();    
                  String s1=req.getParameter("title");
                  String s2=req.getParameter("url");
                  String s3=req.getParameter("summary");
                  String s4=req.getParameter("tags");
                   savedata(s1,s2,s3,s4);

        }


   public String savedata(String s1,String s2,String s3,String s4)throws IllegalArgumentException
         {

       Session dbSession;
        Database db;
        Document newdoc;
        final String title="title";
        final String url="url";
        final String summary="summary";
        final String tags="tags";

        String dbName = "checkurl";
        dbSession = new Session("localhost",5984);

        db = dbSession.getDatabase(dbName);
            newdoc = new Document();
            /*Map for list of properties for the new document*/

              /*Map for list of properties for the new document*/
              Map<String , String> properties = new HashMap<String,String>();

              properties.put(title, s1);
              properties.put(url, s2);
              properties.put(summary, s3);
              properties.put(tags, s4);

              newdoc.putAll(properties);

              /*Saving the new document in the 'student' database */
              db.saveDocument(newdoc);  
              return null;



         }
}




 Extension file: popup.js



     // This callback function is called when the content script has been 
    // injected and returned its results
    function onPageInfo(o)  { 
        document.getElementById('title').value = o.title; 
        document.getElementById('url').value = o.url; 
        document.getElementById('summary').innerText = o.summary; 

    } 

    // Global reference to the status display SPAN
    var statusDisplay = null;

    // POST the data to the server using XMLHttpRequest
    function addBookmark() {
        // Cancel the form submit
        event.preventDefault();

        // The URL to POST our data to
        var postUrl = 'http://localhost:8080/examples/NewServlet';

        // Set up an asynchronous AJAX POST request
        var xhr = new XMLHttpRequest();
        xhr.open('POST', postUrl, true);

        // Prepare the data to be POSTed
        var title = encodeURIComponent(document.getElementById('title').value);
        var url = encodeURIComponent(document.getElementById('url').value);
        var summary = encodeURIComponent(document.getElementById('summary').value);
        var tags = encodeURIComponent(document.getElementById('tags').value);

        var params = 'title=' + title + 
                       '&url=' + url + 
                     '&summary=' + summary +
                     '&tags=' + tags;

        // Replace any instances of the URLEncoded space char with +
        params = params.replace(/%20/g, '+');

        // Set correct header for form data 
        xhr.setRequestHeader('Content-type', 'application/json');

        // Handle request state change events
        xhr.onreadystatechange = function() { 
            // If the request completed
            if (xhr.readyState == 4) {
                statusDisplay.innerHTML = '';
                if (xhr.status == 200) {
                    // If it was a success, close the popup after a short delay
                    statusDisplay.innerHTML = 'Saved!';
                    window.setTimeout(window.close, 1000);
                } else {// Show what went wrong
                    statusDisplay.innerHTML = 'Error saving: ' + xhr.statusText;
                }
            }
        };

        // Send the request and set status
        xhr.send(params);
        statusDisplay.innerHTML = 'Saving...';
    }

    // When the popup HTML has loaded
    window.addEventListener('load', function(evt) {
        // Handle the bookmark form submit event with our addBookmark function
        document.getElementById('addbookmark').addEventListener('submit', addBookmark);
        // Cache a reference to the status display SPAN
        statusDisplay = document.getElementById('status-display');
        // Call the getPageInfo function in the background page, injecting content_script.js 
        // into the current HTML page and passing in our onPageInfo function as the callback
        chrome.extension.getBackgroundPage().getPageInfo(onPageInfo);
    });
import java.io.IOException;
导入java.io.PrintWriter;
导入java.util.HashMap;
导入java.util.Map;
导入javax.servlet.ServletException;
导入javax.servlet.http.HttpServlet;
导入javax.servlet.http.HttpServletRequest;
导入javax.servlet.http.HttpServletResponse;
导入com.fourspaces.couchdb.Database;
导入com.fourspaces.couchdb.Document;
导入com.fourspaces.couchdb.Session;
导入java.io.InputStream;
公共类NewServlet扩展了HttpServlet
{
/**
* 
*/
私有静态最终长serialVersionUID=1L;
@凌驾
公共void doGet(HttpServletRequest-req、HttpServletResponse-res)抛出
ServletException,IOException
{
doPost(请求、回复);
}
@凌驾
public void doPost(HttpServletRequest-req、HttpServletResponse-res)抛出
ServletException,IOException
{
res.setContentType(“文本/html”);
PrintWriter out=res.getWriter();
InputStream requestBodyInput=req.getInputStream();
字符串s1=req.getParameter(“标题”);
字符串s2=req.getParameter(“url”);
字符串s3=req.getParameter(“摘要”);
字符串s4=req.getParameter(“标记”);
保存数据(s1、s2、s3、s4);
}
公共字符串savedata(字符串s1、字符串s2、字符串s3、字符串s4)引发IllegalArgumentException
{
会议期间;
数据库数据库;
文件newdoc;
最终字符串title=“title”;
最终字符串url=“url”;
最终字符串summary=“summary”;
最终字符串tags=“tags”;
String dbName=“checkurl”;
dbSession=新会话(“localhost”,5984);
db=dbSession.getDatabase(dbName);
newdoc=新文档();
/*新文档属性列表的映射*/
/*新文档属性列表的映射*/
映射属性=新的HashMap();
出售(标题s1);
properties.put(url,s2);
资产负债表(汇总,s3);
属性。放置(标签,s4);
newdoc.putAll(属性);
/*将新文档保存在“学生”数据库中*/
db.saveDocument(newdoc);
返回null;
}
}
扩展文件:popup.js
//此回调函数在内容脚本被调用时调用
//注入并返回其结果
函数onPageInfo(o){
document.getElementById('title')。value=o.title;
document.getElementById('url')。value=o.url;
document.getElementById('summary').innerText=o.summary;
} 
//状态显示范围的全局引用
var statusDisplay=null;
//使用XMLHttpRequest将数据发布到服务器
函数addBookmark(){
//取消表格提交
event.preventDefault();
//要将数据发布到的URL
姿态变量http://localhost:8080/examples/NewServlet';
//设置异步AJAX POST请求
var xhr=new XMLHttpRequest();
xhr.open('POST',postUrl,true);
//准备要发布的数据
var title=encodeURIComponent(document.getElementById('title').value);
var url=encodeURIComponent(document.getElementById('url').value);
var summary=encodeURIComponent(document.getElementById('summary').value);
var tags=encodeURIComponent(document.getElementById('tags').value);
变量参数='title='+title+
“&url=”+url+
“&摘要=”+摘要+
“&tags=”+标记;
//将URLEncoded space char的任何实例替换为+
参数=参数替换(/%20/g,“+”);
//为表单数据设置正确的标题
setRequestHeader('Content-type','application/json');
//处理请求状态更改事件
xhr.onreadystatechange=函数(){
//如果请求完成
if(xhr.readyState==4){
statusDisplay.innerHTML='';
如果(xhr.status==200){
//如果成功,请在短暂延迟后关闭弹出窗口
statusDisplay.innerHTML='Saved!';
window.setTimeout(window.close,1000);
}否则{//显示出哪里出了问题
statusDisplay.innerHTML='保存错误:'+xhr.statusText;
}
}
};
//发送请求并设置状态
xhr.send(参数);
statusDisplay.innerHTML=“正在保存…”;
}
//当弹出HTML已加载时
window.addEventListener('load',函数(evt){
//使用addBookmark函数处理书签表单提交事件
document.getElementById('addbookmark')。addEventListener('submit',addbookmark');
//缓存对状态显示范围的引用
statusDisplay=document.getElementById('status-display');
//在后台页面中调用getPageInfo函数,注入content_script.js
//进入当前HTML页面a