473,395 Members | 1,688 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to write RequestDispatcher in the doPost()

Dear All,
In the DoPost() i am calling the function sendTextMail(strFromUser, strToUsers, strCcUsers, strSubject, strMessageBody, strobjAttachments) in the Try catch block.
Now the code compiles perfectly.
My doubt is how i will write RequestDispatcher method in Dopost()
if it is success then it will go to
RequestDispatcher rd=request.getRequestDispatcher(response.encodeURL ("/help/SuccessApplicationStatus.jsp"));
rd.forward(request,response);

else if it is error then
RequestDispatcher rd=request.getRequestDispatcher(response.encodeURL ("/error/errorApplicationStatus.jsp"));
rd.forward(request,response);

Please look at my code and guide me for the needful.

Thanks in Advance.
Regards
Sumanta Panda

Expand|Select|Wrap|Line Numbers
  1. public void doPost(HttpServletRequest request, HttpServletResponse response)
  2.  throws IOException, ServletException {
  3.  
  4.  String strFromUser=null;
  5.  String strToUsers=null;
  6.  String strCcUsers=null;
  7.  String strApplnno=null;
  8.  String strSubject=null;
  9.  String strMessageBody=null;
  10.  String []strobjAttachments=null;
  11.  String errorMessage="";
  12.  HashMap result=null;
  13.  strFromUser=request.getParameter("txtMail");
  14.  
  15. strToUsers="care.cwaindia@citi.com";
  16. strCcUsers="care.cwaindia@citi.com";
  17. strApplnno=request.getParameter("txtApplno");
  18. strSubject="Application Form Status";
  19. //strMessageBody=strApplnno.concat("-Application Form Status");
  20.  strMessageBody= "Application Form Status for Application Form no.-".concat(strApplnno);
  21.  
  22.  try
  23.  {
  24.  
  25.  sendTextMail(strFromUser, strToUsers, strCcUsers, strSubject, strMessageBody, strobjAttachments);
  26.  //RequestDispatcher rd=request.getRequestDispatcher(response.encodeURL("/help/SuccesspageApplicationStatus.jsp"));
  27. // rd.forward(request,response);
  28.  
  29.  }
  30.  catch(MailingException mEx)// Trap MailingException
  31.  {
  32.  try {
  33.  throw mEx;
  34.  } catch (MailingException e) {
  35.      //RequestDispatcher rd=request.getRequestDispatcher(response.encodeURL("/error/ErrorpageApplicationStatus.jsp"));
  36.     //rd.forward(request,response);
  37.      System.out.println("Data2"); 
  38.  }
  39.  }
  40.  
  41.  
  42.  
  43.  } 
  44.  

Expand|Select|Wrap|Line Numbers
  1.  public void sendTextMail(String strFromUser, String strToUsers, String strCcUsers, String strSubject, String strMessageBody, String[] objAttachments) throws MailingException
  2.  {
  3.  try
  4.  {
  5.  // Get a Properties object
  6.  
  7.  
  8.  java.util.Properties objProps = System.getProperties();
  9.  objProps.put("mailhub-ap.ap.ssmb.com",strHost);
  10.  
  11.  // Get a Session object
  12.  Session objSession = Session.getDefaultInstance(objProps, null);
  13.  
  14.  // Construct a new message
  15.  MimeMessage objMessage=new MimeMessage(objSession);
  16.  
  17.  // Set up the parameters for sending the message
  18.  if ( strFromUser != null )
  19.  objMessage.setFrom(new InternetAddress(strFromUser));
  20.  objMessage.setSubject(strSubject);
  21.  
  22.  if (strToUsers == null)
  23.  throw new MailingException("Email id of recipients not defined");
  24.  else
  25.  {
  26.  if (strToUsers != null)
  27.  objMessage.setRecipients(Message.RecipientType.TO,InternetAddress.parse(strToUsers));
  28.  
  29.  }
  30.  
  31.  if(objAttachments==null)
  32.  {
  33.  objMessage.setText(strMessageBody);
  34.  }
  35.  else
  36.  {
  37.  Multipart mpMainPart=new MimeMultipart();
  38.  
  39.  //Creating multi-part message
  40.  BodyPart bpMessageBodyPart=new MimeBodyPart();//this is the Mail Message BodyPart
  41.  bpMessageBodyPart.setText(strMessageBody+"\n");
  42.  
  43.  mpMainPart.addBodyPart(bpMessageBodyPart);
  44.  
  45.  for(int intCount=0;intCount<objAttachments.length;intCount++)
  46.  {
  47.  BodyPart bpFileBodyPart=new MimeBodyPart();//this is the Attachment BodyPart
  48.  DataSource dsSource=new FileDataSource(new File(objAttachments[intCount]));
  49.  bpFileBodyPart.setDataHandler(new DataHandler(dsSource));
  50.  bpFileBodyPart.setFileName(objAttachments[intCount]);
  51.  
  52.  mpMainPart.addBodyPart(bpFileBodyPart);
  53.  }
  54.  
  55.  objMessage.setContent(mpMainPart);
  56.  }
  57.  
  58.  // Send the message
  59.  Transport.send(objMessage);
  60.  }
  61.  catch(MessagingException ex)// Trap MessaginException
  62.  { throw new MailingException(ex); }
  63.  catch(MailingException mEx)// Trap MailingException
  64.  { throw mEx; }
  65.  
  66.  }
  67.  
  68.  }
  69.  
