This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

If any exception happens in the server code error message would be displayed

 
sumanta panda
Ranch Hand
Posts: 224
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
Please look at my code.It is working properly.
My doubt is how to write the code in the sevlet and jsp if any exception is comming in server side code after sumit the jsp page message will display "Contact System Admin" if exception.Else success messgae will
display.




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

Thanks and Regards
SUMANTA

Customer.jsp
function form_sub()
{
if (validateRequest())
{
CustomerQueries.action="<%=response.encodeURL("/servlet/customerserviceServlet.MailSenderServlet")%>";
CustomerQueries.method="post";
CustomerQueries.submit();
}
}

MailSenderServlet.java
public class MailSenderServlet extends HttpServlet {
// private String strHost = null; // Mail Server name
// private String strDefaultFromUser = null; // From user name

public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{
doPost(request,response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {

String strFromUser=null;
String strToUsers=null;
String strCcUsers=null;
String strSubject=null;
String strMessageBody=null;
String []strobjAttachments=null;
String errorMessage="";
HashMap result=null;
strFromUser=request.getParameter("txtFromMail");

strToUsers=request.getParameter("To");
strCcUsers=request.getParameter("To");
strSubject=request.getParameter("selIssue");
strMessageBody=request.getParameter("txtIssue");

try
{
sendTextMail(strFromUser, strToUsers, strCcUsers, strSubject, strMessageBody, strobjAttachments);
}
catch(MailingException mEx)// Trap MailingException
{
try {
throw mEx;
} catch (MailingException e) {

}
}



}

/* public MailSenderServlet()
{
strHost = MailConfig.MAIL_HOST;
strDefaultFromUser = MailConfig.DEFAULT_FROM;
}*/


public void sendTextMail(String strFromUser, String strToUsers, String strCcUsers, String strSubject, String strMessageBody, String[] objAttachments) throws MailingException
{
try
{
// Get a Properties object

java.util.Properties objProps = System.getProperties();
objProps.put("mailhub-ap.ap.ssmb.com","25");
//java.util.Properties objProps = System.getProperties();
//objProps.put("mail.smtp.host",strHost);

// Get a Session object
Session objSession = Session.getDefaultInstance(objProps, null);

// Construct a new message
MimeMessage objMessage=new MimeMessage(objSession);

// Set up the parameters for sending the message
if ( strFromUser != null )
objMessage.setFrom(new InternetAddress(strFromUser));
objMessage.setSubject(strSubject);

if (strToUsers == null)
throw new MailingException("Email id of recipients not defined");
else
{
if (strToUsers != null)
objMessage.setRecipients(Message.RecipientType.TO,InternetAddress.parse(strToUsers));

}

if(objAttachments==null)
{
objMessage.setText(strMessageBody);
}
else
{
Multipart mpMainPart=new MimeMultipart();

//Creating multi-part message
BodyPart bpMessageBodyPart=new MimeBodyPart();//this is the Mail Message BodyPart
bpMessageBodyPart.setText(strMessageBody+"\n");

mpMainPart.addBodyPart(bpMessageBodyPart);

for(int intCount=0;intCount<objAttachments.length;intCount++)
{
BodyPart bpFileBodyPart=new MimeBodyPart();//this is the Attachment BodyPart
DataSource dsSource=new FileDataSource(new File(objAttachments[intCount]));
bpFileBodyPart.setDataHandler(new DataHandler(dsSource));
bpFileBodyPart.setFileName(objAttachments[intCount]);

mpMainPart.addBodyPart(bpFileBodyPart);
}

objMessage.setContent(mpMainPart);
}

// Send the message
Transport.send(objMessage);
}
catch(MessagingException ex)// Trap MessaginException
{ throw new MailingException(ex); }
catch(MailingException mEx)// Trap MailingException
{ throw mEx; }
}
}
 
sumanta panda
Ranch Hand
Posts: 224
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My doubt is sir in the above servlet code where and how the condition i will write if exception come go to ErrorpageQuery.jsp else success.jsp page comming.

Sir please guide me for the needful.

Thanks a lot in advance.

For Error
RequestDispatcher rd=request.getRequestDispatcher(response.encodeURL("/error/ErrorpageQuery.jsp"));
rd.forward(request,response);

For Success
RequestDispatcher rd=request.getRequestDispatcher(response.encodeURL("/success.jsp"));
rd.forward(request,response);
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends. The most common ways are:

1) Declare an error-page entry in web.xml. You can let specific Exceptions or specific HTTP status codes point to a specific error-page. This way you can generalize errors and no changes to existing code are needed.

2) Catch the exception yourself and display the error message at the same page, e.g. next to the input fields, or at the top of the form, or below the submit button, etc, depending on the kind of error.

3) Catch the exception yourself and do a forward or a redirect to the error page yourself.

I prefer a combination of 1) for unexpected exceptions and 2) for expected exceptions. Way 3) is somewhat cumbersome.
 
sumanta panda
Ranch Hand
Posts: 224
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Bauke Scholtz Sir/All,
According to your advise i did it.But i am not sure my understanding is right or wrong.
Here it will never go to success.page even if no exeception.
It will go to always ErrorpageQuery.jsp .
Dear Bauke Scholtz Sir,i request you could please rectify my below mentioned code .

Thanks in advance.
Regards
Sumanta Panda




 
Satya Maheshwari
Ranch Hand
Posts: 368
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@sumanta panda
Did ou check if following is being executed in our code:



Maybe

always throws an exception which results in the error page being displayed.

Just curious, why do you re-throw and re-catch the exception in catch block?
 
sumanta panda
Ranch Hand
Posts: 224
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Satya Sir,

I agreed your view.Could you please suggest after changing the code accoding to your point 2 advise why this
java.lang.IllegalStateException: WEB2645: Cannot forward after response has been committed is comming.

Please sir advise me for the needful.I am a begineer as a j2ee programeer.

Thanks in Advance
Regards
Sumanta

 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sumanta panda wrote:
Cannot forward after response has been committed

You can commit the response only once. The response is considered as committed when the headers are been sent. At that point you cannot change the response anymore with a forward, nor with a redirect.

A response is committed when one of the following circumstances are met:
When you write more than 8kb to the response.
When you flush the response yourself.
When you commit the response yourself.
When you invoke a forward.
When you invoke a redirect.

When you invoke another forward or redirect again after the response is been committed, you will get the above error.

So: look in your code if you aren't doing for example two consecutive forwards in a single request. Most common mistake of new developers is that they think that the code somehow immediately magically exits and jumps out of the method whenever a forward or a redirect is been called. This is untrue. Those methods doesn't behave different than all other Java methods. The code will just continue running the remaining part of the method block the usual way.
 
sumanta panda
Ranch Hand
Posts: 224
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Bauke Scholtz Sir,
I agreed.I am unable to solve the problem.

Could you please rectify my above code?

Thanks in Advance

Regards
Sumanta
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic