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