当前位置 : 主页 > 编程语言 > c++ >

使用ajax在向服务器发送请求

来源:互联网 收集:自由互联 发布时间:2021-06-30
login.html Insert title here 用户名 密码 UserServlet.java package servlet;import java.io.IOException;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.List;import javax.servlet.ServletE
login.html



 
Insert title here





 
用户名 密码
UserServlet.java
package servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import bean.Users;
import biz.IUserService;
import biz.impl.UserServiceImpl;

@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	
	IUserService service=new UserServiceImpl();
	Gson gson=new Gson();
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String type=request.getParameter("type");
			//System.out.println("1111");
			Method th;
			try {
				th = UserServlet.class.getMethod(type, HttpServletRequest.class,HttpServletResponse.class);
				th.invoke(this, request,response);
			} catch (NoSuchMethodException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
	}
	
	//登录
	public void login(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		String name=request.getParameter("name");
		String pwd=request.getParameter("pwd");
		Users info=service.getUserByNameAndPwd(name, pwd);
		if (info!=null){
			request.getSession().setAttribute("info", info);
			response.getOutputStream().print("1");
		}
		else
			response.getOutputStream().print("2");
	}
	
	//分页
	public void getUserPage(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		int pageIndex=request.getParameter("pageIndex")==null?1:Integer.parseInt(request.getParameter("pageIndex").toString());
		int pageSize=request.getParameter("pageSize")==null?10:Integer.parseInt(request.getParameter("pageSize").toString());
		
		List
 
   list=service.getUserPage(pageIndex, pageSize);
		if (list!=null&&list.size()>0){
			//需要把JAVA对象转换成JSON格式字符串
			String msg=gson.toJson(list);
			response.getWriter().print(msg);
		}
		else
			response.getOutputStream().print("1");
		response.getWriter().flush();
	}
	
	//通过姓名获取用户信息
	public void getUserByName(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException, InterruptedException{
		Thread.sleep(2000);
		String name=request.getParameter("name");
		if (service.getUserName(name))
			response.getOutputStream().print("1");
		else
			response.getOutputStream().print("2");
	}
	
	//删除用户
	public void deleteUser(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		int id=request.getParameter("id")==null?0:Integer.parseInt(request.getParameter("id").toString());
		if (service.deleteUsers(id))
			response.getOutputStream().print("1");
		else
			response.getOutputStream().print("2");
		response.getOutputStream().close();
	}
	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
 
网友评论