加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax调用后台java类的例子

发布时间:2020-12-16 02:06:57 所属栏目:百科 来源:网络整理
导读:java端代码如下: 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.I

java端代码如下:

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,IOException {
	    	doPost(request,response);
	    }


}


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

  <servlet>
      <servlet-name>oaLoginLimitedServlet</servlet-name>
      <servlet-class>com.xxxx.xxxx.servlet.oaLoginLimitedServlet</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>oaLoginLimitedServlet</servlet-name>
      <url-pattern>/oaLoginLimitedServlet</url-pattern>
  </servlet-mapping>


重启相关服务。

通过ajax就可以调用了。

var msg = $.ajax({
 type: "post",url: ....+'/oaLoginLimitedServlet?userid='+ $('#act').val(),async:false
}).responseText;



??

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读