jsp中的一个下拉列表实例


最近在写一个网站后台,用到了一个下拉列表,在这将代码贴出来供大家参考!(水平有限)

实现的效果如下:

 

 

 

 

 

 

 

 

代码只要涉及三个文件,两个servlet文件:UserListServlet.java (负责在数据库中查询全部用户记录)和UserListByPowerServlet.java(主要根据权限来查询相关记录,当权限是“全部”时,会从此文件跳转到UserListServlet文件让其执行),还有一个jsp文件,UserList.jsp(负责显示查询的数据)

 

UserListServlet.java

 

 

public class UserListServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
           
		doPost(request ,response);
		
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		
		String pNo=request.getParameter("pNo");
		
		if(pNo==null || pNo.equals("") || pNo.trim().equals("0")){
			pNo="1";
		}
		oj_UserDao ud=new oj_UserDaoImpl();
		int pageSize=10;
		List list=ud.getUserList(Integer.parseInt(pNo), pageSize);
		int totalCount=ud.totalCount();
		int pageCount=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
		request.setAttribute("pNo", pNo);
		request.setAttribute("totalCount", totalCount);
		request.setAttribute("pageCount" ,pageCount);
		request.setAttribute("userList", list);
		 request.setAttribute("power","全部");
		request.getRequestDispatcher("tab/UserList.jsp").forward(request, response);
		
		
		
		
		
		
		
		
	}

}


 

UserListByPowerServlet.java

 


public class UserListByPowerServlet extends HttpServlet {

	 
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
 
		doPost(request,response);
	}

	 
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		String power=request.getParameter("power");
		if(power.equals("全部")){
			
			request.getRequestDispatcher("userListServlet").forward(request, response);
			
		}
		
		String pNo=request.getParameter("pNo");
		
		if(pNo==null || pNo.equals("") || pNo.trim().equals("0")){
			pNo="1";
		}
		oj_UserDao ud=new oj_UserDaoImpl();
		int pageSize=10;
		List list=ud.getUserList2(power,Integer.parseInt(pNo), pageSize);
		int totalCount=ud.totalCount2(power);
		int pageCount=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
		request.setAttribute("pNo", pNo);
		request.setAttribute("totalCount", totalCount);
		request.setAttribute("pageCount" ,pageCount);
		request.setAttribute("userList", list);
	  request.setAttribute("power", power);
		request.getRequestDispatcher("tab/UserList.jsp").forward(request, response);
		
		
		

		 
	}

}


 

public class UserListByPowerServlet extends HttpServlet {

	 
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
 
		doPost(request,response);
	}

	 
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		String power=request.getParameter("power");
		if(power.equals("全部")){
			
			request.getRequestDispatcher("userListServlet").forward(request, response);
			
		}
		
		String pNo=request.getParameter("pNo");
		
		if(pNo==null || pNo.equals("") || pNo.trim().equals("0")){
			pNo="1";
		}
		oj_UserDao ud=new oj_UserDaoImpl();
		int pageSize=10;
		List list=ud.getUserList2(power,Integer.parseInt(pNo), pageSize);
		int totalCount=ud.totalCount2(power);
		int pageCount=totalCount%pageSize==0?totalCount/pageSize:totalCount/pageSize+1;
		request.setAttribute("pNo", pNo);
		request.setAttribute("totalCount", totalCount);
		request.setAttribute("pageCount" ,pageCount);
		request.setAttribute("userList", list);
	  request.setAttribute("power", power);
		request.getRequestDispatcher("tab/UserList.jsp").forward(request, response);
		
		
		

		 
	}

}


 

UserList.jsp(没必要看所有的,只看与下拉列表相关的mygetmoredata()js函数,和select下拉列表)

 

<%@ page language="java" import="java.util.*,model.oj_user" pageEncoding="utf-8"%>














 <%
  	List userList=(List)request.getAttribute("userList");
  	int totalCount=Integer.parseInt(request.getAttribute("totalCount").toString());
  String s=request.getAttribute("pageCount").toString();
  	int pageCount=Integer.parseInt(s);
  	String power=request.getAttribute("power").toString();
  	int pNo=Integer.parseInt(request.getAttribute("pNo").toString());
   %>


用户基本信息列表 按权限查找 <% }else if(power.equals("超级管理员")){ %> <%} else if(power.equals("普通管理员")){ %> <%} else if(power.equals("普通用户")){ %> <%} %>
全选    添加   删除       
<% if(userList!=null){ for(int i=0;i <% }} %>
序号
用户名
用户角色
密码
总提交数
通过数
基本操作
οnclick="checkItem(this, 'mmAll')">
<%=i+1%>
<%= user.getName()%>
<%=user.getPower() %>
<%=user.getPassword() %>
<%=user.getTotal() %>
<%=user.getAccept() %>
删除 |  编辑 | 查看
    共有 <%=totalCount %> 条记录,当前第 <%=pNo %> 页,共 <%=pageCount %>
转到


 

 

他们之间的关系大致为,当第一次从别的网页要跳到userList.jsp时,首先跳到UserListServlet.jsp查询全部的数据,当在jsp页面按权限查找时,跳到UserListByPowerServlet之后,查询数据,再将数据返回到jsp页面进行显示,当在UserlistByPower遇到的权限是“全部”时,此servlet不进行查询,跳到userlistservlet后,再查询!!!

 


 

 

 

你可能感兴趣的:(web开发)