We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

addon for "Technical Problem" posted by Kuching

poweranger77
poweranger77 asked
on
Medium Priority
378 Views
Last Modified: 2010-04-01
Hello there, i read one of the post indicating this issues, i have some question as my application also similar to Kuching's problems...
>>
http://www.experts-exchange.com/Web/Web_Languages/JSP/Q_21772778.html

(A) page 1 ==> (B) servlet 1==> (C) manager (DB retrieve) ==> (B) servlet 1==> (D) page 2

From (A) to (B), i am using the web.xml help to pass the request to servlet. In the doGet method(), i call the managerObject that is doing the connection and logic. Once the calculation and logic is done in (C), i will return it back to the (B). In (B) now, i will set the result (the return of the (C)) as a session and the redirect to (D) as a session. In (D) i will get the result as a session.
>>


I am wondering if i would like to include a MessageObject for this application where should i put it in? I know all the database transaction are happend in the Manager (C) level, but then where should i include this MessageObject operation in?

MessageObject() {
public String main_message="";
public String sub_message="";
public String target_url="";
public String source_url="";
public String flat =""; //indication is a error message or a success_message;

}

1. i am wondering if i put it in (C), which is all the transaction of the DB take place, how am i suppose to pass the targe_url and source_url?

2. if i put it in the (B) which is the servlet, handling the traffic of the application, i would not able to know the error happend during the DB transaction as most of the error happend there.

Thus, how is the common practise for this kind of "message" handling things happend in web applicaiton? please share with me...

thanks..

Comment
Watch Question

Commented:
Hi poweranger77,

You should not mixup servlet/front-end/display logic with business logic.
So, never put target_url or source_url in C. Better way to do this is to
throw a exception in C whenever an error occurs. In your servlet, once you
catch that exception, you know something is wrong and can do a workaround
for it.

________
radarsh

Author

Commented:
radarsh, yeah, i am totally agree. And i think dont understand my Q.

What i am trying to seek is the proper way of handling this kind of messageObject. Where should i put it is the most ideal way. If i dump this in the Manager (C), i will not able to know how to set the ratget_url and source_url. And if i dump it in the Servelt, i would not able to catch the error as most of the tiime the error happend during the transaction process.

share with me, how you all implement this kind of messageObject
CERTIFIED EXPERT
Top Expert 2006

Commented:
It depends on whether it's part of the model or not. If you caonsider the MVC pattern which is implemented in Struts, this is how it would work, as in this pseudocode

