Topic: 新手请教. (Re: Cookie)

  Print this page

1.新手请教. (Re: Cookie) Copy to clipboard
Posted by: fictive
Posted on: 2006-01-15 16:49

请尽量使用准确的文字描述作为标题
(甚至只是简单的错误信息-- What are the error messages?)

若新帖再無恰当标题,可能会被刪除

原來的标题:
新手请教



我按照书上例子写了一个Servlet小程序,编译通过.运行就抛出空指针错误,请大家帮忙看看.
ServletUtilities.java:

package myjava;

import javax.servlet.http.Cookie;

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

public static String getCookieValue(Cookie[] cookies, String cookieName,String defaultValue) {
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookieName.equals(cookie.getName())) {
return (cookie.getValue());
}
}
return (defaultValue);
}

}

}


SearchEnginesFrontEnd.java:

package myjava;

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

/**
* Servlet implementation class for Servlet: SearchEnginesFrontEnd
*
*/
public class SearchEnginesFrontEnd extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

public SearchEnginesFrontEnd() {
super();
}


protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
// TODO Auto-generated method stub
Cookie[] cookies=req.getCookies();

String searchString=ServletUtilities.getCookieValue(cookies,"searchString","Java Programming"); //出错的地方
//String searchString="null"; //如果用这句替换上面一句的话,就正常
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String title="Searching the Web";
out.println(ServletUtilities.headWithTitle(title)
+"<body bgcolor=#fdf5e6>\n"
+"<h1 align=center>Searching the Web</h1>\n"+"\n"
+"<form action=CustomizedSearchEngines>\n"
+"<center>\n"+"Search String:\n"
+"<input type=text name=searchString value="+searchString+"><br>\n"
+"<input type=radio name=searchEngine value=google>Google\n"
+"<input type=radio name=searchEngine value=baidu>Baidu\n"
+"<input type=radio name=searchEngine value=hotbot>Hotbot\n"
+"<input type=radio name=searchEngine value=yahoo>Yahoo<br>\n"
+"<input type=submit value=search><br>\n"
+"</center>\n"+"</form>\n"+"</body>\n"+"</html>\n");
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request,response);
}
}


我的工具是sdk1.5+eclipse+tomcat


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923