Java HttpSession有什么问题?

Java HttpSession有什么问题?,java,tomcat,servlets,nullpointerexception,httpsession,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 C

你好,我有上面的代码

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
第43行是吗

session.setAttribute(cookieBrand,getCars(cookieBrand));
第155行是吗

if (conString.equals(Brands[i])) {
首先,我打开一个html页面,填充一个文本框,在一个radiobutton上进行选择,然后按下一个submit按钮,该按钮调用doPost方法。然后出现第二个html页面。我填充另一个文本框,按下另一个提交按钮,再次调用doPost方法(第二个if语句,screen=color)。然后。。。。。NullPointerException。它应该出现另一个html页面,其中有一个调用doGet方法的按钮。 我不知道我为什么要破例

Html第1页(品牌选择)


Cookie将写入我们的光盘
输入您的姓名:


选择您想要的品牌:
在这里查看奥迪
在这里查看福特
在这里查看丰田

HTML第2页(第二页)


考虑到
你最喜欢什么颜色?

此方法
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 ""; 

    }
私有字符串getCars(字符串构造){
对于(int i=0;i
你能给我们所有的html页面吗?关键是cookies中的代码是相同的,而不是上面的会话。这就是为什么对我来说很奇怪me@BalusC我怎样才能从表格中获得品牌?。就是这样。。。
<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>
<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>
    private String getCars(String conString) {
    for (int i = 0; i < Brands.length; ++i)
    if (conString.equals(Brands[i])) {
        return Cars[i];
    }

    return ""; 

    }