2 zzjmay zzjmay 于 2013.10.04 16:22 提问

在Eclipse中运行程序时,跳转到Servlet的操作就执行一次

index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
/jsp:forward
IndexServlet.java
package com.yxq.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
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 javax.servlet.http.HttpSession;

import com.yxq.dao.ArticleDao;
import com.yxq.valuebean.ArticleBean;

/**

  • Servlet implementation class IndexServlet
    */
    @WebServlet(urlPatterns={"/IndexServlet"})
    public class IndexServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**

    • Default constructor. */ public IndexServlet() { // TODO Auto-generated constructor stub }

    /**

    • @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response);

    }

    /**

    • @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session=request.getSession();
      ArticleDao articleDao=new ArticleDao(); //获取主页面的显示内容 //获取前三篇文章 ArrayList articleList=articleDao.queryArticle(-1, null); //测试语句 System.out.println("长度"+articleList.size()); request.setAttribute("articleList", articleList); //还有其他的例如像左侧栏的一系列的内容,都要在主页显示,以后实现 //实现跳转到指定的页面 RequestDispatcher dispatcher=request.getRequestDispatcher("/front/FrontIndex.jsp"); dispatcher.forward(request, response);

    }

}
以后重新启动服务器,不会更新内容,还是原来获取的内容

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!