如何不通过进Servelt进页面

第一步:在web.xml文件中配置默认路径

              <welcome-file-list>

              <welcome-file>这里指的是默认路径</welcome-file>

              </welcome-file-list>

第二步:判断是通过默认路径跳转或者a链接还是通过表单提交按钮跳转

public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		String operate="";
		if(request.getParameter("operate")!=""){
			operate = request.getParameter("operate");
		}
		//条件查询
		if(request.getParameter("operate")!=null&&operate.equals("select")){
			String bookname = request.getParameter("bookname");
			Integer typeid = Integer.parseInt(request.getParameter("booktype")); 
			bookInfoImpl bi = new bookInfoDao();
			List<bookInfo> books = new ArrayList<bookInfo>();
			books=bi.SelectBook_BooknameTypeid(bookname,typeid);
			request.setAttribute("books", books);
		}
		//详情页面
		if(request.getParameter("bookid")!=null){
			bookInfo book = new bookInfo();
			bookInfoImpl bi = new bookInfoDao();
			int bookid = Integer.parseInt(request.getParameter("bookid"));
			book=bi.SelectBook_Bookid(bookid);
			request.setAttribute("book", book);
			request.getRequestDispatcher("/Detail.jsp").forward(request, response);
		}else{
			//转发到index.jsp界面
			request.getRequestDispatcher("/servlet/ListServelt").forward(request, response);
		}
	}

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


	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
		 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		//页面加载显示类别信息
		bookTypeImpl bt = new bookTypeDao();
		List<bookType> types = new ArrayList<bookType>();
		//查询所有类别信息
		types=bt.SelectAllType();
		//存入request作用域
		request.setAttribute("types", types);
		//判断是否第一次加载页面(下拉框是否有值)
		if(request.getAttribute("books")==null){
			//加载页面就显示所有图书信息
			bookInfoImpl bi = new bookInfoDao();
			List<bookInfo> books = new ArrayList<bookInfo>();
			books=bi.SelectBook_BooknameTypeid("", -1);
			request.setAttribute("books", books);
		}
		//保存条件查询的条件
		if(request.getParameter("bookname")!=null){
			String bookname = request.getParameter("bookname");
			request.setAttribute("bookname", bookname);
		}
		if(request.getParameter("booktype")!=null){
			Integer booktype = Integer.parseInt(request.getParameter("booktype"));
			request.setAttribute("booktype", booktype);
		}
		request.getRequestDispatcher("/index.jsp").forward(request, response);
	}

这里在web.xml文件中配置了一个默认路径 项目先走的是默认路径 就以这个例子举例 页面已加载显示所有的信息

if(request.getAttribute("books")==null)判断是books是否为空 页面默认走本Servelt所以为空 然后查询所有 在通过转发调到页面

如果是条件查询 通过提交按钮才提交到 一个Servelt 提交你查询的条件 然后查询 这个时候在跳转到 默认的路径 这个时候

if(request.getAttribute("books")==null)自然就不为空了所以走别的方法 再通过判断查询 最后在转发到你所需的界面。

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页