Java.lang.IllegalStateException:Cannot forward after response have been committed

Source: Internet
Author: User

Change to Singleton mode:

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: Set the global request, response property of this class
* 2: This method calls the Initpage method, inheriting this class of business methods that implement this method
*/
protected void DoPost (HttpServletRequest req, HttpServletResponse resp) {

/* Single, single date, attribution Department start*/
EmployeeInfo ei=new EmployeeInfo ();
String t = req.getparameter ("tt");
if ("AA". Equals (t)) {
for (int i = 0; i<; i++) {
if (i = = 0 | | i = = 5 | | i = = 9) {
ei.setemployeename ("Yang da");
Ei.setid ("JCT000820160324125900009431");
                }
System.out.println (Thread.CurrentThread () + "= =" + Ei.getemployeename ());
try {
Thread.Sleep (+);
} catch (Interruptedexception e) {
e.printstacktrace ();
                }
            }
        }

if ("BB". Equals (t)) {
for (int i = 0; i<; i++) {
if (i = = 0) {
ei.setemployeename ("KK");
Ei.setid ("JCT000820160324125900009431");
                }
System.out.println (Thread.CurrentThread () + "= =" + Ei.getemployeename ());
try {
Thread.Sleep (+);
} catch (Interruptedexception e) {
e.printstacktrace ();
                }
            }
        }
        
Ei.setemployeename ("Han Jiang");
Ei.setid ("JCT000820160324125900001451");

Ei.setemployeename ("Gujon");
Ei.setid ("JCT000820160324125900001463");

Ei.setemployeename ("Shingli");
Ei.setid ("JCT000820160324125900001465");

Ei.setemployeename ("Chenya");
Ei.setid ("JCT000820160324125900001547");

ei.setbelongorg ("Program Accounting section");
Ei.setorgcode ("JCT000320160324121800000117");
Req.getsession (). SetAttribute ("employee", EI);
/* Single, single date, attribution Department end*/
try {
Req.setcharacterencoding ("UTF-8");
Resp.setcharacterencoding ("UTF-8");
Resp.setcontenttype ("text/html");
Request=req;
RESPONSE=RESP;
. Debug ("Login user Information");
Ei= (EmployeeInfo) request.getsession (). getattribute ("employee");
Log.debug ("Logon User Information =" +ei.getid () +ei.getemployeename ());
Initpage (REQ,RESP);
} catch (Exception e) {
E.printstacktrace ();
}
}

public abstract void Initpage (HttpServletRequest request, httpservletresponse response) throws Exception;

/**
* Write data to JSP foreground page to go
* Response content corresponding type is 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 ("Failed to output results to page:" +e);
E.printstacktrace ();
} finally {
Pw.flush ();
Pw.close ();
Pw=null;
}
}

/**
* Get the value inside the request,
* NULL, value of "" returns ""
*/
Protected string GetParameter (string sourcestr) {
String Str=request.getparameter (SOURCESTR);
if (Str!=null&&str.trim (). Length () >0) {
return str;
}else{
Return "";
}
}

/**
* Cannot share data from request
* URL redirection: can be any URL
* Generally used for users to log off when they return to the main page and jump to other sites and so on
*/
public void Sendredirect (String URL) {
try {
Response.sendredirect (URL);
} catch (Exception e) {
E.printstacktrace ();
Log.error ("servlet jump Page Exception:", E);
}
}

/**
* The browser address bar does not change
* Forward page and forwarded to the page can be in common request data inside
* Page Forwarding: Only other Web Components of the same Web application
* Generally used for user login, according to the role of forwarding to the corresponding module and so on
* 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 jump page Exception: url=" +url+ ", request=" +request,e);
}
}

public void Destroy () {
Super.destroy ();
}
}

Java.lang.IllegalStateException:Cannot forward after response have been committed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.