我用的是EDITPLUS
报的错误如下:---------- compile ----------
HelloServlet3.java:23: cannot resolve symbol
symbol : variable ServletUtilities
location: class coreservlets.HelloServlet3
out.println(ServletUtilities.headWithTitle(title) +
^
1 error
Normal Termination
输出完成(耗费 2 秒)。
其中的两个文件分别是:
package coreservlets;import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;/** Simple servlet for testing the use of packages
* and utilities from the same package.
* <P>
* Taken from Core Servlets and JavaServer Pages 2nd Edition
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2003 Marty Hall; may be freely used or adapted.
*/public class HelloServlet3 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Hello (3)";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1>" + title + "</H1>\n" +
"</BODY></HTML>");
}
}
和和和和和和package coreservlets;import javax.servlet.*;
import javax.servlet.http.*;/** Some simple timesavers. Note that most are static methods.
* <P>
* Taken from Core Servlets and JavaServer Pages 2nd Edition
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2003 Marty Hall; may be freely used or adapted.
*/public class ServletUtilities {
public static final String DOCTYPE =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">"; public static String headWithTitle(String title) {
return(DOCTYPE + "\n" +
"<HTML>\n" +
"<HEAD><TITLE>" + title + "</TITLE></HEAD>\n");
} /** Read a parameter with the specified name, convert it
* to an int, and return it. Return the designated default
* value if the parameter doesn't exist or if it is an
* illegal integer format.
*/
public static int getIntParameter(HttpServletRequest request,
String paramName,
int defaultValue) {
String paramString = request.getParameter(paramName);
int paramValue;
try {
paramValue = Integer.parseInt(paramString);
} catch(NumberFormatException nfe) { // null or bad format
paramValue = defaultValue;
}
return(paramValue);
} public static double getDoubleParameter
(HttpServletRequest request,
String paramName,
double defaultValue) {
String paramString = request.getParameter(paramName);
double paramValue;
try {
paramValue = Double.parseDouble(paramString);
} catch(NumberFormatException nfe) { // null or bad format
paramValue = defaultValue;
}
return(paramValue);
} /** Replaces characters that have special HTML meanings
* with their corresponding HTML character entities.
*/
// Note that Javadoc is not used for the more detailed
// documentation due to the difficulty of making the
// special chars readable in both plain text and HTML.
//
// Given a string, this method replaces all occurrences of
// '<' with '<', all occurrences of '>' with
// '>', and (to handle cases that occur inside attribute
// values), all occurrences of double quotes with
// '"' and all occurrences of '&' with '&'.
// Without such filtering, an arbitrary string
// could not safely be inserted in a Web page. public static String filter(String input) {
if (!hasSpecialChars(input)) {
return(input);
}
StringBuffer filtered = new StringBuffer(input.length());
char c;
for(int i=0; i<input.length(); i++) {
c = input.charAt(i);
switch(c) {
case '<': filtered.append("<"); break;
case '>': filtered.append(">"); break;
case '"': filtered.append("""); break;
case '&': filtered.append("&"); break;
default: filtered.append(c);
}
}
return(filtered.toString());
} private static boolean hasSpecialChars(String input) {
boolean flag = false;
if ((input != null) && (input.length() > 0)) {
char c;
for(int i=0; i<input.length(); i++) {
c = input.charAt(i);
switch(c) {
case '<': flag = true; break;
case '>': flag = true; break;
case '"': flag = true; break;
case '&': flag = true; break;
}
}
}
return(flag);
}
}
恳请再次关注:
报的错误如下:---------- compile ----------
HelloServlet3.java:23: cannot resolve symbol
symbol : variable ServletUtilities
location: class coreservlets.HelloServlet3
out.println(ServletUtilities.headWithTitle(title) +
^
1 error
Normal Termination
输出完成(耗费 2 秒)。
其中的两个文件分别是:
package coreservlets;import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;/** Simple servlet for testing the use of packages
* and utilities from the same package.
* <P>
* Taken from Core Servlets and JavaServer Pages 2nd Edition
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2003 Marty Hall; may be freely used or adapted.
*/public class HelloServlet3 extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Hello (3)";
out.println(ServletUtilities.headWithTitle(title) +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1>" + title + "</H1>\n" +
"</BODY></HTML>");
}
}
和和和和和和package coreservlets;import javax.servlet.*;
import javax.servlet.http.*;/** Some simple timesavers. Note that most are static methods.
* <P>
* Taken from Core Servlets and JavaServer Pages 2nd Edition
* from Prentice Hall and Sun Microsystems Press,
* http://www.coreservlets.com/.
* © 2003 Marty Hall; may be freely used or adapted.
*/public class ServletUtilities {
public static final String DOCTYPE =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">"; public static String headWithTitle(String title) {
return(DOCTYPE + "\n" +
"<HTML>\n" +
"<HEAD><TITLE>" + title + "</TITLE></HEAD>\n");
} /** Read a parameter with the specified name, convert it
* to an int, and return it. Return the designated default
* value if the parameter doesn't exist or if it is an
* illegal integer format.
*/
public static int getIntParameter(HttpServletRequest request,
String paramName,
int defaultValue) {
String paramString = request.getParameter(paramName);
int paramValue;
try {
paramValue = Integer.parseInt(paramString);
} catch(NumberFormatException nfe) { // null or bad format
paramValue = defaultValue;
}
return(paramValue);
} public static double getDoubleParameter
(HttpServletRequest request,
String paramName,
double defaultValue) {
String paramString = request.getParameter(paramName);
double paramValue;
try {
paramValue = Double.parseDouble(paramString);
} catch(NumberFormatException nfe) { // null or bad format
paramValue = defaultValue;
}
return(paramValue);
} /** Replaces characters that have special HTML meanings
* with their corresponding HTML character entities.
*/
// Note that Javadoc is not used for the more detailed
// documentation due to the difficulty of making the
// special chars readable in both plain text and HTML.
//
// Given a string, this method replaces all occurrences of
// '<' with '<', all occurrences of '>' with
// '>', and (to handle cases that occur inside attribute
// values), all occurrences of double quotes with
// '"' and all occurrences of '&' with '&'.
// Without such filtering, an arbitrary string
// could not safely be inserted in a Web page. public static String filter(String input) {
if (!hasSpecialChars(input)) {
return(input);
}
StringBuffer filtered = new StringBuffer(input.length());
char c;
for(int i=0; i<input.length(); i++) {
c = input.charAt(i);
switch(c) {
case '<': filtered.append("<"); break;
case '>': filtered.append(">"); break;
case '"': filtered.append("""); break;
case '&': filtered.append("&"); break;
default: filtered.append(c);
}
}
return(filtered.toString());
} private static boolean hasSpecialChars(String input) {
boolean flag = false;
if ((input != null) && (input.length() > 0)) {
char c;
for(int i=0; i<input.length(); i++) {
c = input.charAt(i);
switch(c) {
case '<': flag = true; break;
case '>': flag = true; break;
case '"': flag = true; break;
case '&': flag = true; break;
}
}
}
return(flag);
}
}
恳请再次关注:
我在Eclipse+MyEclipse中测试了没问题
运行:
javac coreservlets/HelloServlet3.java
我也是初学,也正在看servlet与jsp核心编程.今后多多交流