标签:
改成单例模式:
package com.gzzy.rmm.common.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.SingleThreadModel;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import com.gzzy.af.pojo.EmployeeInfo;
public abstract class BaseServlet extends HttpServlet{
private static final Logger log = Logger.getLogger(BaseServlet.class.getName());
protected EmployeeInfo ei = null;
protected HttpServletRequest request = null;
protected HttpServletResponse response = null;
protected PrintWriter pw = null;
public void init() throws ServletException {
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
/**
* 1:设置 此类的全局request、response属性
* 2:此方法调用 initPage 方法,继承此类实现此方法 的业务方法
*/
protected void doPost(HttpServletRequest req, HttpServletResponse resp){
/*制单人、制单日期、归属部门 start*/
EmployeeInfo ei=new EmployeeInfo();
String t = req.getParameter("tt");
if("aa".equals(t)) {
for(int i = 0; i< 10; i++) {
if(i == 0 || i == 5 || i == 9) {
ei.setEmployeeName("杨大");
ei.setId("JCT000820160324125900009431");
}
System.out.println(Thread.currentThread() + " == " + ei.getEmployeeName());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if("bb".equals(t)) {
for(int i = 0; i< 10; i++) {
if(i == 0) {
ei.setEmployeeName("kk");
ei.setId("JCT000820160324125900009431");
}
System.out.println(Thread.currentThread() + " == " + ei.getEmployeeName());
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// ei.setEmployeeName("韩江");
// ei.setId("JCT000820160324125900001451");
// ei.setEmployeeName("古琼");
// ei.setId("JCT000820160324125900001463");
// ei.setEmployeeName("龚运丽");
// ei.setId("JCT000820160324125900001465");
// ei.setEmployeeName("陈娅");
// ei.setId("JCT000820160324125900001547");
ei.setBelongorg("计划核算科");
ei.setOrgCode("JCT000320160324121800000117");
req.getSession().setAttribute("employee", ei);
/*制单人、制单日期、归属部门 end*/
try {
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("text/html");
request=req;
response=resp;
//.debug("登录用户信息");
ei=(EmployeeInfo)request.getSession().getAttribute("employee");
log.debug("登录用户信息=》"+ei.getId()+ei.getEmployeeName());
initPage(req,resp);
} catch (Exception e) {
e.printStackTrace();
}
}
public abstract void initPage(HttpServletRequest request, HttpServletResponse response) throws Exception;
/**
* 写数据到jsp前台页面去
* response内容相应类型为UTF-8
*/
protected void write(String result) {
PrintWriter pw = null;
try {
response.setContentType("text/html;charset=UTF-8");
pw = response.getWriter();
pw.write(result);
} catch (IOException e) {
log.error("向页面输出结果失败:"+e);
e.printStackTrace();
} finally {
pw.flush();
pw.close();
pw=null;
}
}
/**
* 获取取request里面的值,
* null、"" 的值都返回 ""
*/
protected String getParameter(String sourceStr){
String str=request.getParameter(sourceStr);
if(str!=null&&str.trim().length()>0){
return str;
}else{
return "";
}
}
/**
* 不能共享request里面的数据
* URL重新定向:可以是任意的URL
* 一般用于用户注销登录时返回主页面和跳转到其它的网站等等
*/
public void sendRedirect(String url) {
try {
response.sendRedirect(url);
} catch (Exception e) {
e.printStackTrace();
log.error("servlet跳转页面异常:",e);
}
}
/**
* 浏览器地址栏不变
* 转发页面和转发到的页面可以共性request里面的数据
* 页面的转发:只能是同一个Web应用程序的其他Web组件
* 一般用于用户登录的时候根据角色转发到相应的模块等等
* request.setAttribute("user", user);===jsp==>${user}/request.getAttribute("user")
* jsp=name=post===>servlet=request.getParameter("name");
*/
public void forword(String url) {
try {
request.getRequestDispatcher(url).forward(request,response);
} catch (Exception e) {
e.printStackTrace();
log.error("servlet跳转页面异常:url="+url+",request="+request,e);
}
}
public void destroy() {
super.destroy();
}
}
java.lang.IllegalStateException: Cannot forward after response has been committed
标签:
原文地址:http://www.cnblogs.com/lanjianqing/p/5516642.html