java web之中,如果Servlet中有耗时较长的计算,该怎么做才好?
2016-03-17 23:28
513 查看
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
}
}
下面用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
}
}
相关文章推荐
- Javase小项目尝试
- SpringMvc入门
- Java编程思想第三章---有关运算符的一些细节注意和思考
- Java RandomAccessFile用法
- 图片缩放时java.lang.IllegalArgumentException: pointerIndex out of range解决方案
- struts.xml配置文件(package,namespace,action)
- Java线程(七):Callable和Future
- 能不能简单描述一下你在java web开发中需要用到多线程编程的场景?
- com.mysql.jdbc.PreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V错误解决
- 深入理解Java之泛型
- JAVA第二周随笔
- Spring MVC使用@ResponseBody返回JSON数据406以及乱码问题解决方案
- java 接口Comparable和Comparator的使用
- 如何在eclipse jee中创建Maven project并且转换为Dynamic web project
- 浅析Java中的final关键字
- struts2---struts.xml文件头
- QT 的Moc Uic Rcc有什么用(360ui移植到eclipse)
- Java技术_Java千百问(0007)_为什么会报"错误: 找不到或无法加载主类 HelloWord.class"
- eclipse 预览Android界面报错
- java第八天