一个Servlet例子,编译没任何错,执行还是错,很郁闷

ruru235 2003-12-12 06:22:09
报错是NullPointerException,在我标记的那行,编译没任何错,但是通过URL引用的时候报错,我实在不知道为什么了,大家帮帮忙,分不多,谢谢啊
import javax.servlet.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

/**
* The ColorSessionServlet demonstrates the session management
* mechanisms built into the servlet API. A session is
* established and the client's preferred background color
* as well as the number of times they have requested the
* servlet are stored.
*/
public class ColorSessionServlet extends HttpServlet
{
/**
* Generates HTML pages that allows the client to choose a
* color and then remembers the color on future requests.
*/
public void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException
{
String name; //name of user
String color; //user's color preference
Integer hitCount; //# of times user has requested servlet

//get current session, create a new one if it doesn't exist
HttpSession mySession = request.getSession(true);

//MIME type to return is HTML
response.setContentType("text/html");

//get a handle to the output stream
PrintWriter out = response.getWriter();

if (mySession.isNew()) //first time client requests page
{
//generate HTML form requesting name and color preference
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Color Selector</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<FORM METHOD=\"POST\" " +
"ACTION=\"ColorSessionServlet\">");
out.println("Please select background color:<BR>");
out.println("<INPUT TYPE=\"RADIO\" NAME=\"bcolor\" " +
"VALUE=\"white\">White<BR>");
out.println("<INPUT TYPE=\"RADIO\" NAME=\"bcolor\" " +
"VALUE=\"red\">Red<BR>");
out.println("<INPUT TYPE=\"RADIO\" NAME=\"bcolor\" " +
"VALUE=\"green\">Green<BR>");
out.println("<INPUT TYPE=\"RADIO\" NAME=\"bcolor\" " +
"VALUE=\"blue\">Blue<P>");
out.println("Please enter your name:<BR>");
out.println("<INPUT TYPE=\"TEXT\" NAME=\"name\" " +
"SIZE=\"25\"><P>");
out.println("<INPUT TYPE=\"SUBMIT\" VALUE=\"Submit\">");
out.println("</BODY>");
out.println("</HTML>");
}
else //client has already established a session
{
if (request.getParameter("bcolor") != null)
{
//client is submitting the color preference form
String bcolor; //for user's preferred background color
bcolor = request.getParameter("bcolor");

//get HEX code for color
if (bcolor.equals("red"))
{
color = "#FF0000";
}
else if (bcolor.equals("green"))
{
color = "#00FF00";
}
else if (bcolor.equals("blue"))
{
color = "#0000FF";
}
else //if nothing selected, default to white
{
color = "#FFFFFF";
}

name = request.getParameter("name"); //get user name
hitCount = new Integer(1); //requested 1 time so far

mySession.setAttribute("bcolor", color); //store color
mySession.setAttribute("hitCount", hitCount);
mySession.setAttribute("name", name); //store user name
}
else //user has previously submitted HTML form
{
//get color, name, and hit count from session
color = (String)mySession.getAttribute("bcolor");
name = (String)mySession.getAttribute("name");
hitCount = (Integer)mySession.getAttribute("hitCount");
}

//increment hit count and store in session
mySession.setAttribute("hitCount",new Integer(hitCount.intValue() + 1));//错就在这行,NullPointerException

out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Color Selected</TITLE>");
out.println("</HEAD>");
out.println("<BODY BGCOLOR=\"" + color + "\">");
out.println("<H2>Hello " + name + "!</H2>");
out.println("<H3>You have requested this page " +
hitCount.toString() + " times.<BR>");
out.println("Notice how session management allows me " +
"to remember<BR>");
out.println("who you are, how many times you've " +
"requested this page,<BR>");
out.println("and your preferred background color.<P>");
out.println("<A HREF=\"http://127.0.0.1:8080/test/ColorSessionServlet\">Reload " +
"Page</A></H3>");
out.println("</BODY>");
out.println("</HTML>");
}
out.close();
}


/**
* Returns a brief description of this servlet.
*
* @return Brief description of servlet
*/
public String getServletInfo()
{
return "Servlet uses session management to maintain color.";
}
}
...全文
55 3 打赏 收藏 举报
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
ruru235 2003-12-12
哈哈
一楼的搞笑了
我现在是软件蓝领都不是,俺还是学生,学java没两个月,最近才开始web编程

谢谢二楼啊,这烂程序是《精通Servlets>里面的一个例子,是我没好好分析,谢谢啊
  • 打赏
  • 举报
回复
mor 2003-12-12
哈哈,这个问题又来了。

1.OK,当第一次进来时,mySession.isNew()=true,然后就是显示有Form的页面。
2.然后提交,这个时候的判断条件是request.getParameter("bcolor")!= null
3. a. 如果true则从request里读出提交的内容,
b. 如果false则从session里读出上次的设置。
但是,如果在2时不选任何一个颜色选项,那么到3时,就是false,然后从session里读hitCount,当然读了个null,然后直接用hitCount.intValue(),然后弹出NullPointerException。

这个烂程序我见了好几次了,也不知是谁写的垃圾
  • 打赏
  • 举报
回复
bdsc 2003-12-12
编译没错 软件蓝领
运行没错 程序员
功能没错 开发者
结构没错 设计者

都没错(因为没做) manager
  • 打赏
  • 举报
回复
相关推荐
发帖
Web 开发
加入

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2003-12-12 06:22
社区公告
暂无公告