⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simulationservlet.java

📁 《湖泊生态的简单模拟》(Simulation Of Living Beings In A Lake )。这是我修读美国卡耐基梅隆大学Carnegie Mellon University(CMU)课程s
💻 JAVA
字号:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

/**
 * Class SimulationServlet processes POST requests from the browser when 
 * you click the "Start simulation" button. In response to a POST request, 
 * the SimulationServlet object creates living being objects and sends an HTML
 * representation of the lake to the browser.
 *
 * @author  张维
 * @version 1.0.0	 
 */
public class SimulationServlet 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 {
        
        /* Total time blocks to simulate */	
        int totalTimeBlocksToSimulate = 0;
        
        /* Create a session,a object of HttpSession */
        HttpSession session = request.getSession();
        
        /**
		     *  Indicate the content type (for example, text/html), 
		     *  being returned by the response
		     */
        response.setContentType("text/html");
        
        /* Retrieve an output stream to use to send data to the client */
        PrintWriter out = response.getWriter();
        
        /* Get the user input from the form */
        String numTimeBlocks = request.getParameter("numTimeBlocks");
        
        /* Create a Simulation object */
        Simulation simulation;
        
        /* Construct the HTML response : start with the header */
        if(numTimeBlocks != null) {
            totalTimeBlocksToSimulate = Integer.parseInt(numTimeBlocks);
            simulation = new Simulation(0, 0, 9, 9);
            Map allParams = request.getParameterMap();
            Set allKeys = allParams.keySet();
            
            for(Iterator it = allKeys.iterator(); it.hasNext();) {
                String key = (String)it.next();
                if(!key.equals("Submit") && !key.equals("numTimeBlocks")) {
                    String value[] = (String[])allParams.get(key);
                    for(int i = 0; i < value.length; i++)
                        LivingBeing.createLivingBeing(simulation, key, value[i]);

                }
            }

            if(totalTimeBlocksToSimulate > 0)
                response.setHeader("Refresh", "1");
            out.print(SimulationView.getHtml(simulation));
            session.setAttribute("simulation", simulation);
            session.setAttribute("totalTimeBlocksToSimulate", numTimeBlocks);
        }
        
        simulation = (Simulation)session.getAttribute("simulation");
        String totalTimeBlocksInSession = (String)session.getAttribute("totalTimeBlocksToSimulate");
        
        if(totalTimeBlocksInSession != null)
            totalTimeBlocksToSimulate = Integer.parseInt(totalTimeBlocksInSession);
        if(simulation.getTime() < totalTimeBlocksToSimulate - 1)
            response.setHeader("Refresh", "1");
            
        simulation.simulateATimeBlock();
        out.print(SimulationView.getHtml(simulation));
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -