ajax实现异步刷新,并通过json接收发送数据!

前端之家收集整理的这篇文章主要介绍了ajax实现异步刷新,并通过json接收发送数据!前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

packageservlet;

importjava.io.IOException;
importjava.io.PrintWriter;
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.sqlException;

importjavax.servlet.ServletException;
importjavax.servlet.http.HttpServlet;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;

importcom.util.JdbcUtil;

publicclassAjaxextendsHttpServlet{
privatestaticfinallongserialVersionUID=1L;

publicAjax(){
super();
}

protectedvoiddoGet(HttpServletRequestrequest,
HttpServletResponseresponse)throwsServletException,IOException{
doPost(request,response);
}

protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ request.setCharacterEncoding("utf-8");response.setContentType("text/html"); Connectionconn=null; PreparedStatementpstmt=null; ResultSetrs=null; PrintWriterout=response.getWriter(); Stringuname=request.getParameter("uname"); try{ conn=JdbcUtil.getConnection(); Stringsql="select*fromuser_1whereuname=?"; pstmt=conn.prepareStatement(sql); pstmt.setString(1,uname); rs=pstmt.executeQuery(); if(rs.next()){ System.out.println("json"); //Stringjson="{\"userName\":\""+uname+"\",\"userNo\":\"123\"}"; Stringjson="{\"userName\":\""+uname+"\",\"userNo\":123}"; //Stringjson="{userName:"+uname+",userNo:123}"; out.write(json); System.out.println(json); }else{out.print("false"); System.out.println(uname);} out.close(); JdbcUtil.closeConnection(conn,pstmt); }catch(ClassNotFoundExceptione){ e.printStackTrace(); }catch(sqlExceptione){ e.printStackTrace(); } }

原文链接:https://www.f2er.com/ajax/166458.html

猜你在找的Ajax相关文章