aspose file tools*
The moose likes Tomcat and the fly likes Servlet Compilation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "Servlet Compilation" Watch "Servlet Compilation" New topic
Author

Servlet Compilation

Ayanik Sil
Greenhorn

Joined: Sep 23, 2007
Posts: 27
Hello everybody,
My directory structure is

C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\Books\WEB-INF\classes\com\example\web

where i have put
a file called BookSelect.java
and in the directory
C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\Books\WEB-INF\classes\com\example\model

I have a file called BookPrice.java

code for BookSelect.java is

package com.example.web;

import com.example.model.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
class BookSelect extends HttpServlet {
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException {
PrintWriter out=response.getWriter();
out.println("<br>You have Requested<>");
String c=request.getParameter("Book");
out.println("<br>"+c+"<br>");
out.println("<br>Price Of "+c+"is<br>");
BookPrice bp=new BookPrice();
String prices=bp.price(c);
out.println("<br>"+prices+"<br");
}}

and for BookPrice.java is

package com.example.model;

public class BookPrice {
String value="v";
public String price(String name) {
if(name.equals("HeadFirst Servlets and JSP")) {
value="$50"; }
else {
value="$30";
}
return value;
}}

now,to compile the servlet
from which directory should i compile?
is it from "Books" directory?
will a plain javac command will do?
Since BookSelect.java imports BookPrice.java,
shall i have to set any classpath?
I had set a classpath up to /WEB-INF/com directory,
but during compiling it was seen that BookSelect.java could not find BookPrice.java. shall I keep both the java files in WEB-INF/classes directory?if yes,then what command shall i give?
please help.I am confused.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42602
    
  65
You will need servlet-api.jar in your classpath, as was pointed out to you in the other thread.

You can compile several files at the same time, even if they're in different directories. If you change to the classes directory, you can do something like

javac com/example/web/BookSelect.java com/example/model/BookPrice.java


Ping & DNS - my free Android networking tools app
Ayanik Sil
Greenhorn

Joined: Sep 23, 2007
Posts: 27
Hey thanks for replying.
If i go to the classes directory for compiling BookSelect.java,giving the aforesaid command,then the compiler cannot find BookPrice.java
there are no problems with other lines,except regarding finding BookPrice.java

I had always problems with packages.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42602
    
  65
If i go to the classes directory for compiling BookSelect.java,giving the aforesaid command,then the compiler cannot find BookPrice.java


There shouldn't be with the directory layout you posted above. You can help the compiler even more by setting the classpath explicitly to the current directory:


[ September 29, 2007: Message edited by: Ulf Dittmer ]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12823
    
    5
Classpath for servlet compilation can get complicated, especially if you are compiling from the command line. If your system has multiple versions of Java and multiple versions of various toolkits, things can get very confusing.

Years ago I started using Ant for all Java project compilation and testing because of the ease in defining the classpath and other options on a project by project basis. Ant has a bit of a learning curve but you can start simple.

Bill
Ayanik Sil
Greenhorn

Joined: Sep 23, 2007
Posts: 27
Hey ulf,
I compiled the servlet successfully
I used
javac com/example/web/BookSelect.java com/example/model/BookPrice.java
for compilation.

Now,then when i restarted tomcat it gives
error 500 for the first time when it is run
and then error 404 (probably file not found)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Servlet Compilation