doGet(response, request){
      HttpSession session = request.getSession() ;
      try{
            Manager managerObject = new Manager(//args) ;//create DB Manager object, provide with args/parameters
            MessageObject mo = managerObject.getMessageObject() ;
            if( MessageObject.validate() ){ //validate method to check it's all okay
                  //MessageObject has no business exceptions
                  session.setAttribute("mo" , mo);
                  RequestDispatcher rd = request.getRequestDispatcher("success.jsp") ;
                        rd.forward(request, response) ;
            }else{
                  //MessageObject has business exceptions
                  //this could be duplicate info or well formed but erroneous data
                  session.setAttribute("mo" , mo);
                  RequestDispatcher rd = request.getRequestDispatcher("failure.jsp") ;
                        rd.forward(request, response) ;
            }
      }catch(SQLException se){
            //there has been an application level error/exception, i.e. can't get connection to db
            request.setAttribute("appError" , se) ; //so the error page has access to the exception
            session.setAttribute("mo" , mo); //still keep the data
            RequestDispatcher rd = request.getRequestDispatcher("error.jsp") ;
            rd.forward(request, response) ;
      }
}

Commented:
Hi,

bloodredsun way of handling is good.

One more thing i don't understand is to keep the target_url and source_url in messageObject. What purpose does it solve?

If the target_url is dependent upon the exceptions thrown in the MessageObject, then it can very well be handled in the servlet with the request dispatcher to an appropriate error page.

regards,
fargo





 

Author

Commented:
bloodredsun, i assuem this line [ Manager managerObject = new Manager ] will start the db/business logic transaction.

Thus, i am wondering, for this line [  if( MessageObject.validate() ){ //validate method to check it's all okay ]  the system definitely will able to return true or NoError message since it's not connected to the ManageObject at all.

For this block,
[
catch(SQLException se){
          //there has been an application level error/exception, i.e. can't get connection to db
          request.setAttribute("appError" , se) ; //so the error page has access to the exception
          session.setAttribute("mo" , mo); //still keep the data
          RequestDispatcher rd = request.getRequestDispatcher("error.jsp") ;
          rd.forward(request, response) ;
     }
]
I am in serious doubt that the it can catch any error happending in the DB/logic trasncation object, which is in this place is [ managerObject ]. Becasue all the possible transaction is happending there and in the servlet, i am not able to cathc those errors.

Thats why i plan to set the MessageObject in the DB/Logic object. But then, another problem. how can i ser the targetUrl and SourceUrl there? After all, it'll automatically route to that JSp as instructed.

Fargo, targetURL and sourceURl is for the system to redirect to the correct page after the messageObject dont his job. If there is an error, it'll back to the source_url. If it's success, it'll go to the targetURL.
Commented:
Hi,

source and target url setting in the message object is not the correct way. In your case, Servlet acts as a controller. So, this information should be handled based on the validation returned by the messageObject and redirection should definitely be done in the servlet.

Moreover with the bloodredsun handling of the Manager and messageObject, it's all correct with his assumption of implementation [i assume -:)]. If you provide us the code of Manager and messageObject, one can clearly say how to handle it in servlet.

regards,
fargo

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
fargo, here you go... entire flow...

jsp:
==
..
<form action="servletA" method="POST" >
..
..
<input type="submit" Value="Add Item">
..

servletA:
=====
doGet() {

  String user_name = request.getParameter("user_name");
  String password = request.getParameter("password");
  String role = request.getParameter("role");

  UserManager um = new UserManager();
  um.insertData(user_manager, password, role);
  RequestDispatcher dispatcher = req.getRequestDispatcher("targetUrl.jsp");
  dispatcher.forward(req, res);

}

UserManager.java
============
..
..
 public void inserData(String user_name, String password, String role) {
    try {
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      String url="jdbc:mysql://localhost:3306/netradb?user=root&password=password";
      conn = DriverManager.getConnection(url);
      Statement stmt = conn.createStatement();
      String sql = "INSERT INTO TABLE_A (user_name, password, role)......";

      int result = stmt.executeQuery(sql);
       System.out.println("Number of row affected " + result);
     
   } catch (Exception e)  { e.printStackTrace(); }
   } finally (Exception e)
     {  if (conn!=null) conn.close();
        if (stmt!=null) stmt.close();
     }
}

*************
In a certain way, i do agree with both of you, that servlet should handling the traffic, which means controlling the targetUrl and sourceUrl. So, if i would do that, i will need to only check the messageObject in the manager level. But then few question on this:

1. How can i redirect the message from MessageObject to the jsp? By using session?
2. Can  the messageObject able to capture the error happend in the Manager's transaction above? i means the DB/SQL transaction..
3. Do i need to declare the entire MessageObject's method, variable as a static ?
 

Author

Commented:
what i dont understand is, when there is an error happend in the manager level, the entire system will be down and stop. So, how can i still able to passback the error Message to servlet and to jsp for user.

Author

Commented:
guys, i have an ideas..

what happend if i calling the MessageObject in the servlet level, and in the ManagerLevel, i catch my own exception, call MyException and throw it to the servlet.

And i the servlet i will catch that MyException. If there is no MyException catch, i shall declare as a succesful transaction if not that MyException is the error one.. and redirect from there..

but how can i declare my own exception? and is it workable?

Author

Commented:
thanks guy, i thikn i have finally come out with the conclusion.

i will declare it in the servlet level.

then i will throws the error that potentially will happend in the managerlevel. in the servlet level i will try to catch it if any.

then from there i shall decide if there is an error, i will redirect it to the error.jsp in the catch block. if none, then i will consider to forward this message to redirect to the correct page.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.