aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes MVC example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "MVC example" Watch "MVC example" New topic
Author

MVC example

Shagun Bhardwaj
Greenhorn

Joined: Feb 15, 2011
Posts: 17
Hi,

I was trying to implement MVC model in which I made a controller servlet was as follows

package com.example.web;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class BeerSelect extends HttpServlet {
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("Beer Selection Advice<br>");
String c=request.getParameter("color");
out.println("<br>Got beer color "+c);
}
}


and the model-java class was as follows

package com.example.web;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class BeerSelect extends HttpServlet {
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("Beer Selection Advice<br>");
String c=request.getParameter("color");
out.println("<br>Got beer color "+c);
}
}


But when i compile the BeerSelect.java class I get a major error- package com.example.model.*; does not exist.

Please help

Shagun
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Howdy Shagun!

I think you did post the controller code twice. Also, could you please use the code tags to distinct the source code from post text?

How did you compile your source files and what is your working directory?

Cheers!


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Shagun Bhardwaj
Greenhorn

Joined: Feb 15, 2011
Posts: 17
I compliled the code using javac BeerSelect.java

My program directories are as follows :-

C:\Program Files\Java\jdk1.6.0_23\bin\BeerSelect.java
C:\Program Files\Java\jdk1.6.0_23\bin\BeerExpert.java

and next time i'll remember to use the code tags.
And what do you mean by posting the ontroller code twice..how can it be resolved.I am using the .java and .class files in the bin directory and then I am copying it to my tomcat and projets directory as and when required.Is this was you meant..??

Shagun
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Shagun Bhardwaj wrote:I compliled the code using javac BeerSelect.java


and are you sure you have a *.java file in a com/beer package structure? You've declared the package com.beer, so you need to reflect this in your source directory structures (*.java), as well as in your *.class files.

Shagun Bhardway wrote:
My program directories are as follows :-

C:\Program Files\Java\jdk1.6.0_23\bin\BeerSelect.java
C:\Program Files\Java\jdk1.6.0_23\bin\BeerExpert.java


You better separate your project from the Java home directory and especially the "bin" one, as this is highly unexpected to put your source files there :-) Also take a look at my first comment - I think you didn't use your package structure properly.

Shagun Bhardway wrote:
And what do you mean by posting the ontroller code twice..how can it be resolved.I am using the .java and .class files in the bin directory and then I am copying it to my tomcat and projets directory as and when required.Is this was you meant..??


I mean that you posted something like this:

Shagun Bhardway wrote:
was trying to implement MVC model in which I made a controller servlet was as follows

(here is the controller servlet source code)

and the model-java class was as follows

(here is the very same controller servlet source code)


Cheers!
Shagun Bhardwaj
Greenhorn

Joined: Feb 15, 2011
Posts: 17

BeerExpert.java code




--------------------------------------------------------------------------
BeerSelect.java code




-------------------------------------------------------------------------------------------------------

These are the two classes.Now when I compile BeerSelect.java in jdk/bin folder,which contains both the java files,it gives me the error message "package com.example.model.* doen not exist."
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

1. You are not using a BeerExpert model class from your servlet class.
1a. If you would use it, you would need to import the com.example.model package in the controller servlet class.
2. I assume you have a servlet-api.jar in your JVM lib directory? If not, than invoking the command you posted (which is: javacBeerSelect.java) should not work, as there is no Servlet related classes.
3. I've copied your code and could successfully built it using: javac -cp apache-tomcat-6.0.29/lib/servlet-api.jar BeerSelect.java.

Are you sure you posted the right code and full javac command you invoke?

Cheers!

PS. It is really nice to have the directory structure as you declare packages, so:

src
    `|com
      |`--|example
      |   `--model
      |      `--BeerExpert.java
      |   `--web
      |      `--BeerSelect.java
Shagun Bhardwaj
Greenhorn

Joined: Feb 15, 2011
Posts: 17
The last post had a small correction
The model class I am using is-BeerExpert.java

And the controller class i am using BeerSelect.java

Now can you check if it works on your machine ??

