Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

compiling servlet  RSS feed

 
Patrick Smith
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friends,
i'm doing "head first servlets&jsp" in chapter 3 and am about to compile servlet version 2 (BeerSelect).
I have problems compiling this servlet:
The "package"-line is the following: com.example.web and is accepted by javac. But when importing the package where the javacode of the model is situated (com.example.model.*), javac says the following:
"src\com\example\web\BeerSelect.java:3: package com.example.model does not exist import com.example.model.*;"
Other errors follow, but are due to the fact that this package can't be found.
But the model folder beside the web folder exists and it contains the java file "BeerExpert" that represents the model logic.
Any advice on what to do?
 
Kevin Weiss
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Patrick,

I don't have the book (or code) currently in front of me, but it just sounds like your package namspace information is incorrect.

In your BeerSelect.java file, did you import the model package(s)?

import com.example.model.*;
 
Patrick Smith
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kevin,
Yes it's included. And it's exactly the line where the compiler complains it wouldn't find it.
But there is something interesting when i compile the BeerExpert java code that is located in src/com/example/model. The compiler generates the class file, but followed by this output:
"Note: src\com\example\model\BeerExpert.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details."
I've never seen this before. May be that's what's causing the trouble. FYI: i put the code of the two classes below:
a) BeerSelect.java (located in com.example.web):
"package com.example.web;

import com.example.model.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

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");

BeerExpert be = new BeerExpert();
List result = be.getBrands(c);
Iterator it = result.iterator();
while (it.hasNext())
{
out.print("<br>Try: " + it.next());
}
}
}"
b) BeerExpert.java (located in com.example.model):
"package com.example.model;
import java.util.*;

public class BeerExpert {
public List getBrands (String color) {
List brands = new ArrayList();
if (color.equals ("amber")) {
brands.add("JackAmber");
brands.add("Red Moose");}
else {
brands.add("Jail Pale Ale");
brands.add("Gout Stout");}
return (brands);
}
}"

Thanks, Patrick
 
Patrick Smith
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
is it possible to get some help on this. I've tried to change the folders and package/import names, but i can't get the servlet compiled.
Even when i put the servlet file + modelclass into the /src directory (that means i delete the "package" line in both files), javac tells me that it can't find the classes of the model when i want to comile the servlet.
I really don't know what to do anymore...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!