Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Headfirst Servlets & JSP

 
Garrick Solberg
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Garrick Solberg
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look carefully at the double-quotation : “ is different from "
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We also have a FAQ about this.
 
Garrick Solberg
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah, now I get it. It worked! Thank you, Christophe!
 
Garrick Solberg
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show your web.xml ?
 
Garrick Solberg
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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? : )
 
tushar bhasme
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if it was your case, but, after a change you must restart your webserver.
[=
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic