Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Servlet/JSP/JSF/JavaFX Script  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
reply to postflat modethreaded modego to previous topicgo to next topicgo to back
作者 新手请教. (Re: Cookie)
fictive





发贴: 3
于 2006-01-15 16:49 user profilesend a private message to userreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
请尽量使用准确的文字描述作为标题
(甚至只是简单的错误信息-- 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


why edited on 2006-01-15 23:33


话题树型展开
人气 标题 作者 字数 发贴时间
1280 新手请教. (Re: Cookie) fictive 2832 2006-01-15 16:49

reply to postflat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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