一个Servlet例子,编译没任何错,执行还是错,很郁闷
报错是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.";
}
}