Jan 7 '09 #1
3 7078
r035198x
13,262 8TB
Please use code tags when posting code and make sure that the code is formatted.

Do the forwarding after the catch statement
Expand|Select|Wrap|Line Numbers
  1. String url = "/help/SuccesspageApplicationStatus.jsp";
  2. try  {
  3.    sendTextMail(strFromUser, strToUsers, strCcUsers, strSubject, strMessageBody, strobjAttachments);  
  4. catch(MailingException mEx) {
  5.      url = "/error/errorApplicationStatus.jsp";
  6. }
  7. RequestDispatcher rd=request.getRequestDispatcher(response.encodeURL(url));  rd.forward(request,response); 
  8.  
Jan 7 '09 #2
Dear r035198x Sir,
I did the same thing the code which you have rectified.

Please sir guide me for the needful.
Thanks in advance.
Regards
Sumanta


But Now it is showing an error

for host 127.0.0.1 trying to POST /servlet/customerserviceServlet.ApplicationStatusSevlet, service-j2ee reports: StandardWrapperValve[invoker]: WEB2792: Servlet.service() for servlet invoker threw exception
java.lang.IllegalStateException: WEB2645: Cannot forward after response has been committed

Jan 7 '09 #3
r035198x
13,262 8TB
Post the code that you have now. Please use code tags when you post the code.
Jan 8 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: K S Aldebaraan | last post by:
I'm trying to submit a form with an action of a servlet, and a view equal to the same jsp page. I'm not sure what I'm doing wrong, but keep getting a NullPointerException on the second line of...
1
by: techy techno | last post by:
Hii Just wanted to know how can I decorate my texboxes and Listmenu which is called from a JS file using the following code below: document.write("<SELECT NAME='cur2' ONCHANGE='cconv1();'>");...
5
by: xirowei | last post by:
i'm newbie in java servlet, how to let public void doPost can access to public void doGet, stringLength variable? below is my code: import javax.servlet.*; import javax.servlet.http.*; import...
1
by: vaskarbasak | last post by:
Hi all, Why do we override the doGet and/or doPost methods instead of the service method? Thanks! vaskar
1
sid0404
by: sid0404 | last post by:
Hi I need to send data from my servlet to my html(which contains AJAX), so as per the motivation of the AJAX, this should be done without my webpage reloading / refresh. my code on the ajax...
5
dmjpro
by: dmjpro | last post by:
Could anyone tell me what is difference between jsp:forward and RequestDispatcher.forward? These two codes behave differently ... <jsp:forward page="page_ref"/> and
1
by: sumanta123 | last post by:
Dear Sir, I am calling procedure in my servlet program.The procedure returns correct valule. For example If the Logid id wrong then errorType Returns E and errorDesc LOGIN ID IS NOT AVAILABLE....
3
hsriat
by: hsriat | last post by:
Can anyone put some light on how to receive as an array (or multiple values for same variable) in Java using doPost() method? Thank you
0
by: tangara | last post by:
Hi, I have tried using Override void doPost (HTTPServletRequest request, HttpServletResponse response) to get data from a html form which states form action = post. However, I can get the data...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.