java后台 ajax_ajax调用java后台的一个方法

8246194306cb0554699f6ac2fdd386ef.png

慕桂英3389331

 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      oaLoginLimitedServlet      com.xxxx.xxxx.servlet.oaLoginLimitedServlet          oaLoginLimitedServlet      /oaLoginLimitedServlet     重启相关服务。通过ajax就可以调用了。 [html] view plaincopy var msg = $.ajax({       type: "post",       url: ....+'/oaLoginLimitedServlet?userid='+ $('#act').val(),       async:false   }).responseText;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值