File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Headfirst Servlets & JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Headfirst Servlets & JSP" Watch "Headfirst Servlets & JSP" New topic
Author

Headfirst Servlets & JSP

Garrick Solberg
Greenhorn

Joined: Jul 21, 2010
Posts: 8
I'm trying to write out the code in chapter 3 using notepad, as the book suggests, but I am encountering errors when I try to compile. The errors are numerous in both count and variety, but the most common seems to be problems with certain characters. I have typed the code in manually and copied/pasted with the same effect. What am I doing wrong?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

The errors are numerous in both count and variety,

Let's fix them one at a time Can you post the compiler errors you are getting ?


[My Blog]
All roads lead to JavaRanch
Garrick Solberg
Greenhorn

Joined: Jul 21, 2010
Posts: 8
It was easier for me to grab the chapter 1 example, which had the same issues. Here's the full list:

C:\HeadFirstJSP\project1>javac -classpath c:\tomcat\apache-tomcat-6.0.28\lib\servlet-api.jar -d classes src\Ch1Servlet.java
src\Ch1Servlet.java:21: illegal character: \8220
out.println(ô<html> ô +
^
src\Ch1Servlet.java:21: illegal character: \8220
out.println(ô<html> ô +
^
src\Ch1Servlet.java:21: not a statement
out.println(ô<html> ô +
^
src\Ch1Servlet.java:23: illegal character: \8220
ô<body>ö +
^
src\Ch1Servlet.java:21: not a statement
out.println(ô<html> ô +
^
src\Ch1Servlet.java:23: illegal character: \8221
ô<body>ö +
^
src\Ch1Servlet.java:25: illegal character: \8220
ô<h1 align=center>HF\Æ s Chapter1 Servlet</h1>ö
^
src\Ch1Servlet.java:23: not a statement
ô<body>ö +
^
src\Ch1Servlet.java:25: > expected
ô<h1 align=center>HF\Æ s Chapter1 Servlet</h1>ö
^
src\Ch1Servlet.java:25: illegal start of expression
ô<h1 align=center>HF\Æ s Chapter1 Servlet</h1>ö
^
src\Ch1Servlet.java:25: illegal character: \92
ô<h1 align=center>HF\Æ s Chapter1 Servlet</h1>ö
^
src\Ch1Servlet.java:25: illegal character: \8217
ô<h1 align=center>HF\Æ s Chapter1 Servlet</h1>ö
^
src\Ch1Servlet.java:25: ';' expected
ô<h1 align=center>HF\Æ s Chapter1 Servlet</h1>ö
^
src\Ch1Servlet.java:25: illegal start of type
ô<h1 align=center>HF\Æ s Chapter1 Servlet</h1>ö
^
src\Ch1Servlet.java:25: ';' expected
ô<h1 align=center>HF\Æ s Chapter1 Servlet</h1>ö
^
src\Ch1Servlet.java:25: illegal character: \8221
ô<h1 align=center>HF\Æ s Chapter1 Servlet</h1>ö
^
src\Ch1Servlet.java:26: illegal character: \8220
+ ô<br>ö + today + ô</body>ö + ô</html>ö);
^
src\Ch1Servlet.java:26: illegal character: \8221
+ ô<br>ö + today + ô</body>ö + ô</html>ö);
^
src\Ch1Servlet.java:26: illegal character: \8220
+ ô<br>ö + today + ô</body>ö + ô</html>ö);
^
src\Ch1Servlet.java:26: not a statement
+ ô<br>ö + today + ô</body>ö + ô</html>ö);
^
src\Ch1Servlet.java:26: illegal start of type
+ ô<br>ö + today + ô</body>ö + ô</html>ö);
^
src\Ch1Servlet.java:26: illegal start of expression
+ ô<br>ö + today + ô</body>ö + ô</html>ö);
^
src\Ch1Servlet.java:26: illegal character: \8221
+ ô<br>ö + today + ô</body>ö + ô</html>ö);
^
src\Ch1Servlet.java:26: not a statement
+ ô<br>ö + today + ô</body>ö + ô</html>ö);
^
src\Ch1Servlet.java:26: illegal character: \8220
+ ô<br>ö + today + ô</body>ö + ô</html>ö);
^
src\Ch1Servlet.java:26: not a statement
+ ô<br>ö + today + ô</body>ö + ô</html>ö);
^
src\Ch1Servlet.java:26: illegal start of type
+ ô<br>ö + today + ô</body>ö + ô</html>ö);
^
src\Ch1Servlet.java:26: illegal start of expression
+ ô<br>ö + today + ô</body>ö + ô</html>ö);
^
src\Ch1Servlet.java:26: illegal character: \8221
+ ô<br>ö + today + ô</body>ö + ô</html>ö);
^
src\Ch1Servlet.java:26: not a statement
+ ô<br>ö + today + ô</body>ö + ô</html>ö);
^
src\Ch1Servlet.java:26: illegal start of expression
+ ô<br>ö + today + ô</body>ö + ô</html>ö);
^
31 errors