And I also tried using the apache-tomcat class path you have given in the previous path to compile the code..it gives the same errors,rather some more.

I am new totally new to server programming and java ranch,so a lot of mistakes even in posting and thanks for your help,I really appreciate it.Please reply soon.

Regards
Shagun
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Shagun Bhardwaj wrote:And I also tried using the apache-tomcat class path you have given in the previous path to compile the code..it gives the same errors,rather some more.


Did you adjust the path to reflect your own location of servlet-api.jar?


Ok, your last post looks more reasonable, but...

1. You've got a typo - in your BeerSelect class you have:



Where the method is getBrands(-). Remember that java is case sensitive.

2. You are using packages: com.example.web and com.example.model.
You want to compile the BeerSelect class which imports com.example.model.BeerExpert class, but you didn't provide the package structure. These are the very basics of Java language and Java compiler (take a look at one of the first results in Google about the packages: http://www.jarticles.com/package/package_eng.html).

Try creating a proper directory structure which reflects the packages you use and add the proper path to the classpath. To visualize this, try doing this:

src
`-- com
    `-- example
        |-- model
        |    `-- BeerExpert.java
        `-- web
            `-- BeerSelect.java


Assume that the root directory which is presented above (src) is in your working directory (let's say C:\).
Now, being in C:\ execute

javac com/example/web/BeerSelect.java -cp /home/tomcat7/lib/servlet-api.jar:.


The ":." part of the -cp switch (classpath) is necessary.
Of course adjust the path to your servlet-api.jar to point to your copy.
Shagun Bhardwaj
Greenhorn

Joined: Feb 15, 2011
Posts: 17
I have understood you point 1
But as for point 2,

I have a My projects folder on the desktop

C:\Documents and Settings\user\Desktop\My Projects


This folder has only one project BeerV1 with the following file structure

BeerV1
  • etc
  • lib
  • src

  •     com
          example
            model-->BeerExpert.java
            web--->BeerSelect.java
  • web
  • classes

  •     com
          example
            model-->BeerExpert.class
            web--->BeerSelect.class



    And,In command prompt
    C:\>
    C:\>set CLASSPATH

    The result is
    CLASSPATH=C:\Program Files\Java\apache-tomcat-7.0.6\lib\servlet-api.jar


    Now I need to complie BeerSelect.java to get the BeerSelect.class file which I need for the deployment of my web app.How to proceed ??
    Piotr Nowicki
    Ranch Hand

    Joined: Jul 13, 2010
    Posts: 610

    So, you do have a *.class files in your directory structure as you pointed?

    Just like would you do for any normal Java source file to compile using javac.

    For example while you are in C:\Documents and Settings\user\Desktop\My Projects execute
    javac src/com/example/model/BeerSelect.java -d classes


    Shagun Bhardwaj
    Greenhorn

    Joined: Feb 15, 2011
    Posts: 17
    It is gives a message

    'javac' not recognized as an internal or external command
    Piotr Nowicki
    Ranch Hand

    Joined: Jul 13, 2010
    Posts: 610

    So it means you either don't have a JDK installed (which I doubt :-) or you don't have a PATH environmental variable properly set.

    Please refer i.e. to this topic for more information: http://www.coderanch.com/t/408616/java/java/PATH-enviornment-javac

    After that, open a new console and type the javac command once again.
    Shagun Bhardwaj
    Greenhorn

    Joined: Feb 15, 2011
    Posts: 17
    I did what was said in that post and that error was resolved.
    Now it says
    javac:file not found.

    But,I have checked the file there.
    Piotr Nowicki
    Ranch Hand

    Joined: Jul 13, 2010
    Posts: 610

    What is your current directory?
    what command do you invoke (exactly typeit here)?
    what is the result of invoking "javac" command? (Type the exact result here)?
    Shagun Bhardwaj
    Greenhorn

    Joined: Feb 15, 2011
    Posts: 17
    My current directory is My projects

    C:\Documents and Settings\user\Desktop\My Projects


    I typed

    javac src/com/example/model/BeerSelect.java -d classes


    The result was

    javac: file not found: src\com\example\web\BeerSelect.java
    Usage: javac <options> <source files>
    use -help for a list of possible options

    Piotr Nowicki
    Ranch Hand

    Joined: Jul 13, 2010
    Posts: 610

    Ok, I forgot that you have a BeerV1 directory in which you have a src directory. Therefore, you need to go to

    C:\Documents and Settings\user\Desktop\My Projects\BeerV1


    and execute the command once again.

    By the way, if you're reading HF Servlets and JSP, and posting in SCWCD topic I assume that you're learning to pass the SCWCD exam, right? If so, did you pass the SCJP (OCPJP) exam?
    Shagun Bhardwaj
    Greenhorn

    Joined: Feb 15, 2011
    Posts: 17
    I have BeerSelect.java in model folder
    and BeerExpert.java in web folder

    The command finally got executed and the first error was removed.

    Now it says :-

    javac src/com/example/model/BeerExpert.java -d classes

    Note: src\com\example\model\BeerExpert.java uses unchecked or unsafe operations.

    Note: Recompile with -Xlint:unchecked for details.


    javac src/com/example/web/BeerSelect.java -d classes

    src\com\example\web\BeerSelect.java:3: package com.example.model does not exist
    import com.example.model.*;
    ^
    src\com\example\web\BeerSelect.java:14: cannot find symbol
    symbol : class BeerExpert
    location: class com.example.web.BeerSelect
    BeerExpert be=new BeerExpert();
    ^
    src\com\example\web\BeerSelect.java:14: cannot find symbol
    symbol : class BeerExpert
    location: class com.example.web.BeerSelect
    BeerExpert be=new BeerExpert();


    And,I have not given any exams but I am trying my hand on java JSP to get a taste of it.I want to pursue it as my career in web/application-development and I am not sure which direction to go in.I will be completing my B.tech(Computer Science & Engineering) in April this year.So,before I go for any further course I wanted to check myself with a taste of it.Also,If you can guide me with something related to this..please do so..??

    Regards,
    Shagun
    Piotr Nowicki
    Ranch Hand

    Joined: Jul 13, 2010
    Posts: 610

    Shagun, the thing you are struggling with is purely Java world. If you would pass the OCPJP you would know how to compile Java source files and how to define the classpath.

    As I suspect, you didn't follow what I said before and you didn't add the current directory to your classpath using -cp switch of your javac:

    Pedro Kowalski wrote:Assume that the root directory which is presented above (src) is in your working directory (let's say C:\).
    Now, being in C:\ execute
    javac com/example/web/BeerSelect.java -cp /home/tomcat7/lib/servlet-api.jar:.

    The ":." part of the -cp switch (classpath) is necessary.
    Of course adjust the path to your servlet-api.jar to point to your copy.


    The learning path you've chosen is the right one, as the example you posted is from Head First Servlets & JSP book which is really a good place to start if you want to learn Java web applications. But if you don't feel confident in the Java world itself, before proceeding with Servlets, JSPs, ELs, JSTLs, etc. you can take a look at some Java book like Head First Java.
    Shagun Bhardwaj
    Greenhorn

    Joined: Feb 15, 2011
    Posts: 17
    Dear,

    Nothing is working..may be because i tried doing stuff to early.
    I tried doing what you advised in the last post but thats also not working..!!

    But thanks for such an intensive help.

    Regards
    Shagun
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Bartender

    Joined: Apr 07, 2010
    Posts: 1520
        
      22

    Hi Shagun,

    Don't give up, try to follow the following instructions:
    HowToCreateWebApplicationWithoutAnIDE

    Regards,
    Frits
    Shagun Bhardwaj
    Greenhorn

    Joined: Feb 15, 2011
    Posts: 17
    thanks
    I'll try implementing it..!!

    Regards
    Shagun
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: MVC example
     
    Similar Threads
    "Package does not exist" compilation error
    HF example Weird result - help please
    Compiling servlet version two code in HFSJ
    package help
    Wrapper cannot find servlet class