首页 > 编程语言 >java web之中,如果Servlet中有耗时较长的计算,该怎么做才好?

java web之中,如果Servlet中有耗时较长的计算,该怎么做才好?

时间:2023-05-20 13:04:08浏览次数:35  
标签:web java int throws println response Servlet method out


java web之中,如果Servlet中有耗时较长的计算,该怎么做才好?

下面用Thread.sleep(5000)来模拟耗时较长的计算:

public class calcServlet extends HttpServlet {

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @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 doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * 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 req, HttpServletResponse res)
			throws ServletException, IOException {
		
		String numa = req.getParameter("numa");
		String numb = req.getParameter("numb");
		int iNuma=Integer.parseInt(numa);
		int iNumb=Integer.parseInt(numb);
		int calcResult=calc(iNuma,iNumb);
		
		String strCalcResult=String.valueOf(calcResult);
		req.setAttribute("calcResult", strCalcResult);
		
		req.getRequestDispatcher("/showCalcResult.jsp").forward(req, res);
	}

	private int calc(int iNuma, int iNumb) {
		// TODO Auto-generated method stub
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return iNuma+iNumb;
	}
/*
	public native int calc(int iNuma, int iNumb);
	static{
	System.out.println(System.getProperty("java.library.path"));
	System.out.print("start load!!!");
	System.loadLibrary("calc");
	System.out.print("end load!!!");
	}	
*/	
	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here

	}

}




标签:web,java,int,throws,println,response,Servlet,method,out
From: https://blog.51cto.com/gjwrxz/6317419

相关文章

  • servJump extends HttpServlet //request.getRequestDispatcher //response.sendRed
    packagecom.test;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.HashMap;importjava.util.Map;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;impo......
  • [RapidOCRWeb] 桌面版使用教程
    引言说明:桌面版指的是可以直接解压,双击即可运行的版本。通俗来说,对rapidocr_web做了打包,将相关依赖全部放到一个zip包中,不需要本地有额外的环境,降低使用门槛。下面会以Windows版为例,作简要说明。详情参见:RapidOCRWeb使用步骤下载对应的zip包目前已有的zip包如下:......
  • 适配器模式改造Servlet
    1. 我们编写一个Servlet类直接实现Servlet接口有什么缺点?  11  - 我们只需要service方法,其他方法大部分情况下是不需要使用的。代码很丑陋。2. 适配器设计模式Adapter   11  - 手机直接插到220V的电压上,手机直接就报废了。怎么办?可以找一个充电器。这个充电器就是一个......
  • 学习Javaweb使用jsp中的EL表达式中的错误
    使用EL表达式后数据在页面中不显示浏览器中数据显示的样子:解决方案:在JSP页面中添加以下代码<%@pageisELIgnored="false"%>最后数据显示:......
  • Java命令执行
    (未完待续……)1.Java的命令执行方式JDK中提供的可执行系统命令的API有:java.lang.Runtimejava.lang.ProcessBuilderjava.lang.UNIXProcess(Linux)/java.lang.ProcessImpl(Windows)2.java.lang.Runtimeimportjava.io.ByteArrayOutputStream;importjava.io.IOExceptio......
  • Java文件与IO流
    首先我们要清楚什么是流,正如其名,很形象,流就是像水一样的东西,具有方向性,在java中,流大概就是类接下来,我们要对输入输出流有一个基本认识,什么是输入输出流呢?输入输出明显需要一个参照,而这个参照就是主存。清楚了上面的概念,我们接着看下去吧。文件文件的创建文件创建共有三种......
  • 基于Web的智慧工业园区三维可视化安全管控系统
    建设背景随着经济飞速发展和产业创新升级,作为新经济形式的重要载体,工业园区污染严重、安全生产难以监管等问题日益突出。工业园区作为工业高质量发展的重要载体和平台,工厂聚集,安全生产风险集中,在这个背景下,建设智慧工业园区已经成为了许多企业和政府的共同选择。总体架构工业园区智......
  • java调用python并且实现RESTAPI
    在Eclipse中创建springboot工程(maven)配置pom.xml<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocati......
  • C#调用WebService方法
    1.已知WebService接口的URL,直接调用在VS中,添加服务引用--高级--添加web引用直接输入webserviceURL然后,直接实例化该命名空间下的类的对象,调用该接口下的各个方法即可。2.动态调用以下为帮助类:usingMicrosoft.CSharp;usingSystem;usingSystem.CodeDom;usingSystem.CodeDom......
  • java基于springboot+vue的漫画网站管理系统,附源码+数据库+lw文档+PPT,适合毕业设计、课
    1、项目介绍考虑到实际生活中在漫画网站方面的需要以及对该系统认真的分析,将系统权限按管理员和用户这两类涉及用户划分。(a)管理员;管理员使用本系统涉到的功能主要有:首页、个人中心、用户管理、漫画分类管理、漫画投稿管理、分类管理、排行榜管理、交流论坛、系统管理等功能......