• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MVC example

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Shagun Bhardwaj
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
    Posts: 623
    1
    IntelliJ IDE Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 17
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    It is gives a message

    'javac' not recognized as an internal or external command

     
    Piotr Nowicki
    Ranch Hand
    Posts: 623
    1
    IntelliJ IDE Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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: https://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
    Posts: 17
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 623
    1
    IntelliJ IDE Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 17
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 623
    1
    IntelliJ IDE Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 17
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 623
    1
    IntelliJ IDE Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 17
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    Creator of Enthuware JWS+ V6
    Posts: 3411
    320
    Android Eclipse IDE Chrome
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Shagun,

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

    Regards,
    Frits
     
    Shagun Bhardwaj
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thanks
    I'll try implementing it..!!

    Regards
    Shagun
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic