「ajax代理java」ajax代理转发

博主:adminadmin 2022-12-31 01:18:06 1193

今天给各位分享ajax代理java的知识,其中也会对ajax代理转发进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

JAVA的ajax方法?

//方式一:使用post请求的方式,不通过url传参,采用data传参

$.ajax({

url:"userAction_findMangerByDeptId",//访问的地址

type:"post",

data:{"传递到后台的参数名":参数},

dataType:'text',//后台返回的数据格式类型

success:function(data){

})

}

})

}

//方式二:这是get提交方法,会出现中文乱码,所以要 1.先将数据转码

参数名 = encodeURI(encodeURI(要转的参数));

$.ajax({

url:"userAction_findMangerByDeptId?参数名"+参数值,

type:"get",

dataType:'text',//后台返回的数据格式类型

success:function(data){

})

}

})

}

2.后台接收前端传递内容后要进行解码

String 参数名 = URLDecoder.decode(接收的参数);

如何用JAVA爬取AJAX加载后的页面

普通的爬取是抓不了js的之后的数据的 可以用phantomjs或者htmlUnit实现

附上phantomjs示列代码

package cn.wang.utils;

import java.util.Random;

import com.gargoylesoftware.htmlunit.BrowserVersion;

import com.gargoylesoftware.htmlunit.CookieManager;

import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;

import com.gargoylesoftware.htmlunit.WebClient;

public class htmlUnitUtils {

static WebClient webClient = null;

static Random random = new Random();

static{

//1.创建对象

webClient = new WebClient(BrowserVersion.CHROME);

//2.设置参数

//启动js

webClient.getOptions().setJavaScriptEnabled(true);

//关闭css渲染

webClient.getOptions().setCssEnabled(false);

//启动重定向

webClient.getOptions().setRedirectEnabled(true);

//设置连接超时时间 ,这里是10S。如果为0,则无限期等待

webClient.getOptions().setTimeout(1000 * 15);

//启动cookie管理

webClient.setCookieManager(new CookieManager());

//启动ajax代理

webClient.setAjaxController(new NicelyResynchronizingAjaxController());

//js运行时错误,是否抛出异常

webClient.getOptions().setThrowExceptionOnScriptError(false);

//设置浏览器请求信息

webClient.addRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

webClient.addRequestHeader("Accept-Encoding", "gzip, deflate");

webClient.addRequestHeader("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");

webClient.addRequestHeader("Connection", "keep-alive");

webClient.addRequestHeader("Upgrade-Insecure-Requests", "1");

}

public static void runJs(String url){

try {

webClient.addRequestHeader("User-Agent", Constant.useragents[random.nextInt(Constant.useragents.length)]);

//等待js渲染执行 waitime等待时间(ms)

webClient.waitForBackgroundJavaScript(1000 * 10);

//3.获取页面

webClient.getPage(url);

} catch (Exception e) {

e.printStackTrace();

} finally {

if(webClient != null){

webClient.close();

}

}

}

public static void main(String[] args) {

runJs("");

System.setProperty("phantomjs.binary.path", "D:\\works\\tool\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");

}

}

如何使用ajax调用java类

ajax调用java后台的方法,其实是通过url链接来访问,示例如下:package com.xxxx.xxxx.servlet;

import java.io.IOException;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.sql.DataSource;

public class oaLoginLimitedServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

private static Connection conn=null;

private static PreparedStatement pstmt=null;

public oaLoginLimitedServlet() {

super();

}

public void destroy() {

super.destroy();

}

public static String getCount(String userid)

{

String v_sql=".....";

String v_count="";

try {

pstmt = conn.prepareStatement(v_sql);

pstmt.setString(1, userid);

ResultSet rs = pstmt.executeQuery();

while(rs.next()){

v_count = rs.getString(1);

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}finally{

try {

pstmt.close();

conn.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return v_count;

}

public static Connection getConnection(){

Context ctx = null;

try {

ctx = new InitialContext();

DataSource ds = (DataSource)ctx.lookup("jndiname");

conn = ds.getConnection();

} catch (Exception e) {

e.printStackTrace();

}

return conn;

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String v_userid=request.getParameter("userid");

System.out.println(v_userid);

getConnection();

String v_count=getCount(v_userid);

response.setCharacterEncoding("UTF-8");

response.getWriter().write(v_count);

response.flushBuffer();

}

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request,response);

}

}

如果要前端能够访问到该servlet,需要将该servlet注册到 web.xml文件中。需要在web.xml文件中添加以下内容

[html] view plaincopy

servlet

servlet-nameoaLoginLimitedServlet/servlet-name

servlet-classcom.xxxx.xxxx.servlet.oaLoginLimitedServlet/servlet-class

/servlet

servlet-mapping

servlet-nameoaLoginLimitedServlet/servlet-name

url-pattern/oaLoginLimitedServlet/url-pattern

/servlet-mapping

重启相关服务。

通过ajax就可以调用了。

[html] view plaincopy

var msg = $.ajax({

type: "post",

url: ....+'/oaLoginLimitedServlet?userid='+ $('#act').val(),

async:false

}).responseText;

ajax代理java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于ajax代理转发、ajax代理java的信息别忘了在本站进行查找喔。