C:\HeadFirstJSP\project1>
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

I think that some weird characters came in during your copy/paste. This sometimes happens, for example when copy/pasting some text from Word. You'll have to replace all of them. For example, you can see that the double quotation is not right. You'll have to open your file again, and replace all the weird double quotations with ". There seems to be other strange characters too. Replace them too.
Garrick Solberg
Greenhorn

Joined: Jul 21, 2010
Posts: 8
Hmm, I'm not really seeing them when I open the file. Here are the Ch1Servlet.java contents:

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

public class Ch1Servlet extends HttpServlet {


public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException {


PrintWriter out = response.getWriter();
java.util.Date today = new java.util.Date();
out.println(“<html> “ +
“<body>” +
“<h1 align=center>HF\’ s Chapter1 Servlet</h1>”
+ “<br>” + today + “</body>” + “</html>”);
}
}
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Look carefully at the double-quotation : “ is different from "
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

We also have a FAQ about this.
Garrick Solberg
Greenhorn

Joined: Jul 21, 2010
Posts: 8
Ah, now I get it. It worked! Thank you, Christophe!
Garrick Solberg
Greenhorn

Joined: Jul 21, 2010
Posts: 8
Ok, new problem. I have compiled the class, created the directory structures, and created the web.xml file, as well as copying them to the appropriate tomcat directories. Tomcat is running (I can access its default page), but when I try to access http://localhost:8080/ch1/Serv1 I get a 404 and the description reads: The requested resource () is not available.
tushar bhasme
Ranch Hand

Joined: Feb 11, 2008
Posts: 50
just 1 ques... what is Serv1... if its a servlet, you would have to specify its ext as http://localhost:8080/ch1/Serv1.do ...
Garrick Solberg
Greenhorn

Joined: Jul 21, 2010
Posts: 8
tushar bhasme wrote:just 1 ques... what is Serv1... if its a servlet, you would have to specify its ext as http://localhost:8080/ch1/Serv1.do ...


Hmm, I'll give that a shot. I have gotten the example to work through my IDE, but it would be nice to do it as the book describes.

EDIT: Sadly, no such luck. I'll stick with netbeans for now I guess.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Can you show your web.xml ?
Garrick Solberg
Greenhorn

Joined: Jul 21, 2010
Posts: 8
Christophe Verré wrote:Can you show your web.xml ?


Sure thing, here goes:

<?xml version="1.0" encoding="ISO-8851-1" ?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ms/j2ee
http://java.sun.com/xml/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>Chapter1 Servlet</servlet-name>
<servlet-class>Ch1Servlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Chapter1 Servlet</servlet-name>
<url-pattern>/Serv1</url-pattern>
</servlet-mapping>
</web-app>

Fiorenza Oppici
Ranch Hand

Joined: Jul 22, 2010
Posts: 38
I was asking for info yesterday on the same exercise, compared to mine your xml file content seems to be the same (and my version does work).
i'm not such an expert, but my point is that maybe some file is in the wrong directory and Tomcat can't deploy the project.
maybe it's because you did not paste the xml file in the Tomcat WEB-INF folder? : )


[I study here]
Regards, Fiò
tushar bhasme
Ranch Hand

Joined: Feb 11, 2008
Posts: 50
can you tell us the directory structure for your project under tomcat\webapps, and which file have is kept in which folder? maybe something is wrong with that? but you have already mentioned that its working in netbeans, so am not sure whats going wrong...
Garrick Solberg
Greenhorn

Joined: Jul 21, 2010
Posts: 8
Well, I tried again today and now it works. I have no idea what made it work, but I hope this will hold for future exercises. Thanks for all the help, guys!
Hebert Coelho
Ranch Hand

Joined: Jul 14, 2010
Posts: 754

I don't know if it was your case, but, after a change you must restart your webserver.
[=


[uaiHebert.com] [Full WebApplication JSF EJB JPA JAAS with source code to download] One Table Per SubClass [Web/JSF]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Headfirst Servlets & JSP