HttpSession有什么问题?

时间:2015-12-14 20:56:36

标签: java tomcat servlets nullpointerexception httpsession

您好我有上面的代码

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


public class CarSessionServlet2 extends HttpServlet {
    private String Brands[] = {
        "Audi", "Ford", "Toyota"
    };
    private String Cars[] = {
        "A3", "Fiesta", "Yaris"
    };
    private String screen = "name";

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // reaction


        if (screen.equals("name")) { //elegxos apo poia selida kalw tin doPost. Edw apo tin BrandsSelection.html

            String cookieName = request.getParameter("name"); // choice made
            // will be sent
            // back to
            // client
            String cookieBrand = request.getParameter("brands");

            PrintWriter output;

            HttpSession session = request.getSession(true);
            session.setAttribute("name", cookieName);




            session.setAttribute(cookieBrand, getCars(cookieBrand));

            response.setContentType("text/html");

            output = response.getWriter();

            // send HTML page to client
            output.println("<HTML><HEAD><meta charset='utf-8'><TITLE>");
            output.println("Cookies");
            output.println("</TITLE></HEAD><BODY>");
            output.println("<P>Welcome Ms./Mr.: ");
            // output.println( "<P>" );
            output.println(cookieName);
            output.println(" <BR> you have chosen ");
            output.println(cookieBrand);
            output.println("<BR><a href='SecondPage.html'>" + "Click here to continue..." + "</a>");
            output.println("</BODY></HTML>");
            output.close(); // close stream
            screen = "color";


        }

        if (screen.equals("color")) { //elegxos apo poia selida kalw tin doPost. Edw apo tin SecondPage.html
            PrintWriter output;
            String color = request.getParameter("color");

            HttpSession session = request.getSession(true);
            session.setAttribute("color", color);

            response.setContentType("text/html");
            output = response.getWriter();

            // send HTML page to client
            output.println("<HTML><HEAD><meta charset='utf-8'><TITLE>");
            output.println("Cookies");
            output.println("</TITLE></HEAD><BODY>");
            output.println("<FORM ACTION=\"http://localhost:8080/Askisi2Session/CarSessionServlet2\" METHOD=\"GET\">");
            output.println("<STRONG>To see your selections press the button:<br> </STRONG>");
            output.println("<INPUT TYPE=\"submit\" VALUE=\"Selections\">");
            output.println("</FORM>");
            output.println("</BODY></HTML>");

            output.close(); // close stream
            screen = "name";
        }
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // reaction
        // to
        // the
        // reception
        PrintWriter output; // of
        // GET
        response.setContentType("text/html");
        output = response.getWriter();

        output.println("<HTML><HEAD><meta charset='utf-8'><TITLE>");
        output.println("Cookie with your selections has been read !");
        output.println("</TITLE></HEAD><BODY>");

        HttpSession session = request.getSession(false); // get client's session;
        output.println("<H3>Here is your saved session data:</H3>");
        Enumeration e;
        if (session != null) {
            e = session.getAttributeNames();
        } else {
            e = null;
        }
        while (e.hasMoreElements()) {
            String name = (String) e.nextElement();
            output.println(name + ": " + session.getAttribute(name) + "<BR>");
        }





        output.println("<a href='BrandsSelection.html'>Click here to Restart...</a>");
        output.println("</BODY></HTML>");
        output.close(); // close stream
    }

    private String getCars(String conString) {
        for (int i = 0; i < Brands.length; ++i)
        if (conString.equals(Brands[i])) {
            return Cars[i];
        }

        return ""; // no matching string found

    }

} // END OF CLASS CookieServlet

当我用tomcat 8.0.28运行它时,我得到java.lang.NullPointerException提及

java.lang.NullPointerException
CarSessionServlet2.getCars(CarSessionServlet2.java:155)
CarSessionServlet2.doPost(CarSessionServlet2.java:43)
javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

第43行是

session.setAttribute(cookieBrand,getCars(cookieBrand));

和第155行是

if (conString.equals(Brands[i])) {

首先我打开一个html页面,我填写一个文本框,在radiobutton上做出选择,然后我按下一个调用doPost方法的提交按钮。然后出现第二个html页面。我填满另一个文本框,按下另一个提交按钮,我再次调用doPost方法(第二个if语句,屏幕=颜色)。然后...... NullPointerException。它应该出现另一个带有调用doGet方法的按钮的html页面。 我不知道为什么我会采取这种例外

Html Page 1(BrandsSelection)

<HTML>
<HEAD>
   <TITLE>Cookie will be written in our disc</TITLE> 
</HEAD>
<BODY>
   <FORM ACTION="http://localhost:8080/Askisi2Session/CarSessionServlet2" METHOD="POST">      
      <STRONG>Enter Your Name:<br> </STRONG>
      <PRE>
      <INPUT TYPE="text" NAME="name"><br><br>
      <STRONG>Select the Brand of your Desire:<br> </STRONG>
      <PRE>
      <INPUT TYPE="radio" NAME="brands" VALUE="Audi">check here for Audi<BR>
      <INPUT TYPE="radio" NAME="brands" VALUE="Ford">check here for Ford<BR>
      <INPUT TYPE="radio" NAME="brands" VALUE="Toyota" CHECKED>check here for Toyota<BR>
      </PRE>
      <INPUT TYPE="submit" VALUE="Submit">
      <INPUT TYPE="reset"> </P>
   </FORM>
</BODY>

</HTML>

HTML PAGE 2(SeondPage)

<HTML>
<HEAD> 
   <TITLE>Cookie taken into Account</TITLE> 
</HEAD>
<BODY>
   <FORM ACTION="http://localhost:8080/Askisi2Session/CarSessionServlet2" METHOD="POST">
      <STRONG>What is your favorite color?<br> </STRONG>
      <PRE>
      <INPUT TYPE="text" NAME="color"><br>
      </PRE>
      <INPUT TYPE="submit" VALUE="Submit">
   </FORM>
</BODY>
</HTML>

1 个答案:

答案 0 :(得分:0)

此方法getCars(...)返回"",因为条件if(conString.equals(Brands[i]))与任何字符串都不匹配:

    private String getCars(String conString) {
    for (int i = 0; i < Brands.length; ++i)
    if (conString.equals(Brands[i])) {
        return Cars[i];
    }

    return ""; 

    }