Don't get hanged up on this servlet question.  Just because it's a servlet
doesn't change Java rules.  Of course you can instantiate a class in a servlet.
You don't have to import if it's in the same/none package and is in the
classpath

see below

HTH

dav.

Lalith Jayaweera wrote:

> Hi
>
> Why Should I extend the HttpServlet why Can't I have as follows
>
> class Lecturer{
>
> private String lecID;
> private String lecname;
>
> ..
>
> public void setname(String name_in){
> ...
> }
>
> ....
>
> }
>
> the Lecturer class is saved seperately as lecturer.java
>
> what I need is How can I call it within the servlet
>
> that is how can I use above in the following it gives errors,(I need to
> instantiate objects from the above class inside the following servlet)
>
> import java.io.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
>
> public class admin2 extends HttpServlet{
>
> ....

// assuming that Lecturer is a class in a classpath of your servlet engine;
// not necessarily in the servlets directory
public void doGet(HttpServletRequest    request,
                      HttpServletResponse    response
                     )throws ServletException, IOException
{

    HttpSession session = request.getSession();
    // create new lecturer
    Lecturer lect = new Lecturer()
    // put it in session context for future use when you come back
    // with another request (or for use in JSP)
    session.putValue("Lecturer",lect);

sContext.getRequestDispatcher("/LecturerPage.jsp").forward(request,response);

}

>
> ...
> ....
>
> }
>
> lalith
>
> At 11:32 AM 11/1/99 -0700, you wrote:
> >Lalith Jayaweera wrote:
> >>
> >> Hi everybody,
> >> In my project I have  classes written seperately such as
> >> (subject,deparment,lecturer,courses etc..) How can I instantiate the
> >> objects of these classes and call methods of these classes
> >> inside the servlet.
> >> Should I import all the classes I need or What?
> >>
> >> (Simply, How can deal with above classes inside the doGet method)
> >
> >Simply, the same way you would inside any other Java class or method
> >
> >//import the classes, not shown
> >
> >public class SchoolServlet extends HttpServlet {
> >  //As class members
> >  private Subject[] subjects;
> >  private Department[] departments
> >  //etc.
> >
> >  public School () {
> >    subjects = new Subject[NUMBER_OF_SUBJECTS];
> >    departments = new Department[NUMBER_OF_DEPARTMENTS];
> >  }
> >
> >  //Inside a method
> >  public someMethod () {
> >    Subject subject = new Subject ("Calculus");
> >    subject.doSomething ();
> >    Department department = new Department ("Mathematics");
> >    department.doSomething ();
> >  }
> >}
> >
> >___________________________________________________________________________
> >To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> >of the message "signoff SERVLET-INTEREST".
> >
> >Archives: http://archives.java.sun.com/archives/servlet-interest.html
> >Resources: http://java.sun.com/products/servlet/external-resources.html
> >LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
> >
>
> ___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

--
David Mossakowski        [EMAIL PROTECTED]
http://www.dwdog.com/styk      212.310.7275

"I don't sit idly by, I'm planning a big surprise"
F         U         G         A        Z        I

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to