Results 1 to 4 of 4
  1. #1
    iwanabeguru is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default java classes in webapp which are not servlets

    Hi

    I'm learning java ee and i not found answers for some questions and i hope somebody can explain its for me.

    Now the questions.

    1. Can i load normal java classes from webapps without using any servlet, and can i use these objects without storing them in the Application context. If yes , how can it be done.

    2. If i create an object in a servlet init() method , can i reach this object via a get() method. I think i this is impossible because the servlet instantiated by the container and the reference of the servlet object is not available

    3. If i get an object from the Application context will i get a referene or a copy of that object?

    Thank you for your answers and i hope i put this topic to the right place.

    iwanabeguru
    Last edited by iwanabeguru; 02-08-2011 at 10:49 AM. Reason: typo in title

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Hi,
    I will try answer you.
    1. Sure you can use normal classes from webapps, But if you want invoke this classes you need use servlets classes or jsp in any case.
    2. Sure. The servlet this is usually classess Java. You can use get() and set() methods so in usually classes
    3. In Java all objects, which not primary get with use reference or you need use clone() method or create new instance.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    iwanabeguru is offline Member
    Join Date
    Feb 2011
    Posts
    4
    Rep Power
    0

    Default

    Thank you for your help.

    to question 2.
    For example if i have servlet1 and servlet2 and i want call a method from servlet1 in servlet2 how can i do it?

    3.
    So if i obtain an object from Application context i will only get the reference to that object and not a copy. So if i change this object when i get it from another servlet i will see the new state of the object?

    Am i right?

    Thank you for your answers

    iwanabeguru

  4. #4
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    The same way how you can with usaully Java class.
    for example
    Java Code:
    package action;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class LoginServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //to do anything
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //to do anything
        }
    }
    Java Code:
    package action;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class TaskServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            LoginServlet loginServlet = new LoginServlet();
            loginServlet.doPost(request, response);
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //to do anything
        }
    }
    But I think it is bad approach.
    Application context - on client site it is just html text. You can't change any object. but you can use scriptlet. I think the best way if you use web MVC framework. for example Spring or Struts.
    Skype: petrarsentev
    http://TrackStudio.com

Similar Threads

  1. Replies: 0
    Last Post: 06-03-2010, 02:56 PM
  2. Replies: 12
    Last Post: 01-17-2010, 11:05 PM
  3. catching uncaught exceptions using Spring / WebApp
    By taille50 in forum Web Frameworks
    Replies: 1
    Last Post: 10-18-2009, 01:14 AM
  4. How to run java classes?
    By crunchybear in forum Advanced Java
    Replies: 7
    Last Post: 10-20-2008, 10:57 AM
  5. Java Servlets
    By kondalrao in forum Java Servlet
    Replies: 0
    Last Post: 04-14-2008, 11:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •